Thursday, November 09, 2006
Some SCJP tips
Some SCJP oriented Java tips -
1) U can never make a non-static reference from a static reference. So check for non static calls from main()
2) You can have a method name with the same name as that of the class and returns a value. Its perfectly acceptable. Its just that they would obviously not be the constructors. Take care for such methods
3) widening conversions are always acceptable. narrowing are never acceptable, hence, all narrowing conversions require an explicit cast. Short hand assignment operators make this by default.
e.g.
int i = 10;
short s = i; //compiletime error
int k = 10;
short s;
s+= k ; //acceptable because this short hand conversion actually is s = (byte) (s+i)
short s = 10; //perfectly legal as integer literals withing byte range can be directly assigned
4)Garbage collection can never ever be forced . You can request it thru’ System.gc() or Runtime.getRuntime.gc().
5) The garbage collector is a low priority daemon thread that’s platform dependent. In fact Threads & Garbage collection are the only 2 things in Java that are platform dependent.
6) For method Overloading you need to consider only the method name and parameter types. For over-riding you need to consider method name, parameters, return type and exception list.
e.g.
class Q7
{
public int hello(){return 4;}
}
public class Question7 extends Q7
{
public long hello(){return 5;}
//compile time error for above ‘coz return type diff
public long hello(String s){return 5;}
//Perfectly legal ‘coz this becomes Overloading and not over riding
}
7) Throwing a Runtime Exception and/or Error is equivalent to not throwing an exception because RuntimeException and Error (and their sub classes) are unchecked exceptions.
8) legal identifiers in Java are of the form [$/[A-Z]/[a-z]/_]{ $/[A-Z]/[a-z]/_/[0-9]}*
9) String String = “String” //legal
You could always use a class name, method name as legal identifiers.
I never knew the compiler was so smart ;)
10) switch(a) ;// a is byte/short/char/int and nothing else
11)The bitwise operators & and | are not short circuited ever. so the two operands are always evaluated as against && and || where the second operand may/not be evaluated
12)For positive value >> and >>> yields the same result
U better learn the shift operators in and out. There’s much more to
it which being an application developer I missed.
That’s it folks. I could go on and on if I keep writing but these were the first few and important ones that I learnt myself and hence thought of sharing. I promise to cover things section wise in one of my soon to arrive blogs ;)
One of my friends too has shared some handy tips which you would like to go thru’. Click here
It feels good when you work hard and ur efforts pay off but it feels better when you realize you have gained more knowledge because you just were eager to learn more(this time no university bothered me with deadlines for my exam).....Cheers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment