• 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

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic