4

Given that String s has been declared, the following 5 lines of code produce the same result:

int i = Integer.valueOf(s);
int y = Integer.parseInt(s);
int j = Integer.valueOf(s).intValue();
Integer x = Integer.valueOf(s);
Integer k = Integer.valueOf(s).intValue();

Are there circumstances where each one would be the preferred code? It appears that int and Integer are interchangeable and that .intValue() is not needed.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Nick
  • 41
  • 3

3 Answers3

7

If you require an int, use parseInt(), if you require an Integer use valueOf(). Although they're (sort of) interchangeable now, it still makes more sense to use the one that directly returns the data type that you require. (Historically, they weren't interchangeable at all, this was introduced with auto-boxing and unboxing in Java 5.)

The intValue() method you're using is just converting the Integer class type to the int primitive, so using that and valueOf() is the worst possible combination, you never want to use that. (Nothing bad will happen, it's just longer to read, performs slightly worse and is generally more superfluous.)

If you don't care or don't know, then I'd use parseInt(). Especially as a beginner, it's more common that you want the primitive type rather than the class type.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • The text I'm using introduced parseInt() and valueOf methods together and intruduces wrapper types later in the text. Thanks. – Nick – Nick Jan 23 '13 at 17:31
3

int and Integer are made to look interchangeable by the magic of auto-boxing and auto-unboxing: In many cases where you need one but have the other, the compiler automagically inserts the necessary code to convert them.

This is useful, but if you know about it, you can avoid it in many places which results in slightly faster code (because there's less conversion to do).

  • Integer.parseInt() returns an int, so you should use it if you need an int
  • Integer.valueOf() returns an Integer, so you should use it if you need an Integer
  • Integer.valueOf().intValue() first creates an Integer and then extracts the int value from it. There's no good reason to use this instead of a simple Integer.parseInt().

The decision between int and Integer is easy to do as well:

  • generally you'd want to use the primitive type (int), if possible
  • if you need an Object (for example, if you want to put your number in a Collection), then you need to use the wrapper type (Integer), as the primitive type can't be used here.
Community
  • 1
  • 1
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • SUPER explanation! I haven't got to Collections and wrapper types yet. The text I'm using introduced parseInt() and valueOf methods together and intruduces wrapper types later in the text. Thanks. – Nick Jan 23 '13 at 17:28
0

int and Integer are not interchangeable.Because of Autoboxing feature fron java 5 onwards, int to Integer conversion is taken care by jvm itself.But we should not use Integer class unnecessarily.Primitive data types are always faster.Wrapper classes should be used only required.

Renjith
  • 3,274
  • 19
  • 39