Hi Rita, In Example 2, I search the Integer.java for the answer.
When you are using autoboxing (just like Line 026) , you will get a new Integer object in fact. So when you use "==" for compare the reference, it will not true. But be caution, the implement of autoboxing mechanism in the source do have set caches. Like: public static Integer valueOf(int i) { if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; //there's the cache else return new Integer(i); } So you may get true when you do it for a second time. To solve just using .equals() method instead of "==". In Example 3 Because String is inmutable, each time you set a new string, you will get a new String Object. So use .equals() instead as well. Hugs, Eric Cai On Thu, Jul 30, 2009 at 10:56 PM, RPGoldie <rpgol...@arcor.de> wrote: > > Hi all, > > I encountered a problem when working with Integer objects which I > would like to be discussed. Let me first show you my class: > > 001 public class Test { > 002 > 003 int memberVar; > 004 > 005 public static void main(String[] args) { > 006 > 007 // Example 1 > 008 Test t1 = new Test(); > 009 t1.memberVar = 7; > 010 Test t2 = new Test(); > 011 t2 = t1; > 012 > 013 System.out.println(t1 == t2); > 014 > 015 t2.memberVar = 55; > 016 System.out.println(t1.memberVar); > 017 System.out.println(t1 == t2); > 018 System.out.println(); > 019 > 020 // Example 2 > 021 Integer i1 = new Integer(1); > 022 Integer i2 = i1; > 023 System.out.println(i1 + ", " + i2); > 024 System.out.println(i1 == i2); > 025 > 026 i2 = 5; > 027 System.out.println(i1 + ", " + i2); > 028 System.out.println(i1 == i2); > 029 System.out.println(); > 030 > 031 // Example 3 > 032 String s1 = "Hello!"; > 033 String s2 = s1; > 034 System.out.println(s1 + ", " + s2); > 035 System.out.println(s1 == s2); > 036 > 037 s2 = "Bye!"; > 038 System.out.println(s1 + ", " + s2); > 039 System.out.println(s1 == s2); > 040 > 041 } > 042 } > > Example 1: I create 2 objects t1 and t2 of class Test. In line 011 t1 > is assigned to t2. Now t2 points to the same reference as t1, so line > 013 returns true. Now in line 015 I change the property of t2 to 55. > Because it is the same object as t1, line 016 gives 55. This is what I > expect objects to do. > > Example 2: The same matter but now with wrapper class Integer. In line > 023 both i1 and i2 have the same contents of 1. Line 024 gives true, > so they are pointing to the same reference. Ok. In line 026 I change > object i2 to 5 and expect i1 to change also, because it is the same > object! But it doesn't, line 027 gives 1 and 5, line 028 gives false! > Is this a typical behaviour of wrapper classes or why doesn't it > work??? > > Example 3: same with Strings. It behaves like the Integers in example > 2. > I guess this is because strings are immutable. So in line 037 there is > in fact created a new object of type String with contents "Bye!" and > s1/s2 are no longer the same object. Ok. > > But why does the wrapper class not do what I'm expecting??? > > I'm looking forward to your ideas. > Rita > > > > --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to javaprogrammingwithpassion@googlegroups.com To unsubscribe from this group, send email to javaprogrammingwithpassion-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en -~----------~----~----~----~------~----~------~--~---