|
Didn't happen on mine, Paul: public class Immutable { public static void main(String[] args) { int int1 = 0; Integer i1 = new Integer(0); Integer i2 = new Integer(0); Integer i3 = new Integer(int1); boolean eq1_2 = (i1 == i2); boolean eq1_3 = (i1 == i3); boolean eq2_3 = (i2 == i3); boolean equal1_2 = (i1.equals(i2)); boolean equal1_3 = (i1.equals(i3)); boolean equal2_3 = (i2.equals(i3)); System.out.println("" + eq1_2 + ' ' + eq1_3 + ' ' + eq2_3); System.out.println("" + equal1_2 + ' ' + equal1_3 + ' ' + equal2_3); System.exit(0); } } Prints out: false false false true true true Joe > From: Paul Morgan > > JVMs will cache immutable objects. Even though you've called > > new Integer(0) > > five times you've only created one Integer object which gets referenced > five > times. You can confirm this by comparing Total1 = Total2. After being > initialized they should be equal (pointing to the same Integer(0) object).
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.