Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
becki:linux:java [2010-03-15 15:46] becki |
becki:linux:java [2010-10-19 12:50] (aktuell) becki |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
====== Java == | ====== Java == | ||
- | ===== Links == | + | ===== Related Pages == |
- | [[sunkurs]] | [[comparison table]] | + | <pagelist> |
+ | * [[java plumbing]] | ||
+ | * [[java web programming]] | ||
+ | * [[apache click]] | ||
+ | * [[apache tomcat]] | ||
+ | * [[jrunscript]] | ||
+ | </pagelist> | ||
===== Java Versions and Compatibility == | ===== Java Versions and Compatibility == | ||
Zeile 79: | Zeile 85: | ||
===== Rich Internet Applications == | ===== Rich Internet Applications == | ||
+ | |||
+ | FIXME Whole secton is obsoete. See [[java plumbing]] instead! | ||
+ | |||
==== Java Web Start == | ==== Java Web Start == | ||
Zeile 114: | Zeile 123: | ||
- inherited from java.awt.Container | - inherited from java.awt.Container | ||
see also Java Tutorial Sun: Lesson: Overview of Applets | see also Java Tutorial Sun: Lesson: Overview of Applets | ||
- | |||
- | ==== Deploy both as Applet and JWS == | ||
- | |||
- | It is advisable to separate core functionality from the final deployment mechanism: | ||
- | |||
- | === Appliction specific Code == | ||
- | |||
- | <code java> | ||
- | import javax.swing.*; | ||
- | |||
- | public class MyApplication extends JFrame { | ||
- | |||
- | private void createAndShowGUI() { | ||
- | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
- | setTitle("My App Title"); | ||
- | JPanel panel= new MyContent(); /* get app-specific content */ | ||
- | getContentPane().add(panel); /* and put it into app frame */ | ||
- | pack(); | ||
- | setVisible(true); | ||
- | } | ||
- | |||
- | public static void main(String[] args) { | ||
- | javax.swing.SwingUtilities.invokeLater(new Runnable() { | ||
- | public void run() { | ||
- | MyApplication frame = new MyApplication(); | ||
- | frame.createAndShowGUI(); | ||
- | } | ||
- | }); | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | FIXME The usage of ''SwingUtilities.invokeLater'' is [[javatut>uiswing/start/compile.html|used]] by Sun as well. | ||
- | |||
- | === Applet specific Code == | ||
- | |||
- | <code java> | ||
- | import javax.swing.*; | ||
- | |||
- | public class MyApplet extends JApplet { | ||
- | public void init() { | ||
- | try { | ||
- | SwingUtilities.invokeAndWait(new Runnable() { | ||
- | public void run() { | ||
- | JPanel panel= new MyContent();/* create app-specific */ | ||
- | add(panel); /* content and put it into app frame */ | ||
- | } | ||
- | }); | ||
- | } catch (Exception e) { | ||
- | System.err.println("createGUI didn't complete successfully"); | ||
- | } | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | === Shared Code == | ||
- | |||
- | <code java> | ||
- | import javax.swing.*; | ||
- | |||
- | public class MyContent extends JPanel { | ||
- | private JLabel myLabel; | ||
- | |||
- | MyContent() { | ||
- | myLabel= new JLabel("¡Hola Mundo!"); | ||
- | add(myLabel); | ||
- | } | ||
- | } | ||
- | </code> | ||
===== Events == | ===== Events == | ||
Zeile 245: | Zeile 185: | ||
String s= ""+ 4711; | String s= ""+ 4711; | ||
String s= new Integer(4711).toString(); | String s= new Integer(4711).toString(); | ||
+ | String s= String.valueOf(4711); | ||
</code> | </code> | ||
Zeile 343: | Zeile 284: | ||
String readLine() throws IOException | String readLine() throws IOException | ||
</code> | </code> | ||
+ | |||
+ | ===== Collections == | ||
+ | ==== Overview == | ||
+ | |||
+ | [[javatut>reallybigindex.html#collections|Chapter in the Java tutorial]] | ||
+ | |||
+ | | //[[javaref>api/java/util/Collection.html|Collection]]// |||| //[[javaref>api/java/util/Map.html|Map]]// || | ||
+ | | //[[javaref>api/java/util/Set.html|Set]]// || //[[javaref>api/java/util/List.html|List]]// | //[[javaref>api/java/util/Queue.html|Queue]]// | || | ||
+ | | | //[[javaref>api/java/util/SortedSet.html|SortedSet]]// | | | | //[[javaref>api/java/util/SortedMap.html|SortedMap]]// | | ||
+ | | HashSet LinkedHashSet | TreeSet | LinkedList Vector ArrayList | LinkedList, PriorityQueue | HashTable LinkedHashMap HashMap | TreeMap | | ||
+ | |||
+ | ==== Implementations == | ||
+ | |||
+ | * [[javatut>collections/implementations/|Overview]] | ||
+ | * [[http://www.developer.com/java/article.php/3829891|Selecting the Best Java Collection Class for Your Application]] | ||
+ | * [[javatut>collections/implementations/map.html|General purpose map implementations]] | ||
+ | |||
+ | ==== Methods == | ||
+ | |||
+ | <code java> | ||
+ | // important methods of List: | ||
+ | boolean add(E e) | ||
+ | void add(int index, E element) | ||
+ | boolean contains(Object o) | ||
+ | E get(int index) | ||
+ | int indexOf(Object o) | ||
+ | Iterator<E> iterator() | ||
+ | E remove(int index) | ||
+ | boolean remove(Object o) // Removes the first occurrence | ||
+ | Object[] toArray() | ||
+ | <T> T[] toArray(T[] a) | ||
+ | |||
+ | // important methods of Map | ||
+ | boolean containsKey(Object key) | ||
+ | boolean containsValue(Object value) | ||
+ | V get(Object key) | ||
+ | Set<K> keySet() // Returns a set of the keys | ||
+ | V put(K key, V value) // there is no add! | ||
+ | V remove(Object key) | ||
+ | |||
+ | // important methods of Queue: | ||
+ | boolean offer(E o) // Inserts the specified element at the end; also add() possible | ||
+ | E peek() // Retrieves, but does not remove, | ||
+ | E poll() // Retrieves and removes from the beginning; remove() also possible | ||
+ | </code> | ||
+ | |||
+ | * Set: has no special order and no duplicates are permitted, not indexed; methods ar a subset of List (no index operations) | ||
+ | * List: ordered, duplicates are permitted, indexed | ||
+ | * Map does not implement the collection interface | ||
+ | * 'Sorted' is a subset of 'Ordered' | ||
+ | * Ordering: | ||
+ | * //Lists// are always ordered by index | ||
+ | * //Linked// collections are always ordered by insertion or last access | ||
+ | * //Tree// collections are always sorted | ||
+ | * List can made thread-save by Collections.synchronizedList() | ||
+ | * Tutorial: Vector is a legacy class; API: Vector is a normal class; Hashtable is similar; Vector also implements List | ||
+ | * HashSet & LinkedHashSet: Overwrite hashCode() so that equal objects provide the same hashcode | ||
+ | * LinkedList implements //List// and //Queue// | ||
+ | * //Queue// has Fifo (not stack) behaviour! | ||
+ | * All Interfaces have a size() method | ||
+ | * PriorityQueue sorts Elements by natural order using Comparable | ||
+ | * Elements for SortedSet/TreeSet, SortedMap/TreeMap | ||
+ | |||
+ | PriorityQueue must implement the Comparable interface! | ||
+ | * String implements interface Comparable<String> and Integer implements Comparable<Integer> -> Trying to add both Types to any untyped (old style) Tree-Container results in a ClassCastException | ||
+ | * Whereas adding only the same Type (eg Strings) works but gives a compiler warning. (I assume the type is detected at run time by reflection and the Object to compare is casted to String before compareTo() is called.) | ||
+ | |||
+ | ==== Sorting and Searching == | ||
+ | === Sorting with the Comparable Interface == | ||
+ | |||
+ | To sort a //List// (eg ArrayList) or an array use: | ||
+ | <code java> | ||
+ | static void Collections.sort(List<T> list); | ||
+ | static void Arrays.sort(allPrimitivesAndObject[] a); | ||
+ | </code> | ||
+ | The List interface has no sort method! All elements in the List or array must implement the Comparable interface: | ||
+ | |||
+ | <code java> | ||
+ | Interface Comparable<T> { | ||
+ | int compareTo(T o); | ||
+ | } | ||
+ | </code> | ||
+ | Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Keep in mind that compareTo accepts Type T (the generic of the List) wheras equals always only accepts type Object! | ||
+ | |||
+ | === Sorting with the Comparator Interface == | ||
+ | |||
+ | <code java> | ||
+ | static void Collections.sort(List<T> list, Comparator<? super T> c); | ||
+ | static void Arrays.sort(T[] a, Comparator<? super T> c); // T is an Object, no primitives! | ||
+ | |||
+ | Interface Comparator<T> { | ||
+ | int compare(T o1, T o2); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | === Searching == | ||
+ | |||
+ | <code java> | ||
+ | static int Collections.binarySearch(List<T> list, T key[, Comparator<? super T> c]); | ||
+ | static int Arrays.binarySearch(allPrimitivesAndObject[] a, allPrimitivesAndObject key); | ||
+ | static int Arrays.binarySearch(T[] a, T key, Comparator<? super T> c); | ||
+ | </code> | ||
+ | Call binarySearch() with the array or List as first param, the key as second, and, if not all items implement the Comparable interface, provide a Comparator as 3rd parameter. List or array has to be sorted before binarySearch can be called, otherwise the result is not predictable! If the element is not found, binarySearch returns -length-1 | ||
===== Threads == | ===== Threads == | ||
Zeile 552: | Zeile 596: | ||
==== Misc == | ==== Misc == | ||
+ | * Possibly useful: [[ibm>java/library/j-javadev2-7.html|Kilim]] //actors// instead of threads for concurrent programming | ||
* [[http://www.developer.com/design/article.php/3680701|Thread local static variables]] (Threads Versus The Singleton Pattern) | * [[http://www.developer.com/design/article.php/3680701|Thread local static variables]] (Threads Versus The Singleton Pattern) | ||
* Possibly useful: Simple thread control with [[http://www.developer.com/java/article.php/3713031|CountDownLatch]] | * Possibly useful: Simple thread control with [[http://www.developer.com/java/article.php/3713031|CountDownLatch]] | ||
Zeile 755: | Zeile 800: | ||
t1.state= Tribool.State.FALSE; t0.state= Tribool.State.FALSE; | t1.state= Tribool.State.FALSE; t0.state= Tribool.State.FALSE; | ||
System.out.println((t0.and(t1)).toString()); | System.out.println((t0.and(t1)).toString()); | ||
- | |||
} | } | ||
} | } | ||
</code> | </code> | ||
- | ===== Further Reading == | + | ===== Further Reading / 2do == |
+ | * Intro to the [[ibm>java/library/j-5things9.html|Java Scripting API]] with [[jrunscript]] | ||
+ | * Doc general Java strategy: Use [[Apache Pivot]] for the GUI, [[JUnit]] for testing, [[Apache Ant]] for building, JWS for distribution and [[Groovy]] for coding | ||
* [[http://www.linux-magazin.de/Heft-Abo/Ausgaben/2008/08/Reiches-Angebot|Webentwicklung mit Java]]: An Overview (german) | * [[http://www.linux-magazin.de/Heft-Abo/Ausgaben/2008/08/Reiches-Angebot|Webentwicklung mit Java]]: An Overview (german) | ||
* [[http://www.ibm.com/developerworks/opensource/library/os-lombok/|Eliminate Java verbosity with Lombok]] | * [[http://www.ibm.com/developerworks/opensource/library/os-lombok/|Eliminate Java verbosity with Lombok]] | ||
- | * REST with AJAX: | + | * REST (with AJAX): |
- | * [[http://www.ibm.com/developerworks/web/library/wa-aj-richjava/|Wink]] is a framework for building RESTful Web services | + | * Wink is a framework for building RESTful Web services |
+ | * [[ibm>web/library/wa-aj-jackson/|Wink with Jackson]], a [[http://jackson.codehaus.org/|Java JSON-processor]] | ||
+ | * [[ibm>web/library/wa-aj-richjava/|Build rich Java Web applications with Apache Wink and Ajax]] | ||
+ | * [[ibm>web/library/wa-apachewink1/|RESTful Web services with Apache Wink]] | ||
* [[http://www.developer.com/article.php/3843846|JAX-RS]]: The Java API for RESTful Web Services | * [[http://www.developer.com/article.php/3843846|JAX-RS]]: The Java API for RESTful Web Services | ||
* [[http://www.developer.com/article.php/3841046|Real World REST]] using Jersey, AJAX and JSON | * [[http://www.developer.com/article.php/3841046|Real World REST]] using Jersey, AJAX and JSON |