• Post Reply Bookmark Topic Watch Topic
  • New Topic

why confusing...  RSS feed

 
pias Khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why this will print String Version.?
Thanks in advance

[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ August 04, 2004: Message edited by: Dirk Schreckmann ]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you send an object as a parameter, it goes as it's most narrow definition available. For instance, if you send a BlueJay as a parameter, and the choices of signatures are Animal, Bird, and MultiCellularOrganism, it will go under Bird, the most defined one.

Now, here's the kicker: null is considered belonging to every class, so to speak. Null could be a Animal, Bird, or Multicelluar organism. Thus, in your example, String is more exact than Object. And since null is a String, it goes with that.

dig?
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It prints String Version becos String is a sub class of Object.
Infact, if u replace String with any other class, still the same method
will be invoked.

If u comment out this method, then the method invocation will move up the
hierarchy and prints the Object version.


vijay.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you send an object as a parameter, it goes as it's most narrow definition available.

This is true as long as there is a single hierarchy path that the compiler can follow. If there is more than one hierarchy path that can be followed, a compile time error occurs.



Now, here's the kicker: null is considered belonging to every class, so to speak.

Strictly speaking, null is not anything: the expression

null instanceof ???

is always false. However, any reference variable can be assigned the null reference and the null reference can always be cast to any reference type.

And since null is a String, it goes with that.

Again, null is not anything. The compiler simply matches the most specific type it can find that is not ambiguous.
[ August 03, 2004: Message edited by: Junilu Lacar ]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fair enough... I was close, though...
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


as long as we're being picky, this will give you a nice little error too: Baz extends Baz.

 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as long as we're being picky, this will give you a nice little error too: Baz extends Baz.

You got me there, pardner
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!