



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
General notes and best practices for java programming, including recommendations to use an ide, download the java sdk, and avoid common errors such as pass by reference mistakes. It also introduces the concepts of comparators and interfaces in java.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




http://java.sun.com/j2se/1.4/download.html
http://java.sun.com/j2se/1.4/docs/api/index.html
Pass by Reference... but not really
void foo(String t) { t = new String("World"); }
String s = new String("Hello"); foo(s);
System.out.print(s); //prints "Hello". Why didn't it change?
void foo(String *t) { t = new String("World"); }
String *s = new String("Hello"); foo(s);
cout<<*s<<endl; //prints "Hello". Hopefully obvious why
sorter.add("cat"); sorter.add("dog");
Iterator i = sorter.iterator(); while(i.hasNext()) System.out.print(i.next()+" ");//prints "cat dog hello world"
class MyString { String s; MyString(String s){this.s=s;} public int compareTo(Object other){return -1*(s.compareTo(other));} }
class ReverseCompare implements Comparator { public int compare(Object a, Object b) { return(-1*((Comparable)a).compareTo(b)); } }
SortedSet sorter = new TreeSet(new ReverseCompare()); sorter.add("hello"); sorter.add("world"); sorter.add("cat"); sorter.add("dog");
Iterator i = sorter.iterator(); while(i.hasNext()) System.out.print(i.next()+" ");//prints "world hello god cat "
Interfaces
List list; ArrayList arrayList = new ArrayList(); LinkedList linkedList = new LinkedList();
list = arrayList; // compiler casts these for us automatically list = linkedList; // because both implement the List interface
public static void main(String[] args) { List masterDictionary = new LinkedList();
// ... now 400,000 lines of code that does something fun and profitable }