1

I know that java is return by value not by reference, so it makes a copy of the object and passes the new copy. Is this is the case with the return statements also? Does the

return obj;

creates a new object copy or simply returns the current object itself.

me_digvijay
  • 5,374
  • 9
  • 46
  • 83
  • You say "_I know that java is return by value not by reference, so it makes a copy of the object and passes the new copy_". That is nothing but incorrect. – jlordo Jan 24 '13 at 11:47
  • possible duplicate of [Is Java "pass-by-reference"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) – McDowell Jan 24 '13 at 11:47
  • @jlordo: Yes, you are right. Can I say "it makes a copy of the reference to the object and not the object itself"? – me_digvijay Jan 24 '13 at 12:08

7 Answers7

8

No, this is a wrong assumption. It's true that Java passes everything by value, but this means the following:

  • Scalar variables contain values, so you can easily see that they are passed by value.

  • Object variables, on the other hand, do not contain objects. You could think of them as containing a pseudo-address to an object. That pseudo-address is copied (by value), but it still points to the same object. (Objects are actually created only using a new expression.)

The above is true (for both types of variables) for assignments, arguments and return values.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
2

so it makes a copy of the object and passes the new copy

No! it does not!

Return by value means the the value in the obj reference is returned, if obj points to some object,other reference which collects a return value from this returning method will also point to the same object as which obj is pointing to.

No, it does not create another object to return.

codeMan
  • 5,730
  • 3
  • 27
  • 51
1

Java doesn't copy the object, it passes the object reference to the method, and returns it when you use return

The only time where it copies the value is when you use primary types, like int, char or double

Montolide
  • 773
  • 3
  • 12
  • 27
1

It just return the current object itself.

Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
  • It returns a reference to the object. Not the object itself. Subtle difference, with huge impact. – jlordo Jan 24 '13 at 11:48
1

What you say is more like the C++ of doing such things. When you return an object(non-basic type), you will return a reference and you will not create a new object or perform a copy.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
1

In java atomic types (char,int,long etc.) are returned by value. Objects are always returned by reference (without cloning).

stacker
  • 68,052
  • 28
  • 140
  • 210
1

Does the return obj; creates a new object copy or return the current object itself?

--> obj is an object reference which points to the actual object on the heap. New object reference(by you) will be created in your method where you are assigning returned obj.

Object method() {
  //....
  return obj;
}

void methodA() {
  Object objA = method(); // obj and objA are different because of their scopes but values are same(assigned to objA). That's why both points to the same Object on heap
}
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85