0

The two following codes give me the same result even though only one of them use the keyword new:

Code 1:

Integer test = new Integer(3);

Code 2:

Integer test = 3;

From an OOP perspective, is there anything different between the two codes?

shmosel
  • 49,289
  • 6
  • 73
  • 138
Jen123
  • 49
  • 1
  • 7
  • Probably worth updating your question title. If it were literally true, then you'd be asking about the code `Integer test = Integer(3);`. – Oliver Charlesworth Apr 09 '18 at 22:35
  • The line in code is the same as above: Integer test = 3; – Ninja Apr 09 '18 at 22:37
  • That only applies to *some* types (such as `String` and primitive wrappers), but not *all* types. – Vince Apr 09 '18 at 22:39
  • 5
    You should look into autoboxing if you want to know why your example works. – Jacob G. Apr 09 '18 at 22:49
  • From an _**OOP**_ perspective there _is_ a difference; from a _Java_ perspective there is no difference because of [Syntactic Sugar](https://en.wikipedia.org/wiki/Syntactic_sugar) and [autoboxing](https://stackoverflow.com/q/27647407/17300) – Stephen P Apr 09 '18 at 22:56
  • In `test = 3;`, the `3` will come out of the constant pool, whereas using `new` will create a brand new reference. – Logan Apr 09 '18 at 22:59
  • The second line is a special case. It only works because Java offers **auto-boxing** for primitive types (`int`, `short`, `long`, `byte`, `char`, `float`, `double`). It won't work for Objects. It basically means that Java will automatically convert `3` to an `Integer` representing `3` if needed, like here. And also vice versa if needed. – Zabuzard Apr 09 '18 at 23:27

2 Answers2

4

Instantiating variables with new keyword allocating the variable in the heap memory as a new object but if you used direct initialization the variable will be stored into the JVM pooled memory and if you declared a new variable with the same value it will reference the value for the first variable , there is two benefits of using direct initializing :

  1. Better performance
  2. Less memory needed for the program requirements

Note : you should be aware of using direct initializing as the JVM pooled memory has limited size .

Mohammad
  • 739
  • 7
  • 22
2

What is actually happening here is the compiler is generating the same byte code for the line test = 3; as if you wrote test = Integer.valueOf(3);. In Java, this is called autoboxing. It allows primitive types to be converted to an Object (reference type) and vice-versa without explicitly declaring so in your code. You can read more about it here: Autoboxing and Unboxing at the Oracle Java Tutorials.

In Java, at some point, almost all instances of objects are created with the new keyword, and it is necessary to use new to call a constructor, which will allocate memory for and initialize the fields of an object; whether it's done explicitly by the programmer or behind the scenes is another matter. For example, Integer.valueOf(3) could create a new instance of Integer representing the primitive int 3 when called, but it could also return an old reference to a previously created instance of Integer representing 3, since instances of the Integer class are immutable. Either way, you'll get a reference to an Integer object representing the primitive int value 3.

Jason Carlson
  • 398
  • 2
  • 4