aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes explain the out put Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "explain the out put" Watch "explain the out put" New topic
Author

explain the out put

kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54
I have code below
public class NullTest {

public void test(Object o){

System.out.println("Object:"+o);

}

public void test(Double o){

System.out.println("Double:"+o);

}

public static void main(String[] args) {

new NullTest().test(null);

}

}


It returns Double : null ,why?

another one is


public class NullTest {

public void test(Double o){

System.out.println("Double:"+o);

}

public void test(Integer o){

System.out.println("Integer :"+o);

}

public static void main(String[] args) {

new NullTest().test(null);

}

}


it throws compile time error, why?

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Kish please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489



in that code, the compiler tells that :

Reference to test is ambiguous, both method test(java.lang.Integer) and method test(java.lang.Double)


Both class java.lang.Integer and java.lang.Double entends java.lang.Number directly...
so.., both methods refer to same superclass (java.lang.Number), that's why compiler tells ambiguous in that methods...



There's no error in this code because neither test method refer to same superclass..


It returns Double : null ,why?


because null boxed to Number...

please correct me if i'm wrong..
Thanks


Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
because Number is more specific than Object...program always choses the most specific method


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
Hi leonardo, I have a doubt

Both Integer and Double extend java.lang.Number .
So the code provides an error.

If that the case then Double is a subclass of object right. then it must also provide the same error right. (I know it sounds silly but cannot resist)


To err is human,
To forgive is not company policy
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Double is subclass of Object so as Raju said Double is more specific than Object and will be chosen but Double and Integer both extends Number they dont have any superclass-subclass relation, Infact they both are same level classes so there is ambiguity. If you had methods with Number and Double or Integer and Double then there would be no ambiguity.

I hope I am clear.


SCJP 1.6 96%
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
If you had methods with Number and Double or Integer and Double then there would be no ambiguity.


Neha is it Number and Double or Number and Integer...........
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
yes i understood now . Thank you so much
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

PrasannaKumar Sathiyanantham wrote:
If that the case then Double is a subclass of object right. then it must also provide the same error right. (I know it sounds silly but cannot resist)


Yes, it must also provide the same error...


The compiler warnings us that there's code that refer to method ambiguously .. (i.e test2)..
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Raju Champaklal wrote:
If you had methods with Number and Double or Integer and Double then there would be no ambiguity.


Neha is it Number and Double or Number and Integer...........

ya sorry !! you are right its Number and Integer

So, how is it going? only one day left.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Kish you added [quote] tags instead of [code] tags. This is how the code looks with [code] tags

 
Consider Paul's rocket mass heater.
 
subject: explain the out put