Wednesday, March 17, 2010

SCJP Tips

Here you will find a list of SCJP gotcha's which will be important from the exam point of view.


1- If you get a question where a child object is passed to the parent reference means polymorphism
and if the constructor of the parent object is calling an instance method, then the version which will
be called will be of child class not the parent class.


Check this thread for the discussion of the point above. :)

2- You can call a method which takes var-args as a parameter without passing  any arguments. Strange
but true. :) 

3- Often we are confused with the use of static keyword. It is known that static methods cannot be overriden but they can be redefined. Many beginners are confused with, what is the difference between overriding and  redefining. Lets say for example we have code like this


public class Animal
{
    public static void x()
    {
        System.out.println("I am in animal class");
    }
}

public class Dog extends Animal
{
    public static void x()
    {
        System.out.println("I am in Dog class");
    }
    
    
    public static void main(String[] args)
    {
    
    Animal a = new Dog(); //1
    }



}


If we look at line 1, this time the output will be "In Animal class". As static methods are class methods, so when the compiler sees line 1, it will see method call like Animal.x() behind the scenes. It means that overriding looks at the actual object type at Run-time while redefining looks at the reference type. 


The above example was taken from a sample discussion done by Larry and can be found here

4- StackOverFlow is an error not an exception thrown by JVM.


5- Forward referencing plays an important role on the exam. A good link about forwarding referencing can be found here

6- Another excellent article written by Corey for forward referencing can be found here. I highly recommend reading this article before reading article of point 5.

7- A very good article about inner classes can be found here

8- Excellent article written by Corey for garbage collection and that's where i find myself struggling most of the time. You can find the article here 

9- An exception, be it run-time or a checked exception, it must be the last statement of block. Else the
code won't compile and will give a unreachable code statement. For example


public class Test { 
   
    void me(){ 
        throw new RuntimeException(); 
    } 
 
    public static void main(String[] args) { 
 
        try{ 
            System.out.println("start"); 
            Test t = new Test();           
            throw new RuntimeException();  // Line 1
            System.out.println("go on"); 
        }catch(Exception e){             
        } 
    } 
     
} 

If we check the code above, it won't compile because we are throwing a RuntimeException and after that giving a printout statement. In such a case, control will never come back to the try block again so giving a
statement after throwing an exception is a compile time error and makes the code unreachable.

10- A very good link about understanding the Generic tips advised by Devaka Cooray can be found here

11- As exam is approaching on 20th April, so from today, there will be a hell lot of pace building up and today onwards will be updating the section pretty regularly. Keeping my fingers crossed.

12- For good mock exams, even the site like Java Black Belt is amazing.

13- A couple of people advised me to go through the sample questions and buy some exams from Java Beat.
I find the JavaBeat kit is quiet cheap. I am going to buy it. If anyone wants a copy of it, please let me know and i will be willing to help.

14- In case of HashMap, the hashing is used for the keys only.

15- valueOf() method plays an important role over the exam as well. Lets say we have the following
snippet of code which i am reffering to one the examples in ExamLab simulator.


import java.lang.*;

enum Services{ 
Hutch(78), 
Dialog(77), 
Mobitel(71), 
Celtel(72); 
int id=0; 
Services(int n){ 
id=n; 
} 
} 

public class Telecommunications{ 
public static void main (String args[]){ 
Object ob=Services.Hutch==Services.valueOf("Hutch"); 
System.out.println(ob); 
}
}


If we look at this statement


Services.valueOf("Hutch")


This valueOf() method is not of class Enum. This is a compiler generated valueOf() method which takes the string representation of the object and returns the object equivalent to it. One more thing, whatever you are passing inside the valueOf() method should be compatible with the object. It means that if you pass a string which is not present in the enum as constant, it will compile fine but will generate a IllegalArgumentException at Runtime.

16- Always remember that if a class is declared final and you are trying to downcast the reference to that class to a interface, then you must implement that interface in the final class or any other parent class. JLS specifies that if you don't implement that interface, it will give you a compile time error. For example



class A{

    
class B extends A {


class C extends A{}    
   
final class D extends B{   

public void doMethod(){    
    A a = new B();    
    B b = new D();    
    C c=new C();    
    D d= null;    
   // If we say something like
  Runnable r = (Runnable) d ; //1
}

}




At line 1, it will give you a compile time error. Because class D is final and Runnable is an interface. This example has been taken from ExamLab simulator developed by Devaka Cooray.

16-  There is a huge difference between hiding, shadowing and obscurring. Hidings involves class inheritance where as shadowing doesn't. A classic article can be found here

Huh, i am back again with my SCJP tips, hope you people are not cursing me. Health was badly down and as well as preparation. Anyhow, lets get rolling now.

17- The most important concept or may be error which might confuse you is method overloading or which
method the compiler call. Lets say for example, lets look at this code snippet


class Q{ 
} 

class A extends Q { 
} 

class B extends A { 
}


class C extends B { 

static void m(C x, A y) { 
System.out.print("BA"); 
} 

static void m(B x, C y) { 
System.out.print("AC"); 
} 
public static void main(String[] args) { 
C c = new C(); 
m(c, c); 
}
}   





Now the question is this will this method piece of code compile? No, it will give a compile time error and will
say method m(C,A) is ambiguous for type C. Huh, whats the problem? Lets refer to JLS at this point, our
savior. 


First check this in JLS.


The sample won't compile because both of the declarations of m are accessible and applicable and neither one is more specific then the other. That's why ambiguity is caused in this sample. 


THURSDAY
This weekend, this section will be heavily updated. Keep a look out for SCJP Notes and SCJP Tips section. Hopefully i will keep my promise.


18- If anyone of you is having a confusion after reading Collections and Generics chapter from K&B about pollFirstMethod() that why we need to use it, why it is even there. Then In general, you can use it on some specific situations where you feel you should use it. For an example, suppose a scenario like this - suppose that you have a Java web based application, and you need to display something like "quotation of the hour" on the home page. You get these quotations via RSS feeds of some other external sources, and the quotation on your home page should be updated once per hour. In this case, you can maintain a collection. Your program adds new quotations to the collection as soon as it gets some new quotations via RSS feeds - so the the collection will never get empty. At the begining of each hour, another thread can get a quotation from that collection and display it on the home page. If you don't need to display the same quotation for many times, you can use the pollFirst() method to get the quotation for each hour. It simply gets a quotation from the collection and removes it - so you don't get that same quotation again. 


Additionally, pollFirst() method could be typically used if you want to use a stack data structure to solve (mathematical) problems. A great example where pollFirst() could be used is to convert infix expressions into postfix expressions and also to evaluate postfix expressions by pushing and popping operators and operands into and from a stack (this is how the compiler does it). Here you always want to pollFirst() your top two operands on the stack, so that you can apply the operator to evaluate the value. 

To learn more about postfix expressions or to look at an example search "postfix expression evaluation using stack" in Google. It is a typical thing you will study if you take the "Data Structures" course in Computer Science. 




19- An important gotcha to remember about Arrays. For example,


Object[] = new String[5][5];  //Will this Compile?
String[] = new String[5][5]; //Will this compile?



At first glance, you will think the first line will give a compiler error. No it will compile just fine, because
array super-class is always Object, so no matter if it's one-dimensional array or even three dimensional,
the first statement will always compile fine. Yes second statement will give an error. Interesting finding.


20-


Here you will find a list of SCJP gotcha's which will be important from the exam point of view.

1- If you get a question where a child object is passed to the parent reference means polymorphism
and if the constructor of the parent object is calling an instance method, then the version which will
be called will be of child class not the parent class.

Check this thread for the discussion of the point above. :)

2- You can call a method which takes var-args as a parameter without passing  any arguments. Strange
but true. :) 

3- Often we are confused with the use of static keyword. It is known that static methods cannot be overriden but they can be redefined. Many beginners are confused with, what is the difference between overriding and  redefining. Lets say for example we have code like this

public class Animal
{
    public static void x()
    {
        System.out.println("I am in animal class");
    }
}

public class Dog extends Animal
{
    public static void x()
    {
        System.out.println("I am in Dog class");
    }
    
    
    public static void main(String[] args)
    {
    
    Animal a = new Dog(); //1
    }



}

If we look at line 1, this time the output will be "In Animal class". As static methods are class methods, so when the compiler sees line 1, it will see method call like Animal.x() behind the scenes. It means that overriding looks at the actual object type at Run-time while redefining looks at the reference type. 

The above example was taken from a sample discussion done by Larry and can be found here

4- StackOverFlow is an error not an exception thrown by JVM. 

5- Forward referencing plays an important role on the exam. A good link about forwarding referencing can be found here

6- Another excellent article written by Corey for forward referencing can be found here. I highly recommend reading this article before reading article of point 5.

7- A very good article about inner classes can be found here

8- Excellent article written by Corey for garbage collection and that's where i find myself struggling most of the time. You can find the article here 

9- An exception, be it run-time or a checked exception, it must be the last statement of block. Else the
code won't compile and will give a unreachable code statement. For example


public class Test { 
   
    void me(){ 
        throw new RuntimeException(); 
    } 
 
    public static void main(String[] args) { 
 
        try{ 
            System.out.println("start"); 
            Test t = new Test();           
            throw new RuntimeException();  // Line 1
            System.out.println("go on"); 
        }catch(Exception e){             
        } 
    } 
     
} 

If we check the code above, it won't compile because we are throwing a RuntimeException and after that giving a printout statement. In such a case, control will never come back to the try block again so giving a
statement after throwing an exception is a compile time error and makes the code unreachable.

10- A very good link about understanding the Generic tips advised by Devaka Cooray can be found here

11- As exam is approaching on 20th April, so from today, there will be a hell lot of pace building up and today onwards will be updating the section pretty regularly. Keeping my fingers crossed.

12- For good mock exams, even the site like Java Black Belt is amazing.

13- A couple of people advised me to go through the sample questions and buy some exams from Java Beat.
I find the JavaBeat kit is quiet cheap. I am going to buy it. If anyone wants a copy of it, please let me know and i will be willing to help.

14- In case of HashMap, the hashing is used for the keys only.

15- valueOf() method plays an important role over the exam as well. Lets say we have the following
snippet of code which i am reffering to one the examples in ExamLab simulator.


import java.lang.*;

enum Services{ 
Hutch(78), 
Dialog(77), 
Mobitel(71), 
Celtel(72); 
int id=0; 
Services(int n){ 
id=n; 
} 
} 

public class Telecommunications{ 
public static void main (String args[]){ 
Object ob=Services.Hutch==Services.valueOf("Hutch"); 
System.out.println(ob); 
}
}


If we look at this statement


Services.valueOf("Hutch")


This valueOf() method is not of class Enum. This is a compiler generated valueOf() method which takes the string representation of the object and returns the object equivalent to it. One more thing, whatever you are passing inside the valueOf() method should be compatible with the object. It means that if you pass a string which is not present in the enum as constant, it will compile fine but will generate a IllegalArgumentException at Runtime.

16- Always remember that if a class is declared final and you are trying to downcast the reference to that class to a interface, then you must implement that interface in the final class or any other parent class. JLS specifies that if you don't implement that interface, it will give you a compile time error. For example



class A{

    
class B extends A {


class C extends A{}    
   
final class D extends B{   

public void doMethod(){    
    A a = new B();    
    B b = new D();    
    C c=new C();    
    D d= null;    
   // If we say something like
  Runnable r = (Runnable) d ; //1
}

}




At line 1, it will give you a compile time error. Because class D is final and Runnable is an interface. This example has been taken from ExamLab simulator developed by Devaka Cooray.

16-  There is a huge difference between hiding, shadowing and obscurring. Hidings involves class inheritance where as shadowing doesn't. A classic article can be found here

Huh, i am back again with my SCJP tips, hope you people are not cursing me. Health was badly down and as well as preparation. Anyhow, lets get rolling now.

17- The most important concept or may be error which might confuse you is method overloading or which
method the compiler call. Lets say for example, lets look at this code snippet


class Q{ 
} 

class A extends Q { 
} 

class B extends A { 
}


class C extends B { 

static void m(C x, A y) { 
System.out.print("BA"); 
} 

static void m(B x, C y) { 
System.out.print("AC"); 
} 
public static void main(String[] args) { 
C c = new C(); 
m(c, c); 
}
}   




Now the question is this will this method piece of code compile? No, it will give a compile time error and will
say method m(C,A) is ambiguous for type C. Huh, whats the problem? Lets refer to JLS at this point, our
savior. 

First check this in JLS.

The sample won't compile because both of the declarations of m are accessible and applicable and neither one is more specific then the other. That's why ambiguity is caused in this sample. 

THURSDAY
This weekend, this section will be heavily updated. Keep a look out for SCJP Notes and SCJP Tips section. Hopefully i will keep my promise.

18- If anyone of you is having a confusion after reading Collections and Generics chapter from K&B about pollFirstMethod() that why we need to use it, why it is even there. Then In general, you can use it on some specific situations where you feel you should use it. For an example, suppose a scenario like this - suppose that you have a Java web based application, and you need to display something like "quotation of the hour" on the home page. You get these quotations via RSS feeds of some other external sources, and the quotation on your home page should be updated once per hour. In this case, you can maintain a collection. Your program adds new quotations to the collection as soon as it gets some new quotations via RSS feeds - so the the collection will never get empty. At the begining of each hour, another thread can get a quotation from that collection and display it on the home page. If you don't need to display the same quotation for many times, you can use the pollFirst() method to get the quotation for each hour. It simply gets a quotation from the collection and removes it - so you don't get that same quotation again. 

Additionally, pollFirst() method could be typically used if you want to use a stack data structure to solve (mathematical) problems. A great example where pollFirst() could be used is to convert infix expressions into postfix expressions and also to evaluate postfix expressions by pushing and popping operators and operands into and from a stack (this is how the compiler does it). Here you always want to pollFirst() your top two operands on the stack, so that you can apply the operator to evaluate the value. 

To learn more about postfix expressions or to look at an example search "postfix expression evaluation using stack" in Google. It is a typical thing you will study if you take the "Data Structures" course in Computer Science. 


19- An important gotcha to remember about Arrays. For example,
Object[] = new String[5][5];  //Will this Compile?
String[] = new String[5][5]; //Will this compile?
At first glance, you will think the first line will give a compiler error. No it will compile just fine, because
array super-class is always Object, so no matter if it's one-dimensional array or even three dimensional,
the first statement will always compile fine. Yes second statement will give an error. Interesting finding.

20. I have come across a very interesting question today. First of all always remember that static methods are never inherited so they can't be overridden. Now when we talk about sub-classes, in such cases, static methods are redefined. For redefinition, there are three rules which needs to be remembered

   a- They must have the same name.
   b- The method signature must be same
   c- If the redefined method in the sub-class throws a checked exception which is not thrown by parent class, the         it will result in compile time exception. Though the method in subclass can throw a RuntimeException. By throwing the checked exception, the parent class method cannot be properly hidden so compiler will throw the exception.

For an interesting discussion which happened at javaranch please check this
You all will find it very interesting.

21- Painful unreachable statement gotcha. I didn't know it either, so just liking it here, so that we can remember. Unreachable code exception can occur in different scenarios. One of the common scjp scenario is when exception is thrown and after that some variable or some code is given. Lets say for example

try{
            if(true)
            throw new Exception();
            int var = 3 ;
            System.out.println("" + var);
        }
        catch(Exception e){
             ;
        } 

Gosh it should give us unreachable code error as exception is thrown and after that a variable is declared. After exception is thrown, code will never come back in this try block again. If we change the if statement to while(true), it gives a unreachable code error. Well that is kind of confusion. Remember for JLS if(true) and if(false) are exceptional statements, but still i am unsure about the reason. I will update it as i get some reason into my head.

Okay quoting directly from JLS here right now.
As an example, the following statement results in a compile-time error:

while (false) { x=3; }
because the statement x=3; is not reachable; but the superficially similar case:
if (false) { x=3; }

does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as “unreachable” in the technical sense specified here. The rationale for this differing treatment is to allow programmers to define “flag variables” such as:

static final boolean DEBUG = false;
and then write code such as:

if (DEBUG) { x=3; }

The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.

I am having a very interesting discussion today and you check it here
The real reason Henry Wong is quoted as "Anyway, the reason is for conditional compilation. Since, Java doesn't have a preprocessor, and having the ability to turn on debugging code via a compilation constant is common, this was needed." 

Lets discuss if further and i will update it.

Monday - 10th May 2010
okay guys coming back for the preparation, after a long time. Almost a 10 days holiday.

Okay remember this tip onwards

22- Keyword 'this' cannot used inside a static context means inside a static method, else it will be a compile time error.


23- A very good exhaustive list of SCJP Tips can be found in javaranch thread. You can find it ScjpTips

24- ArthimeticException is a RunTimeException. So always remember that if a method is expected to throw an ArthimeticException but in the callee, we have caught the exceptions in the order that first RuntimeException is caught and then Arthiemetic exception, the program will never compile.

25- DivideByZeroException is a checked exception. Anything which will be divided by Zero will result in infinity.

26- When you have two classes and the parent class constructor is throwing a checked exception and the unchecked exception then the child class constructor must specify that it throws those exception. Else the program won't compile. For example
class Parent{
  public Parent() throws IOException, RuntimeException{
}
}

class Child{
public Child() throws IOException, RuntimeException{
}
}

The above amended code snippet works fine now. Do remember that this restriction just holds true for checked exceptions. If the constructor was throwing only RuntimeException, the program would have compiled fine if you had not given any constructor in child-class. Remember that RuntimeException's are not checked exceptions.
Riv

5 comments:

  1. Can i too, have your scjp notes, sir ?? Please .... ;)

    ReplyDelete
  2. :)
    No you can't have my scjp notes dear. Buy them..lol

    ReplyDelete
  3. Nice job Prithvi. This inspired me to maintain a log of tips for Scjp. It would be like you can post a list of mock exams you found useful. Can you please send me a copy of java beat dumps if you got them to nurjahan1599@yahoo.com?
    Could you also please forward any other dumps you have. Thanks

    ReplyDelete
  4. Dear Nurjahan,

    Thanks for liking the post and getting inspired. It would be a good attempt if you do the same, as it will help people. Well regarding Javabeat and all, these are all commercial softwares and to re-distribute such material is against the law as they are paid. For some free sites, you can look at javachamps, its a pretty good site.

    Hope this helps,

    ReplyDelete
  5. U r Doing a Great Job
    Keep Going............. -_-

    ReplyDelete