You are on page 1of 6

Abstract classes define all methods but do not implement them in a body.

All concrete subclasses must fully implement all abstract methods. Different between interface and abstract classes. Interface cannot implement methods while abstract classes can. Abstract classes can only be extended once while interface can be implemented multiple times. Abstract classes can only between on is a relationship between classes while interfaces can be implemented to classes that are not related to each other. Polymporphism IS-A test class extend another Encapsulation is hiding class members from each other. Cohesion where there should few as possible function in a class. Instance variables and objects live one the heap while local variables live on the stack. Short circuit operators is where the left side condition is false, there is not evaluation on the right condition Enhanced for loop in Java 5 Swithes fall through until first break or default. Exception matching must start at most detailed (most derived sub classed) and in order of predecessor of parent class. You also need to declare or catch exception. If method or class ducks to throw exception, parent class must be able to catch the child class exception if it is declared to throw exceptions. Runtime exceptions (i.e. out of bounds for array, EOF, Null Pointer, etc)> TO create exception: class MyException extends Exception {} Exceptions can be run time or programmatically like Illegal Argument, NumberFormart, Assertion Error, or Class Cast Assert(x==1): aReturn(); if it returns false, it will thrown an AssertionError. To enable assertions, use java ea or jave da to diable. Only use assertions against private methods, not public due they could do strange things as you have no control of the code that is calling it. Strings are immutable. Use StringBuffer over StringBuilder since StringBuilder is not thread safe. During serialization, transient variables are skipped over. During de-serialization, constructors are not run so instance variables are not assign original values. Regex: \d digit, \s whitespace, \w word like letter, digit, or underscore

. (dot) is metacharacter ??-greedy zero or more *? Is relunctant for zero or more +? Is reluctant one or more Inner class can generate MyClass$MyInner.class file. Need to call using: MyOuter mo = new MyOuter(); MyOuter.MyInner inner= new mo.MyInner(); Inner class to access objects in outer class need to be defined as final. To properly run a thread do: Class FooRunnable implements Runnable { Public void run() { } Public class TestThread { FooRunnable r = new FooRunnable(); Thread t = new Thread(r); t.start(); } Thread states include New, Runnable, Running, Waiting/Blocking/Sleeping/ or Dead. Use t.sleep(), t.yield(), or t.setPriority(),t.join(). Synchronize certain methods to prevent race conditions but could deadlock. You could synchronize blocks of code with synchronize(this). Deadlock is when two threads are blocked but waiting for each other to release the others lock. Wait() will tell thread to object to wait while notify() will release while notifyAll() will release all threads waiting. Static synchronized methods to do block regular synchronized methods. Set java class path with cp switch. Equals() example: Public Boolean equals(Object o) { If(o instanceofMoof) && ((Moof)o).getMoofValue()==this.moofValue) return true;

else return false; equals() contract must be reflexive, symmetric, transitive (between values of x,y,z), and consistent (all x.eqals(y) must equal false) Class with hashCode: Class HasHash{ Public int x=; HasHash(int xValue) {x=xVal;} Public int hashCode() { return (x*17) }} //must generate something unique Public Boolean equals(Object o) { ..similar as above..} Collections include Lists, Sets (unique things) , Maps (with unique Id), and Queues (ordered by how they are processed) collection() with lowercase c are data structures which are stored and iterated over Collection with Capital C are interface set from java.util.Collection which List, Set, and Queue. You cannot implement Collection directly. Collections with Capital C and plural are the collection of static methods which the collection uses. Ordered could be ordered by hash code. Sorted by alphanumeric. ArrayList is growable, vector is synchronized mamking it slower, linked list roder by index position. HashSet undordered, unordered but uses hashCode. HashMap unorsorted, unordered but uses hashCode. One null is allowed with multiple null values. Hashtable are synchronized and have multiple has values. TreeMap as well. PriorityQueues is last. For comparable, do: Class DVDInfo implements Comporable<DVDInfo> { Public int CompareTO(DVDInfo d} return title.compareTo(d.getTitle()); }

Class DVDInfo implemens Compable { Public int compareTo(Object o) { DVDInfo d =(DVDInfo)o; Return title.compareTo(d.getTitle()); } } Comparable: Class GenreSort implements Comparator<DVDInfo> { Public int compare(DVDInfo a, DVDINfo b) { return a.getGenre.compareTo(b.getGenre())); } } Iterator through List: Iterator<Dog> i=d.iterator(); //make iterator While(i.hasNext()) {Dog d=i.next(); } For Map: use: Map<Object,Object> m = HashMap<Object,Object>(); m.put( k ,new Dog( fido )) For PriorityQueue, do: PriorirityQueue<Integer> pq=new PriorirtyQueue<Integer>(); Pq.offer(1); also use size, peek, poll methods Generics: List<?> -anything List<? Extends Animla> OR List<? Extends Dog> or List<? Super Dog> is fine For generic class: Public class RentalGeneric<T> {

Private List<T> rental; Public T getRental() { } } <!--[if !supportLineBreakNewLine]--> <!--[endif]-->

1.

top level classes can only be "default", public, final, abstract

2. wait() and notify*() only in synchronized code 3. private constructor can not be called from constructor of subclass. 4. there is automatic narrowing conversion of literals and final variables for byte, short, char, int (if fits to new range) 5. array creation: given size, or initializer, not both! 6. non-public main() compiles, but doesn't run. 7. implicit method modifiers in interfaces: public abstract 8. implicit field modifiers in interfaces: public static final 9. if the subclass overrides a method, then the parent constructor calls the overriding one. 10. member variables are resolved at compile time 11. (int)-2.9 == -2 12. there can be method with the same name as the constructor. It even can return void. 13. "byte" += "int" compiles and works (possibly loose prec.) 14. Arrays of generic type: only unbounded wildcards. 15. private is private to the class, not to the object 16. final field must be initialized in initializer, constructor, or at the place of declaration 17. static final field must be initialized in static initializer or at the palce of declaration 18. static fields/methods/classes can be declared only in static or top-level classes. 19. Set, List and Map strenghten the requirements on equals and hashCode contracts 20. Externalizable class must have no-args constructor 21. default serialization writes non-static and non-transient fields.

22. It is not possible to convert or cast arrays of primitives.

You might also like