Subscribe to Java/SCJP        RSS Feed
-----

SCJP Notes - Part 7

Icon Leave Comment
1) The final keyword on a method means that the method cannot be overridden in a subclass. But since the main() method is a static method, it can't be overridden anyway. So making the main() method final makes no sense.

2) main method can be final.

3) A class without a main method can be run by JVM, if its ancestor class has a main method. (main is just a method and is inherited)

4) We can set the reference variables to null, hinting the gc to garbage collect the objects referred by the variables. Even if we do that, the object may not be gc-ed if it's attached to a listener. (Typical in case of AWT components) Remember to remove the listener first.

5) protected void finalize() throws Throwable { }
In the descendents this method can be protected or public. Descendents can restrict the exception list that can be thrown by this method.

6) finalize is not implicitly chained. A finalize method in sub-class should call finalize in super class explicitly as its last action for proper functioning. But compiler doesn't enforce this check.

7) NAN's are non ordinal and INFINITIES are ordinal.
x == Float.NaN won't work.
X == Double.POSITIVE_INFINITY will give expected result.

8) If x instanceof Y is not allowed by compiler, then Y y = (Y) x is not a valid cast expression. If x instanceof Y is allowed and returns false, the above cast is valid but throws a ClassCastException at runtime. If x instanceof Y returns true, the above cast is valid and runs fine.

9) Extended assignment operators do an implicit cast. (Useful when applied to byte, short or char)
byte b = 10;
b = b + 10; // Won't compile, explicit cast required since the expression evaluates to an int
b += 10; // OK, += does an implicit cast from int to byte



10)
	class Base
	  {
		String name;
	 
		Base()
		{
		  name = "Base class";
		} 
		
		public String getName()
		{
		  return name;
		} 
	  }
	 
	  public class Sub extends Base 
	  {
		String name;
		
		Sub()
		{
		  name="Sub class";
		} 
		
		public static void main(String args[])
		{
		  Base parent = new Base();
		  Sub   child  = new Sub();
	 
		  System.out.println("value of name in parent object "							
							  +parent.getName()+
							 " \n value of name in child object "
							  +child.getName()); 
		}
	   }


The output of the above program is:
value of name in parent object Base class
value of name in child object Base class

11) Always keep in mind that operands are evaluated from left to right, and the operations are executed in the order of precedence and associativity.
int i = 0;
i = i++;
i = i++;
i = i++;
System.out.println(i); // prints 0, since = operator has the lowest precedence.



12) Member variables marked final are not initialized by default.

13) avoid overloading a varargs method because it can be difficult to discern which overloading gets called

14) Same thread if started twice throws IllegalThreadStateException.

0 Comments On This Entry

 

September 2014

S M T W T F S
 123456
78910111213
1415161718 19 20
21222324252627
282930    

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)