Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
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
Forum:
Programmer Certification (OCPJP)
valueOf() vs parseXXX()
Chaitanya Jadhav
Ranch Hand
Posts: 42
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
According to K & B
The difference between the two methods is
* parseXxx() returns the named primitive.
* valueOf() returns a newly created wrapped object of the type that invoked
the method.
But both method return primitive as well as Object.??
then what is difference between them?
Integer i1=Integer.valueOf("22"); int i2=Integer.valueOf("22"); double d1 = Double.parseDouble("3.14"); Double d2 = Double.parseDouble("3.14");
SCJP 1.5 80%
Ankit Garg
Sheriff
Posts: 9707
43
I like...
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Chaitanya the book is right. valueOf returns a wrapper class object. This code
int i2=Integer.valueOf("22");
works only because of autoboxing. valueOf here returns an Integer object which is unboxed to int. To
test
this, try this code
class ValueOfTest { void method(Integer i) { System.out.println("Integer"); } void method(int i) { System.out.println("int"); } public static void main(String[] args) { new ValueOfTest().method(Integer.valueOf("22")); //displays Integer } }
Same is the case with parseInt here
Double d2 = Double.parseDouble("3.14"); //works because of autoboxing
SCJP 6 | SCWCD 5 |
Javaranch SCJP FAQ
|
SCWCD Links
Chaitanya Jadhav
Ranch Hand
Posts: 42
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Ankit...
nice example .
thanks..
now i understood..
SCJP 1.5 80%
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
parseXxx and valueOf
Doubt with String Methods
difference between parsexxx() and valueOf()
parseXXX vs valueOf In Primitive Type Wrapper Classes
parseXxx() and valueOf()
More...