Showing posts with label SCJP revision. Show all posts
Showing posts with label SCJP revision. Show all posts

SCJP - Last Minute Revision Notes

1. Check for any obvious complier errors first like two else statements , accessing ofprivate variable/method , accessing a variable declared in a for loop outside the for loop.

2. If you try to unbox a null Integer value to int , it will result in a NullPointerException.

3. If method having the same name as Class name and having a return type is not a constructor but a ordinary method.

4. Map is the only interface that does not extend Collection. List , Set and Queue extends the Collection interface.

Collection is a interface ,
Collections is Class which extends Object class.

Map: key , value pair
Hashtable , HashMap , LinkedHashMap , TreeMap

Set : unique values
HashSet , TreeSet , LinkedHashSet

List : List of values allows duplicates, accessed by index
ArrayList , Vector , LinkedList

Queue : stack or queue. to-do list. priority queue is sorted based on the natural order or based on the object of Comparator or Comparable.LinkedList , PriorityQueue
The twins clases.

a. The Hashtable and HashMap: Both are atmost same except that Hashtable methods are synchronized and it does not allow null key/value, whereas HashMap allows one null key and many null values and its methods are not synchronized . The Hashtable is oldest class in java. The t in Hashtable is indeed a small letter rather than capital letter.

b. The Vector and ArrayList have same functionalities except that Vector Methods are synchronized , while ArrayList methods are not. Both ArrayList and Vector implementes RandomAccess interface (which is marker interface having no methods) as of java 5

c. The LinkedList can be used to access values in insertion order. The LinkedHashMap can be used access values in insertion or access order. The LinkedList implements Queue(as of java 5) and List. The LinkedList now implements queue methods like offer, poll and peek.

d. The TreeSet , TreeMap and PriorityQueue sorts the values.

e. The HashSet , Hashtable and HashMap are unordered and unsorted. if you add n values in these Collection objects , iteration will not give you a particular ordering ie no insertion or access or natural order. It iterates in a random order.

f. The LinkedHashSet is ordered form of HashSet , The LinkedHashMap is the ordered form of HashMap.

g. the offer() method adds a element in queue. the poll method returns the first method and removes it from queue. the peek just returns the first method from queue. The higgest priority is stored first in PriorityQueue. if you poll each value from PriorityQueue then values returned will be of Higgest proririty to lowest priorirty.

h. The Object of class which need to be added in HashSet , Hashtable , LinkedHashSet or HashMapt must override the below methods
The exception are all Wrapper classes. The class Number , StringBuffer and StringBuilder.

For example object of Student class need to be added in LinkedHashSetA student class contains id , name. As id is a primaray key , we should not have rwo students with the same id. if the Student class does not override the above methods then

Student d = new Student(44); //44 is id
Student d 1= new Student(44); //44 is id
public int hashCode(){}
public boolean equals(Object o){}
public boolean equals(Object o { if (o instanceof Student) {
Student t= (Student) o;
if(o.getId() ==this.id ) {
return true;
} //if
} //ifreturn false;
} //method
public int hashCode() {
//use any of the below
return id*4;
return id;
return 1038; //any constant
//do no use any of the below
Random r =new Random(44);
return r.nextInt();
//here t is transient variable
return t*id;
}
if above method is implemented then d and d1 will be same object.
both d and d1 are different object accoring to the LinkedHashSet , as a result both will be added to the Set

i. The Object of class which need to be added in TreeMap , TreeSet and PriorityQueue must implement Comparable or Comparator interface

j. java.lang.Comparabl e declaresompareTo( ) and accepts one Object as argument.k. java.util.Comparato r declares compare() which accepts 2 Object as argument.

l. in generics if you declare a variable it must be instantiated with the same type or no type at all

//valid

Listlisto1 =new ArrayList ();
List listo2 =new ArrayList();
List listo3 =new ArrayList ();
List list3 =new ArrayList();
m1(list01);/ /error
m2(list01);/ /ok
m1(list02);/ /error
m2(list02);/ /ok
m1(list03);/ /ok
m2(list03);/ /ok
m1(list3);// ok
m2(list3);// error

//invalid

List listo =new ArrayList(); //compiler error

// functions
static void m1( a) {}
static void m2( a) {}

//The argument that Child class can be applied to a parent class i s not at all applicable to generics . if you specified a type in the variable declaration it must be followed while instantiating also.

In the Exam , there will be drag-drop quetion . in which there will be 4 variable instanciated with the genreic and passed as arguemnt to a function. you have drag and drop , if the code compiles or not.

m.
public class {} and
public class {}
are invalid syntax and leads to compiler error

but it can be defined as

public class {} or
public class {}
public class {} public class {}
public T getObject() {} //correct
public T computeArrays( Y y) {}
//correct, the font-size of s is increased to differentiate from the interface Collection
//here Y is the argument and T is the return type.

5. For garbage collection , you must focus on the value and its reference . there may be a question . which is earliest line in which the value referred by object in line 5(some line) is eligible for garbage collection . The reference variable will be given other values in the consecutive steps but focus if the value which was created/defined in line 5 (example) is referenced by any other variable. The variable can be instance or local variable. if it is a local variable , its scope will be till the method ends but except in the situation the value is returned.
For example below :

Object o= new Object//line 5
Object s =o;//line 6
// the value created in line 5 is referred by s

case 1 .

o = new Object(); //7 //o is set to different value
s=null; //8 s is null or
s = new Object(); // s is set to a new value
}
so old value has no reference.

here the value created in line 5 will be eligible for gc after line 5

case 2.

o= new Object();//5
Object s =o;//6
o = new Object();//7
return s;//8 }
The value created in line 5 will not be eligible for garbage collection even after the method has ended as it is returned.

case 3:

private Object o;
public void data() {
Object o = new Object();//1
doSomething( o);//2
o = new Object();//3
o=null;//4
doSomething( null);//5
Object s = new Object();//6
Object nothing = new Object();//7
return s;//8
}//9

In the above example value created in line 1 is eligible for garbage collection after the line 5. The value created in the line 3 is eligible for garbage collection after the line 4.

The value created in line 6 will not be eligible for garbage collection even after the method ends as the value is returned. The value created in line 7 is eligible for garbage collection after the line 9.

public void doSomething( Object obj) {
o= obj;
}