• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing an Object without casting!

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The code below prints - Object :2



I was expecting something in hashcode,as automatic unboxing doesn't work with Object class.
So what am i missing?
Thanks
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I was expecting something in hashcode,as automatic unboxing doesn't work with Object class.
So what am i missing?



The Integer class overrides the toString() method to return the integer value as a string -- which is what is used by the system out.

Henry
 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,but which Integer class you are talking about?
When i iterate through the vector,objects of Object class are returned.Correct me if i am wrong.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Max White wrote:Hi Henry,but which Integer class you are talking about?



The type of the instance that you added to the vector -- see your example for details.

Max White wrote:When i iterate through the vector,objects of Object class are returned.Correct me if i am wrong.



Polymorphism applies here. An Integer IS-A Object.... In other words, the Object that is returned is the same Integer that you added.

Henry
 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really confused now.As far as i know if we don't use generics,we always get an Object type from a Collection and we need to cast it to the appropriate type.For example -



How does it prints the actual value at [3] and not hashcode?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am really confused now.As far as i know if we don't use generics,we always get an Object type from a Collection and we need to cast it to the appropriate type.



When a subclass overrides a method, then it will be the subclass' method that will be invoked, when that method is called -- and this is true regardless of the type of the reference (subclass or superclass) used to access the object.

So... it doesn't matter if it is an Object reference, or an Integer reference. The actual object is an Integer object, and it is the toString() method of the Integer class that will be called, regardless of the reference, and / or any casting.

Henry
 
Max White
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The actual object is an Integer object



I know that actual object is an Integer object,but how does the compiler knows?
Thanks!
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Max White wrote:
I know that actual object is an Integer object,but how does the compiler knows?
Thanks!



Actually, the compiler doesn't know -- the resolution of the method to invoke is completed at runtime.

Henry
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




The above statement in your code is analagous to Object o = new Integer (2) ; Even though iterator.next() returns an object with an Object reference, it is still an Integer object. Hence the Integer version of toString executes. This is due to inheritance.




This results in a compiler error because you are equating a primitive with an object. During Autoboxing the compiler is looking for the reference type rather than the actual object you are passing through. Hence the casting.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic