• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

parseXxx() and valueOf()

 
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello guys;

page 241 from the K&B book, it states:
The difference
between the two methods is
■ parseXxx() returns the named primitive. >>> it also returns object of the type that invoked
■ valueOf() returns a newly created wrapped object of the type that invoked >>> it also returns the named primitivethe method.
Here are some examples of these methods in action:


the following is alos true


your comments please !


and one more thing

why this one runs well:


but this gives NFE at runtime?!
 
author
Posts: 23931
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

Hama Kamal wrote:
page 241 from the K&B book, it states:
The difference
between the two methods is
■ parseXxx() returns the named primitive. >>> it also returns object of the type that invoked
■ valueOf() returns a newly created wrapped object of the type that invoked >>> it also returns the named primitivethe method.
Here are some examples of these methods in action:



It doesn't also returns object of the type or vice versa -- as you can't define a method to return both an Object and a primitive value.

Hama Kamal wrote:
the following is alos true


your comments please !



While those statements do compile, it doesn't mean that both primitives and objects are returned. What is happening is that autoboxing is taking place. The compiler will generate code to convert primitives with its wrapper type. Specifically, it does this to the code -- which you don't see.



Henry
 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Hama Kamal wrote:
page 241 from the K&B book, it states:
The difference
between the two methods is
■ parseXxx() returns the named primitive. >>> it also returns object of the type that invoked
■ valueOf() returns a newly created wrapped object of the type that invoked >>> it also returns the named primitivethe method.
Here are some examples of these methods in action:



It doesn't also returns object of the type or vice versa -- as you can't define a method to return both an Object and a primitive value.

Hama Kamal wrote:
the following is alos true


your comments please !



While those statements do compile, it doesn't mean that both primitives and objects are returned. What is happening is that autoboxing is taking place. The compiler will generate code to convert primitives with its wrapper type. Specifically, it does this to the code -- which you don't see.



Henry



Hello Mr.Henry

thanks for your explanation,, what about the issue below

why the below statement runs well


but this one give NFE at runtime
 
Henry Wong
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hama Kamal wrote:
Hello Mr.Henry

thanks for your explanation,, what about the issue below

why the below statement runs well


but this one give NFE at runtime



Take a look at the javadoc for java.lang.Float and java.lang.Long, it will become clear why one works and the other doesn't. As for why the designers implemented the two parser inconsistently, that I do not know.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic