• 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

Difference between xxxValue() methods and casting

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The xxxValue() methods of numeric wrappers seem to work just like casting.

Please take a look at the following simple program:


The last line prints true.

So what is the advantage of providing these methods in Java language?

Thanks,
Nidhi
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The casting was not introduced until Java 5.0, and it implicitly calls xxxValue() in the background.

When you (implicitly or explicitly) cast a Float to a float (or Integer to int etc) the compiler calls floatValue() (intValue() etc). This will give a NullPointerException if the Float (Integer etc) is null.
When you (implicitly or explicitly) cast a float to a Float (or int to Integer etc) the compiler calls valueOf (which is available for all primitive types).
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,

That is very interesting. Thanks for sharing the info!!

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

Nidhi Sar wrote:
That is very interesting.



The automatic conversion between primitives and their corresponding wrapper classes is called autoboxing (which was introduced in version 5).

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic