• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Calling overloaded methods with null

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 22
Oracle Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!



 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic