0

In Java, when we assign an object to a variable of the matching class type, the variable only contains a reference to the memory location where the object in stored.

Is the case same with Primitive data types as well?

I mean, in int i = 10;, does i store the address of the memory location where the value 10 is stored?

PS: In sharp contrast, C++ actually stores the objects and not the references, right? Unless we use pointers and reference variables, right?

Trent Boult
  • 117
  • 3

2 Answers2

1

In Java, everything is stored by value. The value of an Object type in contrast to a primitive is the reference. Note that the wrapper types (like Integer) do constant interning for low values.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Well, that might work out OK according to your system of definitions, but, AFAIK, nobody considers the "value" of an object to be its reference. – Atsby Mar 09 '15 at 02:15
  • 1
    @Atsby You should, that's why you get the behavior of *pass by reference* in method calls (even though Java is *pass by value*). – Elliott Frisch Mar 09 '15 at 02:17
  • How can everything be stored by value when variables of user defined classes clearly only hold the reference to the object? – Trent Boult Mar 09 '15 at 02:18
  • @TrentBoult It's the value of the reference. – Elliott Frisch Mar 09 '15 at 02:19
  • AFAIK, everyone considers the "value" of an object to be the assignments that have been made to its fields. In Java, you always manipulate the object by reference, unlike C/C++ which allow you to pass the entire bunch of fields by value (if you insist on doing so). – Atsby Mar 09 '15 at 02:23
  • I am convinced by Java pass-by-value but not store-by-value – dragon66 Mar 09 '15 at 02:31
  • @dragon66 I think that's just semantics at the level of the runtime. – Elliott Frisch Mar 09 '15 at 02:40
-2

Indeed, in Java, primitives are always handled by value and objects are always handled by reference. Note however that these are the semantics; i.e., what the meaning of Java code is supposed to be. A particular implementation of Java (i.e., a JVM) is free to manage memory however it likes internally, as long as it appears to obey the correct semantics for anything that can be observed (i.e., output of the program).

And your PS remark is also correct.

Atsby
  • 2,277
  • 12
  • 14
  • So, let's see. int, double, char, bool, byte, short, long, float...these are the ONLY types that are supposed to be held by value, right? Is this list exhaustive? – Trent Boult Mar 09 '15 at 02:11
  • Yes, except it's boolean, not bool in Java. – Atsby Mar 09 '15 at 02:27
  • @Atsby Please have look at [this](http://stackoverflow.com/a/40523/1612078) once. – user1612078 Mar 09 '15 at 03:03
  • @user1612078 I hadn't read that before, but you'll notice that I never said Java was pass-by-reference for objects. I said objects are always "handled" by reference. Strictly speaking, the terms pass-by-reference and pass-by-value are informal and don't make sense for Java because Java always forces one level of indirection for objects and zero levels of indirection for primitives, and it just doesn't support the kinds of abstractions that would allow anything to be passed in a different way than the default/mandatory way. – Atsby Mar 09 '15 at 03:43
  • @Atsby In other words you mean to say is, the only possible way to access an object is through a reference variable. Well, thats right. But when it comes to the concept like _pass-by-value_ or _pass-by-reference_, make a note that, its **a primitive variable** that is passed-by-value, and its **an object-reference** (and not an object itself) that is passed-by-value. And thats why it is said that java is always pass-by-value. What is senseless in it, I don't understand. – user1612078 Mar 09 '15 at 04:26
  • You can think of it as an object-reference that is passed by value, but you can also think of it as the value of the object (i.e., the collection of fields that it "is") being passed by reference. With your definitions, even C is always pass-by-value, even when you pass a pointer. – Atsby Mar 09 '15 at 05:35
  • 1
    @Atsby _but you can also think of it_. No you can't. _call-by-reference_ is a concept that applies to variables. There _reference_ is a reference to the variable, its identity. There's no such thing in Java. – Sotirios Delimanolis Mar 09 '15 at 14:43
  • @SotiriosDelimanolis IMHO, there isn't a lot of difference between a local variable and an object field. I think we all agree that if you pass an object field to an &-parameter in C++, that is call by reference. Passing an object parameter in Java works exactly like an abbreviation for call by reference of each field of the object. It also works exactly like call by value of a reference to the object. I don't see a compelling reason to say it's one and -not- the other, both interpretations have the same semantics. – Atsby Mar 09 '15 at 19:38
  • My point is to use the appropriate terminology in the appropriate context. Call-by-reference is about variable identity. Java does not allow you to evaluate variable identity and therefore cannot support pass-by-reference. The fact that you can dereference an object to access its fields is besides the point. – Sotirios Delimanolis Mar 09 '15 at 20:51
  • If you'd like to edit your answer to clarify what you mean, I'll re-evaluate my downvote. – Sotirios Delimanolis Mar 09 '15 at 20:52
  • @SotiriosDelimanolis I don't think it's appropriate to edit the answer since the question wasn't actually about call/pass-by-whatever in Java. Somehow I got shanghaied into such a discussion in comments; I didn't realize when I got roped in that this call/pass-by-whatever was such a charged topic. – Atsby Mar 09 '15 at 23:48
  • Do what you wish. You obviously wrote something which people didn't agree with or were confused about. – Sotirios Delimanolis Mar 09 '15 at 23:54
  • @SotiriosDelimanolis That's an inherent risk when you try to explain something that is actually complex in simple terms. There is of course a chance that someone will be confused further by a 'simple' explanation. I'm satisfied that, on the whole, more good than harm comes from 'simple' explanations, so I don't hesitate to make them. – Atsby Mar 10 '15 at 00:20