Home > Back-end >  How do object references work in wrapper classes objects and custom objects?
How do object references work in wrapper classes objects and custom objects?

Time:10-31

Consider the below code:

class Student{
    int id;
    String name;
}

public class Main
{
    public static void main(String[] args) {
        
        Student s1 = new Student();
        s1.id = 10;
        s1.name = "student 1";
        
        Student s2 = new Student();
        s2.id = 20;
        s2.name = "student 2";
        
        Student s3 = s1;
        s3.name = "student 3";
        
        System.out.println(s1.id  " " s1.name);
        System.out.println(s3.id  " " s3.name);
    }
}

For the above code the output is:

10 student 3
10 student 3

But when I use Objects of Integer, String or some other wrapper class objects the output is different. Consider the below code:

public class Main
{
    public static void main(String[] args) {
        
        Integer a = 10;
        Integer b = 20;
        Integer c = a;
        
        c = 30;
        
        System.out.println(a);
        System.out.println(c);
    }
}


Output for the above code is:

10
30

In my custom object, if I change the value for one object, then both the objects value gets changed, but why it's not the case in Objects like Integer and String.

My confusion is how do references work in classes like Integer, String, Double and so on.

CodePudding user response:

s1 refers to a part of memory, lets call that part A. after assigning s1 to s3, s3 will also refer to A; meaning that s1 and s3 both have pointers to a same memory block. changing s1 or s3 is actually changing data in that memory block; and will affect both.

CodePudding user response:

The wrapper classes are immutable. This means that if we want to change the value of an wrapper class, we have no choice but to assign a new value to the variable referencing the wrapper. Hence, in the code presented, we create three instances of Integer (through autoboxing) that are accessible:

Integer a = 10; // 1st instance of Integer created
Integer b = 20; // 2nd instance of Integer created
Integer c = a;  // still 2 instances, c references the same Integer as a
    
c = 30; // 3rd instance of Integer created
    
System.out.println(a);
System.out.println(c);

We can make this visible by extending the code a little bit:

Integer a = 10; // 1st instance of Integer created
Integer b = 20; // 2nd instance of Integer created
Integer c = a;  // still 2 instances, c references the same Integer as a
System.out.println(a == c); // will print "true", a and c reference the same exact Integer
    
c = 30; // 3rd instance of Integer created
    
System.out.println(a);
System.out.println(c);
System.out.println(a == c); // will print "false", a and c reference different Integers

Ideone demo

We can see that references of wrapper-types do not get any special treatment. The behaviour (aside from the capability of autoboxing) is the same as with any outher reference-type in Java.

CodePudding user response:

You are comparing apples and oranges.

In the code snippet A

    Integer a = 10;
    Integer b = 20;
    Integer c = a;
    
    c = 30;

you are not modifying the object referenced by a and c, you are assigning a new reference to c.

In the code snippet B

    Student s1 = new Student();
    s1.id = 10;
    s1.name = "student 1";
    
    Student s2 = new Student();
    s2.id = 20;
    s2.name = "student 2";
    
    Student s3 = s1;
    s3.name = "student 3";

you are not assigning a new object reference to s3, you are modifying the object referenced by s1 and s3.

A code snippet on par with code snippet A would be

    Student s1 = new Student();
    s1.id = 10;
    s1.name = "student 1";
    
    Student s2 = new Student();
    s2.id = 20;
    s2.name = "student 2";
    
    Student s3 = s1;

    s3 = new Student();
    s3.name = "student 3";
  • Related