Java doesn't have reference type?

Wrote by: hoangnguyen     Create date: 13/07/2016     774 views

"Object assign reference,  primary assign value" is the mantra that a lot of programmers have read or heard when learning object-oriented programming (OOP). Forget the mantra and take a look at the code below. Let's speculate the outcome of line 9 - f.log () and 12 - f.log ().

 

package java_pointer;
 
public class Main {
 
    public static void main(String[] args) {
        Foo f = new Foo("f");
         
        changeReference(f);
        f.log();
         
        modifyReference(f);
        f.log();
    }
     
    public static void changeReference(Foo a){
        Foo b = new Foo("b");
        a = b;
    }
     
    public static void modifyReference(Foo c){
        c.setName("c");
    }
     
    static class Foo{
         
        private String mName;
         
        public Foo(String name){
            mName = name;
        }
         
        public void setName(String name){
            mName = name;
        }
         
        public void log(){
            System.out.println("ten toi la: "+ mName);
        }
    }
}

 

 Have you guessed the results yet? Is your results like below? If not the same, please copy the code above and tested to verify.

ten toi la: f
ten toi la: c

 

Why is that? Why not "b", that was "f"? The answer is: because Java doesn't have reference type. Most of developer think that Java is the pure object-oriented language, Java doesn't have pointer but the pointer really exist inside, nothing less than it was hidden away. Everytime we initialize an object, ex: "Foo f = new Foo("f")" so f can be considered as a pointer. To prove this, we will figure out how the code block working.

 

1. Firstly at code line 6

Foo f = new Foo("f");

 

Here we initialize an object  f with type Foo, assign it a name  f.

 

2. Consider the function "public static void changeReference(Foo a)". an object a with type Foo declared, but doesn't point to anywhere(null)

 

3. At line 8 changeReference(f) because we assign f as a param so currently a also point to the same memory as f.

 

4. Line 16

Foo b = new Foo("b");


Initialize an object b type Foo and assign it a name b.

 

5. Line 17

a = b;


At this time a will point to the same memory area as b(none f). So, when we out the method changeReference at line 8, f had not been changed, value of field mName still f.

 

 6. dòng 11

modifyReference(f);


Same as line 9. one object c with type Foo created and point to the same memory area as f.

 

 7. Line 21

c.setName("c");


Because c and f point to the same memory area so when c change, f also change.

 

Apply the above code block on other language such as C#, C++ you will get the same result as we saw. So C#, C++ doesn't have reference type, too? actually not so, in C# we have concepts ref and out, in C++ we can use pointer to to help us assign parameter as reference type. But in Java doesn't have these concepts.


Source: Here

Nguyễn Ngọc Hoàng - Software Engineer
Ngoc Hoang Nguyen

If we cannot do anything, due to we haven't yet acquainted with it.

Go on will also reach to destination.

Follow me

Please wait