• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling overloaded methods with null

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. When you pass the overloaded methods a hardcoded String on line 3

tc.method("Test"); // line 3

the "String Version"
will be selected.

The compiler is able to distinguish between String and StringBuffer on line 3.

Both String and StringBuffer reference variables 'sb1' and 's1' can be assigned 'null'.




If on line 4 you attempt
tc.method(null);

you will receive a compiler error.

Question:
Is this because the compiler will not know which method
to select if you attempt to call 2 overloaded methods with null?

 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:1. When you pass the overloaded methods a hardcoded String on line 3

tc.method("Test"); // line 3

the "String Version"
will be selected.

The compiler is able to distinguish between String and StringBuffer on line 3.


I hope you know that "Test" is certainly a String. It is not a StringBuffer. So the compiler has no reason for getting confused here.

Thomas Hauck wrote:
Both String and StringBuffer reference variables 'sb1' and 's1' can be assigned 'null'.




If on line 4 you attempt
tc.method(null);

you will receive a compiler error.

Question:
Is this because the compiler will not know which method
to select if you attempt to call 2 overloaded methods with null?



Right. Because a null gives no data to the compiler about its intended reference type.
So if you have two or more overloaded methods that have a reference variable as a parameter and you invoke one of these overloaded methods by passing null as an argument, you will get a compile time error.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to add a little bit more. These will all compile and it's up to you to decide what will be printed

 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great reply @Roel.

First of all I dint know null can be casted. Secondly this program can take everything as a parameter except null value. Here primitives are upgraded as object and it calls method which takes object as a parameter, when argument is primitive!!!

Thanks & regards,
Prathima
 
Sibendu Dey
Greenhorn
Posts: 20
Java Oracle Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:1. When you pass the overloaded methods a hardcoded String on line 3

tc.method("Test"); // line 3

the "String Version"
will be selected.

The compiler is able to distinguish between String and StringBuffer on line 3.

Both String and StringBuffer reference variables 'sb1' and 's1' can be assigned 'null'.

Here "Test" is a String , hence , it is quite straight forward that the compiler will call the method with String as parameter.

Thomas Hauck wrote:


If on line 4 you attempt
tc.method(null);

you will receive a compiler error.

Question:
Is this because the compiler will not know which method
to select if you attempt to call 2 overloaded methods with null?



If you call the method with null as argument , then both the methods with String and StringBuffer as arguments become eligible to handle the call.
The compiler doesn't find any "specific" method to be called.

Consider the following code



Hope it helps!!



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic