*
The moose likes Beginning Java and the fly likes parseInt(String) or Integer.valueOf(s).intValue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parseInt(String) or Integer.valueOf(s).intValue" Watch "parseInt(String) or Integer.valueOf(s).intValue" New topic
Author

parseInt(String) or Integer.valueOf(s).intValue

Ian Stone
Greenhorn

Joined: Sep 15, 2004
Posts: 13
OK, It's not 10am and I haven't had my first coffee of the morning but...

Is there any advantage to using
int val = Integer.valueOf(someNumberAsString).intValue();

as opposed to the more terse (single method invocation)
int val = Integer.parseInt(someNumberAsString);

I've tended to always use the latter.
Oh, and I am using jdk1.3.1 and jdk1.4 and not assuming autoboxing
(the new Java 5/Tiger language feature) is available to me.

Comments on why one might adopt the first more unwieldy version
over the second one?

~IanS
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In my JDK, Integer.valueOf is implemented as

return new Integer(parseInt(s, 10));

That is, valueOf is just a shorthand for creating an object from a parseInt call.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Comments on why one might adopt the first more unwieldy version
over the second one?


I don't think there's any good reason. Probably some people found the valueOf solution first, and stopped looking. Integer.parseInt() is all you need.


"I'm not back." - Bill Harding, Twister
 
Don't get me started about those stupid light bulbs.
 
subject: parseInt(String) or Integer.valueOf(s).intValue
 
Similar Threads
convert string to primitive integer
Compiler error in JavaBean
Why is it???
What is the differece b/w Parseint and ValueOf()
Need suggestion on implementation