0
public class Concept {    
    int num;    
    public static void main(String args[])    
    {    
        Concept obj1=new Concept();    
        Concept obj2=obj1;    
        obj1.num=100;    
        obj2.num=200;    
        System.out.println(obj1.num);    
        System.out.println(obj2.num);    
        //obj1.num=null;    

        obj2.num=500;    

        System.out.println(obj1.num);    
        System.out.println(obj2.num);    
    }   

}     

Here, this is simple java program.

Output is

200
200
500   
500

How does the memory allocation work here? And how can we set null as Obj1.num=null?

Synchro
  • 35,538
  • 15
  • 81
  • 104
Tilak Raj
  • 472
  • 4
  • 12

6 Answers6

7

obj2 has the same reference of obj1, so modifying the state of any of these will be reflected in both variables since they're pointing to the same reference.

Here's a picture to explain the behavior of object references (explained in this case using Person class):

enter image description here

Source: https://stackoverflow.com/a/7034719/1065197

Note that you cannot nullify an int nor any primitive value in Java, so obj1.num = null is invalid. However, if you declare num as Integer you can set its value as null since Integer is a wrapper class for the primitive int.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
6

You ask two questions at once.


Java uses references, thus by setting obj2 = obj1, the values are not copied, both references refer to the same object. Setting the value of num through obj1 is the same as setting it through obj2.

In memory it looks like:

        +-----------------+
obj2 -> |  Concept        | <- obj1
        +-----+-----+-----+
        | num | int | 200 |
        +-----+-----+-----+

If you would however use the following code, two objects would be constructed:

Concept obj1 = new Concept();
obj1.num = 200;
Concept obj2 = new Concept();
obj2.num = 300;

Then the memory would look like:

        +-----------------+         +-----------------+
obj1 -> |  Concept        | obj2 -> |  Concept        |
        +-----+-----+-----+         +-----+-----+-----+
        | num | int | 200 |         | num | int | 300 |
        +-----+-----+-----+         +-----+-----+-----+

Thus modifying num from obj1 would set a different part of the memory than from obj2.


You cannot assign null to an int: it's a primitive type. If you want an integer with a null value, try Integer.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
3

Java is not C++,

Concept obj1=new Concept();    
Concept obj2=obj1; // obj2 is a reference to obj1.

I think you wanted,

Concept obj1=new Concept();    
Concept obj2=new Concept();

Also, you can't set a primitive type (like int) to null. You could set an Integer wrapper to null,

Concept {    
 // int num;
 Integer num = null;
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

You can't, because you are declaring num as an int, which is a primitive type.

What can you do? You can use its wrapper class Integer:

Integer num;
...
num = null; // valid since num is now an object
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
2
public class Concept {    
    int num;    
    public static void main(String args[])    
    {    
        Concept obj1=new Concept();    // Only one Concept object created. The reference obj1 is pointing to it.
        Concept obj2=obj1;    // now 2 references obj1 and obj2 are pointing to the same object.
        obj1.num=100;    // since both references are pointing to the same object, the changes they make will be on the `same` object
        obj2.num=200;    
        System.out.println(obj1.num);    
        System.out.println(obj2.num);    
        //obj1.num=null;    

        obj2.num=500;    

        System.out.println(obj1.num);    
        System.out.println(obj2.num);    
    }   

}     
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
1
Obj1.num=null;

num cannot be set to null because num is a primitive int, not an object. In Java only object references may be set to null.

As for the memory handling. I have annotated your code below to explain what is happening.

public class Concept {    
    int num;    
    public static void main(String args[])    
    {    
        Concept obj1=new Concept();      // A new object is created.  Ref stored in obj1
        Concept obj2=obj1;               // The ref from obj1 is copied to obj2
        obj1.num=100;                    // obj1 and obj2 now hold the same ref to the same object
        obj2.num=200;                    // overwrites previous assignment; the java compiler is likely to strip out the previous line as it is redundant
        System.out.println(obj1.num);    
        System.out.println(obj2.num);    
        //obj1.num=null;    

        obj2.num=500;                    // as before

        System.out.println(obj1.num);    
        System.out.println(obj2.num);    
    }   

}    

It is worth noting, that Java, unlike some other languages always creates its objects on the heap. A region of memory that is under its management, and it garbage collects for us. It does not create objects on the stack, thus fields and variables of type Object are always references to the object.

Chris K
  • 11,622
  • 1
  • 36
  • 49