-2

I am new to learning Java, and I was told to create object variables like this:

Integer a = new Integer(2);

Instead of like this:

Integer a = 2;

Can someone explain why is creating object variables the 2nd way bad? edit: I am adding this here cause I am getting mixed answers Which one am I supposed to use and when?

Pa3ckP7
  • 39
  • 5
  • 4
    Who says the 2nd way is bad? Do you have any reference for that. – Amit Bera Apr 18 '19 at 16:19
  • Well, the question is whether it is about creating objects or about the special case of `Integer`. `Integer`s between -128 and 127 inclusive are required to be cached, so with `Integer a = 2` you're not always creating an object. – MC Emperor Apr 18 '19 at 16:20
  • The new keyword instantiates a new object of the given type. The assignment of two for an integer only works because it's automatically boxed. – maio290 Apr 18 '19 at 16:21
  • These two do roughly the same thing. But the second is better IMO, because it is easier to type, is more readable, and also has the benefit @arshajii states in his answer (which I don't understand why was downvoted) – CryptoFool Apr 18 '19 at 16:24
  • This doesn't answer your question, but since you say you're new to Java, you may be confused. In most cases you shouldn't create an `Integer` object at all, but rather use the **primitive type** `int` (i.e., `int a = 2;`). – Mike Harris Apr 18 '19 at 16:28
  • No one said to me that the 2nd way is bad, but had an experience where it was giving me errors, because i was using the 2nd way. I don't remember what was i doing when it happened so don't ask me for the code. I use this when i need an object for object methods or when a method returns an Integer. – Pa3ckP7 Apr 18 '19 at 16:35

1 Answers1

3

The second approach is actually better, since it will implicitly call Integer.valueOf(). From the docs:

Returns an Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

(Emphasis mine.)

See also: Autoboxing

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • Thanks for the emphasis, I was confused how to do them Could you make the explanation easier cause I don't think I understand what you mean – Pa3ckP7 Apr 18 '19 at 16:25
  • You're lacking a source. And that's something which really bothers me. – maio290 Apr 18 '19 at 16:25
  • 1
    @maio290 The source is linked... – arshajii Apr 18 '19 at 16:25
  • No, the source doesn't give the info WHY this is done. This is found here https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html ... – maio290 Apr 18 '19 at 16:26
  • 1
    @maio290 While that's certainly important, I really don't think it's directly relevant to the question. The source clearly describes why this alternative is better, which is ultimately the point of the question. In any case I can reference that link as well. – arshajii Apr 18 '19 at 16:27
  • As of *Java 9*, the `Integer(int val)` constructor is deprecated. – WJS Apr 18 '19 at 18:07