• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ian Stone
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic