0

In terms of instances of wrapper classes, does the instance behave differently when the instance is created via a String arg in the constructor in comparison to an int, double etc.

E.g is there a difference in:

Integer wrapperInt= new Integer(33);

Integer wrapperInt2= new Integer("33");
6
  • 1
    Why should it? What functionality do you expect to differ exactly? Commented Feb 26, 2014 at 11:53
  • What's the difference you are expecting? Commented Feb 26, 2014 at 11:53
  • Can't you check the JDK source? Commented Feb 26, 2014 at 11:53
  • What do u mean by JDK source? Commented Feb 26, 2014 at 11:53
  • docjar.com/html/api/java/lang/Integer.java.html (for example) Commented Feb 26, 2014 at 11:54

4 Answers 4

4

The end result will be the same - you'll have an Integer object with the value 33.

The version that takes a String will throw a NumberFormatException if the input string cannot be parsed.

Note: There's no need to write a statement like Integer wrapperInt = new Integer(33);. Let the compiler do it for you (auto-boxing):

Integer wrapperInt = 33;

If, for some reason, you do not want to use auto-boxing, then at least use Integer.valueOf(...) instead of using the constructor:

Integer wrapperInt = Integer.valueOf(33);

This is more efficient; the valueOf method can return a cached object (so that it's not necessary to create a new Integer object).

Sign up to request clarification or add additional context in comments.

2 Comments

Correct for 33 but incorrect in the general case; there's an edge case involving octal.
What do you mean "an edge case involving octal"? Please explain.
2

No, it doesn't. Both instances represent the integer 33. If there was a difference, it would be written in the javadoc.

Note that you should favor the usage of the factory methods instead:

Integer i = Integer.valueOf(33);
i = Integer.valueOf("33");

1 Comment

Read the question. The question is not "do these constructors do the same thing". Obviously they don't, since one takes an int and the other one a String. The question is: "do instances behave differently"? And the answer is no: an Integer, whether it's created with one or the other constructor, behaves the same way.
0

The only difference is you will be creating a string object unnecessarily in the second approach and it will try to parse the string you have passed to the constructor, If it couldn't parse the string then it will throw NumberFormatException.

Comments

0

The answer is that yes, there can be a difference between the two syntaxes. Specifically, the syntax

new Integer(33);

results in the value 33 being interpreted as an integer constant by the compiler and stored as a literal in the code. By contrast, the syntax

new Integer("33");

results in a call that routes the string value through Integer.parseInt(s, 10). This matters if the value in question has a leading zero character; in the case of an int literal, the compiler will evaluate the literal as octal:

new Integer(010); // equals 8

By contrast, the string constructor will always evaluate the value as base 10:

new Integer("010"); // equals 10

In any case, you should almost always be using Integer.valueOf(), as it is usually more efficient.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.