0

It is a simple silly question but I don't know but how can I state the situation myself. Assume I have a class like this

public class MyClass
{
  public int value1;
  public void assignValue(int v1)

    {
        value1=v1;
    }
    public MyClass(int v1)
    {
        value1=v1;
    }
    public void write()
    {
        System.out.println("value1 :"+value1);
    }
}

If I run My Program like this

public class Program {

    public static void main(String args[])
    {
        //first
        MyClass c1 = new MyClass(10);
        MyClass c2 = new MyClass(20);
        c2 = c1;
        c1.assignValue(15);
        c1.write();
        c2.write();

       //but these are classes too.
       Integer d1 = 10;//also Integer d1 = new Integer(10); same
       Integer d2 = 20;
       d2 = d1;
       d1 = 15;
       System.out.println(d1);
       System.out.println(d2);
    }
}

Why c1 and c2 s values are equal and why not d1 and d2 are not(I have created from Integer Class an object)?

5
  • 2
    Integer objects are immutable. Commented Apr 17, 2019 at 8:29
  • 2
    When you do c2 = c1 you make both variables refer to the same object. You don't reassign those variables after that, so they remain the same object. When you do d2 = d1 you make both variables refer to the same object. But then d1 = 15 makes d1 refer to a different object. Commented Apr 17, 2019 at 8:30
  • 2
    what exactly do you think this: c2 = c1; does? Commented Apr 17, 2019 at 8:30
  • You should follow the Java Naming Conventions: variable and method names are always written in camelCase. That means they start with lowercase. Commented Apr 17, 2019 at 8:42
  • you are aware that obj.method(value) is totally different than obj = value? first one is calling a method of the instance pointed to by obj; second one is changing (replacing) the pointer to point to another instance (this actually is independent of being immutable or not) Commented Apr 17, 2019 at 8:51

3 Answers 3

2
c2 = c1; // Both are pointing to same object

c1.AssignValue(15); // Value is being updated, not the actual reference.

Now, coming to the 2nd part of the code.

d2 = d1; // Both are pointing to same object

d1 = 15; // Reference object has been updated

But d2 are still pointing to the old object.

Sign up to request clarification or add additional context in comments.

Comments

1

Here c1.AssignValue(15); you are changing value not the object reference, but d1 = 15; this will change the object reference.

Comments

0

When you do c2 = c1; you are assingning the reference of the object.

When you do d1 = 15; you are essentially doing d1= new Integer(15). A new object is created and the reference of that held by d1. d2 is still referencing the old object (10)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.