This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • 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: 23945
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: 23945
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
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic