Abhishek
Abhishek

Reputation: 2525

Copying one object to another Object is yielding different results in java

public static void main(String[] args) {
    Integer a = 1;
    Integer b = 0;
    b=a;
    System.out.println(a);
    System.out.println(b);
    ++a;
    System.out.println(a);
    System.out.println(b);
}

output : 1 1 2 1

public static void main(String[] args) {
    ArrayList<Integer> a = new ArrayList<Integer>();
    ArrayList<Integer> b = new ArrayList<Integer>();
    a.add(1);
    a.add(1);
    a.add(1);
    a.add(1);
    b=a;
    System.out.println(a.size());
    System.out.println(b.size());
    b.add(2);
    System.out.println(a.size());
    System.out.println(b.size());
}

output : 4 4 5 5

For above code why both objects are not referring to same memory location.

Upvotes: 5

Views: 109

Answers (5)

Srinivasu
Srinivasu

Reputation: 1235

    Integer a = 1;
    Integer b = 0;
    b=a;
    System.out.println(a);
    System.out.println(b);
    if(a == b){
        System.out.println("Both objects are equal");
    }
    ++a;
    if(a != b){
        System.out.println("Both objects are different");
    }
    System.out.println(a);
    System.out.println(b);

Upvotes: 2

user3437460
user3437460

Reputation: 17474

This is because Integer are immutable. Once created, you can't change it. If you wants to force the value to change, it will point to a new memory location. When you did ++a, a becomes a new object.


It might be easier if you look at it from the point of view of a String. (String are immutable as well)

    String s1 = "aaa";
    String s2 = s1;

    s1 = "bbb";
    System.out.println("s1: " + s1);
    System.out.println("s2: " + s2);

OUTPUT:

s1: bbb
s2: aaa

Upvotes: 3

muasif80
muasif80

Reputation: 6016

All the wrapper classes are immutable in Java actually. We know String as a famous immutable class. In addition to it other wrappers like Integer is also immutable.

See this http://www.javaworld.com/article/2077343/learn-java/java-s-primitive-wrappers-are-written-in-stone.html

Upvotes: 3

k_g
k_g

Reputation: 4483

++a creates a new object =a+1 then sets a = this new object. b is not changed because assignment doesn't affect the object that was contained in the variable being reassigned.

On the other hand .add(2) adds 2 to the list specified by a, which is the same reference as the one specified by b since they have been equated.

Upvotes: 0

TheLostMind
TheLostMind

Reputation: 36304

Case -1 :

public static void main(String[] args) {
    Integer a = 1;
    Integer b = 0;
    b=a;                      // 2 references a and b point to same Integer object
    System.out.println(a);
    System.out.println(b);
    ++a;                      // now a references to a new integer object with value 2 where as b refers to old integer object with value 1
    System.out.println(a);
    System.out.println(b);
}

Case 2 :

Again both a and b both refer and work on same arrayList instance. So modifying using one reference is reflected in other reference as well

Upvotes: 1

Related Questions