• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

explain the out put

 
kish kumar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Leonardo Carreira
Ranch Hand
Posts: 489
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because Number is more specific than Object...program always choses the most specific method
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i understood now . Thank you so much
 
Leonardo Carreira
Ranch Hand
Posts: 489
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kish you added [quote] tags instead of [code] tags. This is how the code looks with [code] tags

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic