سؤال

I tried to search for this but couldn't find an answer easily.

If we have multiple Integer Java objects corresponding to the same integer value--are they the same in memory?

هل كانت مفيدة؟

المحلول

No, but if you use Integer.valueOf(), Long.valueOf(), etc you may get single cached values. Not the same thing.

نصائح أخرى

Not unless you create them like this

Integer a = new Integer(5);
Integer b = a;
Integer c = b;

In the above case, a, b, and c, would all refer to the same Integer object.

If, on the other hand, you declare them like this

Integer a = new Integer(5);
Integer b = new Integer(5);
Integer c = new Integer(5);

then the 3 variables, a, b, and c all refer to separate Integer object.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top