Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

null reference to string objects

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code is passing null to the overloaded method. I did not get that why the method with String args is called?

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First a correction: these PowerSupply() things are not methods but constructors.

Ok, in main we do new PowerSupply(null) so one of PowerSupply(String) or PowerSupply(Object) will be used to construct the instance. The Java Language Specification specifies that the most specific one will be used. String is derived from Object, so it is the most specific of the two. There is no other possibility so PowerSupply(String) is used.

Something to try: add the following constructor and compile again.


Do you understand why the compilation fails?
[ June 10, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made the following related observations based on various mock tests.

Let us assume methods are overloaded- say we have methods method(char), method(int), method(float), method(double), method(Object), method(String), method(Integer).
If an anonymous primitive / object is passed the method invoked will be based on the anonymous primitive / object type. If the primitive / object was not anonymous the method invoked would be based on the primitive�s variable type or object�s reference type.
If the argument is an anonymous null and there are multiple overloaded methods that take reference arguments the compiler will complain about the ambiguity since there are multiple overloaded methods that take reference arguments but there are two inheritence heirachies String->Object & Integer->Object.
If however the methods were method(char), method(int), method(float), method(double), method(Object), method(String) and the argument is an anonymous null then since there are multiple overloaded methods that take reference arguments but there is only one inheritence heirachy it will take the highest type here -String.

If there is no corresponding methods an attempt for widening conversion or upcasting will be attempted as with polymorphism. While doing the widening or upcasting when the argument is a primitive the nearest (narrowest -thus if argument is a long and methods are method(char), method(int), method(float), method(double), method(Object), method(String) then method(float) will be used) primitive will be used and when the argument is a reference the farthest(widest-similar to discussion on anonymous nulls) type will be used.
On the other hand the designers of the Java programming language felt that implicit narrowing conversions of method and constructor arguments would add unnecessary complexities to the process of resolving overloaded method calls. So implicit narrowing is never attempted during overloading. Note: implicit narrowing is attempted during assignments and compound assignments.
Otherwise the binding is compile time binding and not dynamic binding as with polymorphism
 
saiprasad raut
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry and Swamy for your time...
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Swamy for such a great explanation of the concept.

Kaps
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic