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 REST with Spring (video course) this week in the Spring 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

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

Ian Stone

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?

Ilja Preuss

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

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
jQuery in Action, 3rd edition