3

I read that Java does everything by call by value. I was wondering how to verify this fact? As far as I understand, in case of objects(not primitives) functions get its own copy of reference but points to same object. In that case, reference of that object in callee function and caller function should be different? How can I verify that? In other words, how to print the reference of the object.

System.out.println(object);  //does this print reference i.e text following @

EDIT: I understand that modifying object in callee function does change the value in caller function. I am interested in how to print the references of objects as in what property can I print on console that clearly shows me 2 different reference.

prap19
  • 1,818
  • 3
  • 16
  • 26

3 Answers3

9

Java passes references by value. This means you'll get a copy of the reference, so once you dereference that you'll get to the same object in the heap as with the original reference.

But if Java was pass by reference:

public static void nullify(Object obj) {
    obj = null;
}

public static void main(...) {

    String x = "Hello";        
    nullify(x);

    System.out.println(x);
}

The call to S.o.p. would print null if Java was pass by reference. But it isn't, so x is unchanged and you'll get Hello.

NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • Try changing the value in the obj. I understand that there 2 references created one for main and one for nullify. On making obj=null, 1 reference is eliminated but main still holds one. – prap19 Nov 24 '11 at 23:40
  • 1
    @prap19 I don't think you understood me... In a language that supports pass-by-reference, nullifying `obj` inside the method would nullify it everywhere, including in the callee. This proves Java doesn't support pass-by-reference. – NullUserException Nov 24 '11 at 23:45
  • i understand your point, I am actually interested in printing out some reference information about the object? In your case, I want some information about obj to be printed in nullify() and print similar information in main() for String . – prap19 Nov 24 '11 at 23:53
  • @prap19 Java doesn't let you mess with memory addresses. That's by design. In Java, it doesn't matter what address an object is; you can't access an object using its address anyways. – NullUserException Nov 24 '11 at 23:55
  • hmm ok ok..i was just wondering if there is any exposed api that gets me such information. – prap19 Nov 24 '11 at 23:56
  • @prap19 That's abstracted away so you don't have to worry about it. Messing with pointers can be a very error prone process. – NullUserException Nov 24 '11 at 23:59
1

Assuming Class A property aa

A a= new A();
a.aa = 1;
// print a.aa here should be 1
method(a);
// here a.aa should be 2


Void method(A a) {
  a.aa =2;
  a = new A();
  a.aa = 3;
}

So this shows reference was passed as value. When you change the object in the method it does not change the reference of caller.

Sid Malani
  • 2,078
  • 1
  • 13
  • 13
0

I think the String Object@1ed3af is composed of the class name of your object and it's hashcode, separated by a @. This is a unique string, identifying it.
Read this topic to get a full explanation !

Sephy
  • 50,022
  • 30
  • 123
  • 131
  • @prap19: So this is a hexcode of length 6, which means you can produce 16^6 different objects in Java? This would be 16M objects - not very much! So it can't be unique. Unique is in contradiction to `hashcode` as well. – user unknown Nov 24 '11 at 23:47