lesson 7 collections, generics jni. what is the collections framework? collections framework...

111
Lesson 7 Collections, Generics JNI

Post on 22-Dec-2015

245 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Lesson 7

Collections, Generics

JNI

Page 2: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

What is the Collections framework? Collections framework provides two things:

– implementations of common high-level data structures: e.g. Maps, Sets, Lists, etc.

– An organized class hierarchy with rules/formality for adding new implementations

The latter point is the sense in which Collections are a framework.

Note the difference between providing a framework + implementation and just implementation.

Some other differences:– code reuse– clarity– unit testing?

Page 3: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Definition of colelction A collection — sometimes called a container — is

simply an object that groups multiple elements into a single unit.

Collections are used to store, retrieve, manipulate, and communicate aggregate data.

They typically represent data items that form a natural group, e.g.– poker hand (a collection of cards), a mail folder (a

collection of letters), or a telephone directory (a mapping from names to phone numbers).

Page 4: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

History Pre Java SDK1.2, Java provided a handful of data

structures:– Hashtable– Vector– Bitset

These were for the most part good and easy to use, but they were not organized into a more general framework.

SDK1.2 added the larger skeleton which organizes a much more general set of data structures.

Legacy datastructures retrofitted to new model. Generic types/autoboxing added in 1.5

Page 5: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

General comments about data structures “Containers” for storing data. Different data structures provide different

abstractions for getting/setting elements of data.– linked lists– hashtables– vectors– arrays

Same data structures can even be implemented in different ways for performance/memory:– queue over linked list– queue over arrays

Page 6: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

More on data structures

Everyone should take a basic class in building data structures

I recommend the book Mastering Algorthims with C by Kyle Loudon

In Java, one does not usually build data structures, but rather uses the provided one

Using Java’s data structures requires a little understanding of the Collections framework

Adding your own requires a deeper understanding.

Page 7: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Learning to use data structures

Dual purposes for us to study Collections:– Be able to choose, properly use built-in data

structures.– Another study in OO class design

Thus, we start by study the Collections class design.

Then, we provide many examples of how to use the built-in types in real programming.

Page 8: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Collections-related Interface hierarchy

Collection

List Set

SortedSet

Map

SortedMap

Iterator

ListIterator

• The Collection inteface stores groups of Objects, with duplicates allowed• The Set interface extends Collection but forbids duplicates• The List interface extends Collection, allows duplicates, and introduces positional indexing.• Map is a separate hierarchy

Page 9: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Collection implementations

Note that Java does not provide any direct implementations of Collection.

Rather, concrete implementations are based on other interfaces which extend Collection, such as Set, List, etc.

Still, the most general code will be written using Collection to type variables.

Page 10: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

A Peek at generics

Old way

List myIntList = new LinkedList(); // 1myIntList.add(new Integer(0)); // 2Integer x = (Integer) myIntList.iterator().next(); // 3

New way with Generics …

List<Integer> myIntList = new LinkedList<Integer>(); // 1’myIntList.add(new Integer(0)); //2’Integer x = myIntList.iterator().next(); // 3’

Page 11: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Generics vs. Casting Note that new method is backward compatible

with old method– Old code ports– Warnings issues by compiler

What are some advantages of Generics?

What are some disadvantages?

How can we use Generics if we are mixing types?

Page 12: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Another example of GenericsHere is a simple example taken from the existing Collections tutorial: // Removes 4-letter words from c. Elements must be strings static void expurgate(Collection c) { for (Iterator i = c.iterator(); i.hasNext(); ) if (((String) i.next()).length() == 4) i.remove(); }

Here is the same example modified to use generics: // Removes the 4-letter words from c static void expurgate(Collection<String> c) { for (Iterator<String> i = c.iterator(); i.hasNext(); ) if (i.next().length() == 4) i.remove(); }

Think “Collection of Strings”

Page 13: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Generics

There are lots of little subtleties introduced by this capability.

Better not to dwell on them this week – will pick up after we study the Collections themselves.

Page 14: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Collection Interface

boolean add(Object o);boolean addAll(Collection c);void clear();boolean contains(Object o);boolean containsAll(Collection c);boolean equals(Object o);int hashCode();boolean isEmpty();Iterator iterator();boolean remove(Object o);boolean removeAll(Collection c);boolean retainAll(Collection c);int size();Object[] toArray();Object[] toArray(Object[] a);

Optional operation, throwUnsupportedOperationException

What does this mean in termsof what we’ve learned aboutInterfaces and OO architecture?

Page 15: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Comments on Collection methods Note the iterator() method, which returns an

Object which implements the Iterator interface.

Iterator objects are used to traverse elements of the collection in their natural order.

Iterator has the following methods:– boolean hasNext(); // are there any more elements?– Object next(); // return the next element– void remove(); // remove the next element

Page 16: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

AbstractCollection Class

java.util.AbstractCollection •Abstract class which is partial implementation of of Collection interface

•Implements all methods except iterator() and size()

• Makes it much less work to implement Collections Interface

Page 17: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

List interface

An interface that extends the Collections interface. An ordered collection (also known as a sequence).

– The user of this interface has precise control over where in the list each element is inserted.

– The user can access elements by their integer index (position in the list), and search for elements in the list.

Unlike Set, allows duplicate elements. Provides a special Iterator called ListIterator for

looping through elements of the List.

Page 18: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Additional methods in List Interface List extends Collection with additional

methods for performing index-based operations:– void add(int index, Object element) – boolean addAll(int index, Collection collection) – Object get(int index) – int indexOf(Object element) – int lastIndexOf(Object element) – Object remove(int index) – Object set(int index, Object element)

Page 19: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

List/ListIterator Interface The List interface also provides for

working with a subset of the collection, as well as iterating through the entire list in a position friendly manner:– ListIterator listIterator() – ListIterator listIterator(int startIndex) – List subList(int fromIndex, int toIndex)

ListIterator extends Iterator and adds methods for bi-directional traversal as well as adding/removing elements from the underlying collection.

Page 20: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Concrete List Implementations There are two concrete implementations of the List

interface– LinkedList– ArrayList

Which is best to use depends on specific needs. Linked lists tend to be optimal for inserting/removing

elements. ArrayLists are good for traversing elements sequentilly

Note that LinkedList and ArrayList both extend abstract partial implementations of the List interface.

Page 21: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

LinkedList Class

The LinkedList class offeres a few additional methods for directly manipulating the ends of the list:– void addFirst(Object)– void addLast(Object);– Object getFirst();– Object getLast();– Object removeFirst();– Object removeLast();

These methods make it natural to implement other simpler data structures, like Stacks and Queues.

Page 22: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

LinkedList examples See heavily commented LinkedList Example in course

notes A few things to be aware of:

– it is really bad to use the positional indexing features copiously of LinkedList if you care at all about performance. This is because the LinkedList has no memory and must always traverse the chain from the beginning.

– Elements can be changed both with the List and ListIterator objects. That latter is often more convenient.

– You can create havoc by creating several iterators that you use to mutate the List. There is some protection built-in, but best is to have only one iterator that will actually mutate the list structure.

Page 23: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

ArrayList Class Also supports the List interface, so top-level code

can pretty much invisibly use this class or LinkedList (minus a few additional operations in LinkedList).

However, ArrayList is much better for using positional index access methods.

At the same time, ArrayList is much worse at inserting elements.

This behavior follows from how ArrayLists are structured: they are just like Vectors.

Page 24: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

More on ArrayList Additional methods for managing size of

underlying array size, isEmpty, get, set, iterator, and listIterator

methods all run in constant time. Adding n elements take O[n] time. Can explicitly grow capacity in anticipation of

adding many elements. Note: legacy Vector class almost identical. Main

differences are naming and synchronization. See short ArrayList example.

Page 25: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Vector class

Like an ArrayList, but synchronized for multithreaded programming.

Mainly for backwards-compatibility with old java.

Used also as base class for Stack implementation.

Page 26: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Stack class Stack()

          Creates an empty Stack.  Method boolean empty()

Tests if this stack is empty.  E peek()

Looks at the object at the top of this stack without removing it from the stack. 

E pop()           Removes the object at the top of this stack and returns that object as the value of this function. 

E push(E item)           Pushes an item onto the top of this stack. 

int search(Object o)           Returns the 1-based position where an object is on this stack. 

Page 27: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Set Interface Set also extends Collection, but it prohibits

duplicate items (this is what defines a Set). No new methods are introduced; specifically, none

for index-based operations (elements of Sets are not ordered).

Concrete Set implementations contain methods that forbid adding two equal Objects.

More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element

Java has two implementations: HashSet, TreeSet

Page 28: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

HashSets and hash tables

Lists allow for ordered elements, but searching them is very slow.

Can speed up search tremendously if you don’t care about ordering.

Hash tables let you do this. Drawback is that you have no control over how elements are ordered.

hashCode() computes integer (quickly) which corresponds to position in hash table.

Independent of other objects in table.

Page 29: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

HashSet Class

Hashing can be used to implement several important data structures.

Simplest of these is HashSet– add elements with add(Object) method

– contains(Object) is redefined to first look for duplicates.

– if duplicate exists, Object is not added

What determines a duplicate?– careful here, must redefine both hashCode() and

equals(Object)!

Page 30: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

HashSet

Look HashSetExample.java Play around with some additional methods. Try creating your own classes and override

hashCode method. Do Some timings.

Page 31: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Tree Sets Another concrete set implementation in Java is

TreeSet. Similar to HashSet, but one advantage:

– While elements are added with no regard for order, they are returned (via iterator) in sorted order.

– What is sorted order?• this is defined either by having class implement Comparable

interface, or passing a Comparator object to the TreeSet Constructor.

• Latter is more flexible: doesn’t lock in specific sorting rule, for example. Collection could be sorted in one place by name, another by age, etc.

Page 32: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Comparable interface Many java classes already implement this. Try

String, Character, Integer, etc. Your own classes will have to do this explicitly:

– Comparable defines the method public int compareTo(Object other);– Comparator defines the method public int compare(Object a, Object b);

As we discussed before, be aware of the general contracts of these interfaces.

See TreeSetExample.java

Page 33: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Maps

Maps are similar to collections but are actually represented by an entirely different class hierarchy.

Maps store objects by key/value pairs:– map.add(“1234”, “Andrew”);– ie Object Andrew is stored by Object key 1234

Keys may not be duplicated Each key may map to only one value

Page 34: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Java Map interface

Methods can be broken down into three groups:– querying– altering– obtaining different views

Fairly similar to Collection methods, but Java designers still thought best to make separate hierarchy – no simple answers here.

Page 35: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Map methods Here is a list of the Map methods:

– void clear()– boolean containsKey(Object)– boolean containsValue(Object)– Set entrySet()– boolean get(Object) – boolean isEmpty()– Set keySet()– Object put(Object, Object)– void putall(Map)– Object remove(Object)– int size()– Collection values()

Page 36: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Map Implementations We won’t go into too much detail on Maps. Java provides several common class

implementations:– HashMap

• a hashtable implementation of a map• good for quick searching where order doesn’t matter• must override hashCode and equals

– TreeMap• A tree implementation of a map• Good when natural ordering is required• Must be able to define ordering for added elements.

Page 37: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Simplified for loop in 1.5

Old way ArrayList<Integer> list = new ArrayList<Integer>();

for (Iterator i = list.iterator(); i.hasNext();) { Integer value=(Integer)i.next();}

New Way

ArrayList<Integer> list = new ArrayList<Integer>(); for (Integer i : list) { ... }

Page 38: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Autoboxing in jdk1.5

Pre 1.5 – must cast– ArrayList<Integer> list = new ArrayList<Integer>();

list.add(0, new Integer(42)); int total = (list.get(0)).intValue();

Post 1.5 – cast done by compiler– ArrayList<Integer> list = new ArrayList<Integer>();

list.add(0, 42);int total = list.get(0);

Page 39: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

JNI

Linking Java and C code

Page 40: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

JNI

Stands for “Java Native Interface” Set of tools/code that allows user to call “native”

methods from Java. Includes “bindings” for C/C++. Can be used to call C/C++ from Java (typical), or

Java from C (invocation API) Differs from spawning executable – data is passed

to/from C/C++ method Question: why is this difficult?

Page 41: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Reasons for using JNI

Feature not available in java language (rare).

Code already written in another language, don’t want to rewrite (typical).

Java is slow (how slow?) Other language has no additional features

per se, but has much better syntax for handling certain operations (Fortran for math).

Page 42: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Problems with JNI

Only provides C/C++ bindings. Going to Fortran, COBOL, Ada, etc. requires extra step.

Not portable Mapping is not trivial Can be unsafe Cannot run from applet (security issues)

Page 43: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Machinery for using JNI

Steps to follow …

Page 44: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Basic steps to calling native code

1. Write java class with at least one method declared with native keyword. Provide no implementation

– public native void sayHello();

– Example above is most simple, but method may pass any parameters or have any return type.

2. Add a call to System.loadLibrary(“libname”) in the class that declares the native method:

– static{System.loadLibrary(“hello”);}//static means called only once.

Page 45: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Steps, cont.

3. Compile the class– javac Hello.java

4. Produce the C/C++ header files using the javah utility:

– Javah Hello– This produces the header file Hello.h

5. Write your implementation file by first copying the function signature produced in the include file. Also, #include the header file.

#include “Hello.h”

Page 46: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Steps, cont.

6. Write the implementation in C/C++. This will require using JNI methods to access the data or possibly casts to convert to basic C/C++ types

7. Best technique: Break into two steps. Think of your C/C++ function as a wrapper which accesses the Java data and maps it to C data using JNI methods, then shoves the converted data into a prewritten standalone C program.

Page 47: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Steps, cont.

8. Compile your native method(s) as a shared object (or DLL on Windows). – WARNING: Be sure to point your linker to the include

files in /jdk1.3/include and jdk1.3/include/linux (for example).

– WARNING: Mixing languages is much easier using a straight C wrapper rather than C++.

9. Set the environment variable LD_LIBRARY_PATH to the shared object directory

Run main Java class.

Page 48: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

C language bindings

What does Java pass to my method?

Page 49: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

What does Java pass to my C function? JNIEnv* : A pointer to the the JNI

environment. This pointer is a handle to the current thread in the JVM, and contains mapping functions and other housekeeping information.

jobject : A reference to the object that called the native code. (like “this” pointer).

Any arguments specified by the method.

Page 50: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Pause for some nice pictures

Page 51: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level
Page 52: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Legacy C calls Java

Page 53: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Java calls legacy

Page 54: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level
Page 55: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

JNI is inter-language “glue”

Page 56: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Simple examples on union HelloWorld Example: No data passed

– Hello.java– Hello.cc

Max example : Only native dataypes– Utils.java– utils.cc

Advanced Max example: Arrays– Utils.java– utils.cc

Max Java-C-Fortran: max.f

Page 57: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

General Strategy

Keep interface as simple as

Page 58: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Native datatype mappings

Java Type Native Type Size in bits

boolean jboolean 8, unsigned

byte jbyte 8

char jchar 16, unsigned

short jshort 16

int jint 32

long jlong 64

float jfloat 32

double jdouble 64

void void N/a

Page 59: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Java object Mappings• Object passed by reference• All objects have type jobject as:

Page 60: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Object mappings, cont.

For example, if a method getLine exists in a class call Prompt, then:private native String getLine(String Prompt);

is mapped intoJNIExport jstring JNICALL Java_Prompt_getLine(JNIEnv *, jobject,

jstring);

But how to access data/methods from object that is passed in?

Page 61: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

JNI Advice

Can seem like a bewildering number of functions. Do not try to learn it all. Keep interfaces very simple.

– Preferably, only native datatypes, Strings, and arrays.

Be careful about – Copies vs. rerences

– Freeing memory

Best not to allocate memory from with native code.

Page 62: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Accessing java strings

Do NOT do the following:

JNIEXPORT jstring JNICALL Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt){

printf(“%s”, prompt);

}

Why is this bad?

Page 63: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Right way to access Strings

Must use special methods in env structurechar *str = (*env)->GetStringUTFChars(env,prompt,0);

/* this maps into regular C char* */

printf(“%s”, str); /* now it is ok to print */

(*env)->ReleaseStringUTFChars(env, prompt, str);

/* must release String to avoid memory leaks */

Page 64: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Returning Strings

Previous technique allows us to use a String passed in from Java.

What if we want to return a String? Can use NewStringUTF as:char buf[128]; /* allocate memory for local char* in C */

scanf(“%s”, buf); /* read into char* from stdio */

return( (*env)->NewStringUTF(env, buf));

/* construct and return the Java String */

Page 65: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Other JNI String methods GetStringChars

– Takes the Java String and returns a pointer to an array of Unicode characters that comprise it.

ReleaseStringChars– Releases the pointer to the array of Unicode characters

NewString– Constructs a new String object from an array of

Unicode Characters GetStringLength

– Returns the length of a string of Unicode characters

Page 66: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Java arrays

Note that you can NOT do the following:JNIExport jint JNICALL Java_IntArray_sumArray(JNIEnv

*env, jobject obj, jintArray arr){

int i, sum = 0;

for (i = 0; i<10; i++){

sum += arr[i]; /* NO! – why not?

}

... Must use java methods to access array data in C

Page 67: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Array methods

The previous example should be written as:jsize len = (*env)->GetArrayLength(env,arr);

jint *body = (*env)->GetIntArrayElements(env,arr,0);

for (i=0;i<len;++i){

sum += body[i];

}

(*env)->ReleastIntArrayElements(env,arr,body,0);

/* very important – copies back to java array if copy had to be made */

Page 68: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Array methods, cont.

Note that there are analagous functions for float, byte, double, etc:– Get<type>ArrayElements– Release<type>ArrayElements

Important: These Get functions may copy the entire array. If this is undesirable, use

Get/Set<type>ArrayRegion functions

Page 69: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Function for accessing arrays

Function Array Type

GetBooleanArrayElements boolean

GetByteArrayElements byte

GetShortArrayElements short

GetIntArrayElements int

GetLongArrayElements long

GetFloatArrayElements float

GetDoubleArrayElements double

GetObjectArrayElements object

Page 70: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Functions for releasing arraysFunction Array Type

ReleaseBooleanArrayElements boolean

ReleaseByteArrayElements byte

ReleaseShortArrayElements short

RelaseIntArrayElements int

ReleaseLongArrayElements long

ReleaseFloatArrayElements float

ReleaseDoubleArrayElements double

ReleaseObjectArrayElements object

Page 71: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Calling java methods

What if you pass a java object to a C routine and wish to “call back” a method on the Java object.

Good to avoid this when you can but sometimes it is very important.

Need to use the jobject reference that is passed in by java.

Page 72: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Steps to follow

Native method calls JNI function GetObjectClass– returns the jclass object that is type of that obj

Native method calls JNI function GetMethodID– returns jmethodID of method in class (0 for no such

method)

Finally, native method calls JNI function CallVoidMethod.– invokes an instance of method with void return type.

You pass object, methodID, and actual arguments.

Page 73: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

A simple alternative – spawning a system executable Advantages

– Infinitely simpler– Portable– Can use any native language

Disadvantages– Can only pass data to and from vi stdout– Must reload executable for each invocation

Page 74: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Legacy Collections

java.util.Vector– Still useable, but typically ArrayList is

preffered.– Only major difference is if you are using

muliple threads

java.util.HashTable– Still useable, but typically HashMap is

preferred.– Again, different if using multiple threads.

Page 75: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

JNI

JNI (Java Native Interface)

Page 76: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

JNI

Stands for “Java Native Interface” Set of tools/code that allows user to call “native”

methods from Java. Includes “bindings” for C/C++. Can be used to call C/C++ from Java (typical), or

Java from C (invocation API) Differs from spawning executable – data is passed

to/from C/C++ method Question: why is this difficult?

Page 77: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Reasons for using JNI

Feature not available in java language (rare).

Code already written in another language, don’t want to rewrite (typical).

Java is slow (how slow?) Other language has no additional features

per se, but has much better syntax for handling certain operations (Fortran for math).

Page 78: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Problems with JNI

Only provides C/C++ bindings. Going to Fortran, COBOL, Ada, etc. requires extra step.

Not portable Mapping is not trivial Can be unsafe Cannot run from applet (security issues)

Page 79: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Machinery for using JNI

Steps to follow …

Page 80: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Basic steps to calling native code

1. Write java class with at least one method declared with native keyword. Provide no implementation

– public native void sayHello();

– Example above is most simple, but method may pass any parameters or have any return type.

2. Add a call to System.loadLibrary(“libname”) in the class that declares the native method:

– static{System.loadLibrary(“hello”);}//static means called only once.

Page 81: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Steps, cont.

3. Compile the class– javac Hello.java

4. Produce the C/C++ header files using the javah utility:

– Javah Hello– This produces the header file Hello.h

5. Write your implementation file by first copying the function signature produced in the include file. Also, #include the header file.

#include “Hello.h”

Page 82: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Steps, cont.

6. Write the implementation in C/C++. This will require using JNI methods to access the data or possibly casts to convert to basic C/C++ types

7. Best technique: Break into two steps. Think of your C/C++ function as a wrapper which accesses the Java data and maps it to C data using JNI methods, then shoves the converted data into a prewritten standalone C program.

Page 83: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Steps, cont.

8. Compile your native method(s) as a shared object (or DLL on Windows). – WARNING: Be sure to point your linker to the include

files in /jdk1.3/include and jdk1.3/include/linux (for example).

– WARNING: Mixing languages is much easier using a straight C wrapper rather than C++.

9. Set the environment variable LD_LIBRARY_PATH to the shared object directory

Run main Java class.

Page 84: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

C language bindings

What does Java pass to my method?

Page 85: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

What does Java pass to my C function? JNIEnv* : A pointer to the the JNI

environment. This pointer is a handle to the current thread in the JVM, and contains mapping functions and other housekeeping information.

jobject : A reference to the object that called the native code. (like “this” pointer).

Any arguments specified by the method.

Page 86: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Pause for some nice pictures

Page 87: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level
Page 88: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Legacy C calls Java

Page 89: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Java calls legacy

Page 90: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level
Page 91: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level
Page 92: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

JNI is inter-language “glue”

Page 93: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Simple examples on union HelloWorld Example: No data passed

– Hello.java– Hello.cc

Max example : Only native dataypes– Utils.java– utils.cc

Advanced Max example: Arrays– Utils.java– utils.cc

Max Java-C-Fortran: max.f

Page 94: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

General Strategy

Keep interface as simple as

Page 95: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Native datatype mappings

Java Type Native Type Size in bits

boolean jboolean 8, unsigned

byte jbyte 8

char jchar 16, unsigned

short jshort 16

int jint 32

long jlong 64

float jfloat 32

double jdouble 64

void void N/a

Page 96: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Java object Mappings

• Object passed by reference• All objects have type jobject as:

Page 97: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Object mappings, cont.

For example, if a method getLine exists in a class call Prompt, then:private native String getLine(String Prompt);

is mapped intoJNIExport jstring JNICALL Java_Prompt_getLine(JNIEnv *, jobject,

jstring);

But how to access data/methods from object that is passed in?

Page 98: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

JNI Advice

Can seem like a bewildering number of functions. Do not try to learn it all. Keep interfaces very simple.

– Preferably, only native datatypes, Strings, and arrays.

Be careful about – Copies vs. rerences

– Freeing memory

Best not to allocate memory from with native code.

Page 99: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Accessing java strings

Do NOT do the following:

JNIEXPORT jstring JNICALL Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt){

printf(“%s”, prompt);

}

Why is this bad?

Page 100: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Right way to access Strings

Must use special methods in env structurechar *str = (*env)->GetStringUTFChars(env,prompt,0);

/* this maps into regular C char* */

printf(“%s”, str); /* now it is ok to print */

(*env)->ReleaseStringUTFChars(env, prompt, str);

/* must release String to avoid memory leaks */

Page 101: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Returning Strings

Previous technique allows us to use a String passed in from Java.

What if we want to return a String? Can use NewStringUTF as:char buf[128]; /* allocate memory for local char* in C */

scanf(“%s”, buf); /* read into char* from stdio */

return( (*env)->NewStringUTF(env, buf));

/* construct and return the Java String */

Page 102: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Other JNI String methods GetStringChars

– Takes the Java String and returns a pointer to an array of Unicode characters that comprise it.

ReleaseStringChars– Releases the pointer to the array of Unicode characters

NewString– Constructs a new String object from an array of

Unicode Characters GetStringLength

– Returns the length of a string of Unicode characters

Page 103: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Java arrays

Note that you can NOT do the following:JNIExport jint JNICALL Java_IntArray_sumArray(JNIEnv

*env, jobject obj, jintArray arr){

int i, sum = 0;

for (i = 0; i<10; i++){

sum += arr[i]; /* NO! – why not?

}

... Must use java methods to access array data in C

Page 104: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Array methods

The previous example should be written as:jsize len = (*env)->GetArrayLength(env,arr);

jint *body = (*env)->GetIntArrayElements(env,arr,0);

for (i=0;i<len;++i){

sum += body[i];

}

(*env)->ReleastIntArrayElements(env,arr,body,0);

/* very important – copies back to java array if copy had to be made */

Page 105: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Array methods, cont.

Note that there are analagous functions for float, byte, double, etc:– Get<type>ArrayElements– Release<type>ArrayElements

Important: These Get functions may copy the entire array. If this is undesirable, use

Get/Set<type>ArrayRegion functions

Page 106: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Function for accessing arrays

Function Array Type

GetBooleanArrayElements boolean

GetByteArrayElements byte

GetShortArrayElements short

GetIntArrayElements int

GetLongArrayElements long

GetFloatArrayElements float

GetDoubleArrayElements double

GetObjectArrayElements object

Page 107: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Functions for releasing arrays

Function Array Type

ReleaseBooleanArrayElements boolean

ReleaseByteArrayElements byte

ReleaseShortArrayElements short

RelaseIntArrayElements int

ReleaseLongArrayElements long

ReleaseFloatArrayElements float

ReleaseDoubleArrayElements double

ReleaseObjectArrayElements object

Page 108: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Calling java methods

What if you pass a java object to a C routine and wish to “call back” a method on the Java object.

Good to avoid this when you can but sometimes it is very important.

Need to use the jobject reference that is passed in by java.

Page 109: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Steps to follow

Native method calls JNI function GetObjectClass– returns the jclass object that is type of that obj

Native method calls JNI function GetMethodID– returns jmethodID of method in class (0 for no such

method)

Finally, native method calls JNI function CallVoidMethod.– invokes an instance of method with void return type.

You pass object, methodID, and actual arguments.

Page 110: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

A simple alternative – spawning a system executable Advantages

– Infinitely simpler– Portable– Can use any native language

Disadvantages– Can only pass data to and from vi stdout– Must reload executable for each invocation

Page 111: Lesson 7 Collections, Generics JNI. What is the Collections framework?  Collections framework provides two things: –implementations of common high-level

Spawning Executable -- technique Process p = Runtime.exec(“some_exec”); Use p to manage process:

– p.getInputStream();– p.getOutputStream();– p.kill();– p.halt();