5 ms·
I gave up on this... It's explicitly said in the Java standard that the language is pass-by-value. On one occasion I've been told that the standard must be wron
by viraptor 12y ago
I gave up on this... It's explicitly said in the Java standard that the language is pass-by-value. On one occasion I've been told that the standard must be wrong then. Some people just don't want to accept reality if it doesn't match their interpretation of definitions. :-(
- kremlin 12y agoCalling it "pass by value" without qualifying that, for objects, it passes the reference by value, is very confusing.
- viraptor 12y agoBut these are completely separate things. You can have any mix of those (c# actually has all): - Pass by value, values hold object references. (java, c# objects) - Pass by value, values hold objects. (c# structs) - Pass by reference, values hold object references. (c# inout parameter, passing object) - Pass by reference, values hold objects. (c# inout parameter, passing struct)
- agricola 12y agoI'm not really sure what point you are trying to make here. In general, the whole thing about passing a reference by value is just BS. What you need to ask is, "What am I passing to the function?" If you are passing an object, and Java (or whatever language you are using) determines a reference (memory address or whatever), then passes that reference to the function, then you are, by definition, passing an object by reference. The important thing to focus on in this argument is, "What am I passing?" The answer for this scenario is that the programmer is trying to pass the object itself. Who cares about the address / location of the object. If you decide to pass the reference explicitly, then you are passing a reference, not the object. This would be like explicitly passing a pointer in c++, and is apparently a controversial topic when it comes to Java. A reference will nearly always (I can't actually think of a case where it isn't) be a primitive, and therefore, be passed by value. For argument sake, say I'm wrong about all this. My question would be, "What would it look like to pass an object by reference, and how does that differ from what is currently happening?"
- viraptor 12y agoThe point was, that there is a language close to java which can pass objects by value or by reference. (It passes object references by value or by reference if you want to call it like that) To pass an object by reference, it looks exactly like C#'s out/ref parameter. void function(ref SomeClass arg) { arg=null; } var a=new SomeClass(); function(a); // now a is null Which is different from what happens without ref. (or by default in Java)
- dlubarov 12y agoWell, the standard doesn't use "pass by reference" or "pass by value" terminology. It just uses the term "value" to refer to the result of evaluating an expression. The spec also says > there must be a standard way for the compiler of the creation expression to pass a reference ... to the member class's constructor So it seems perfectly correct to say that "objects are passed by reference", and that those references could also be referred to as values.
- viraptor 12y agoThe whole fragment is actually about why non-private inner classes have an implicit constructor parameter. It talks about compiler "passing a reference". Sure - that reference/pointer is passed - but it's passed by value. That last sentence is a bit backwards compared to the quote. The references are passed by value if one must put it like that. But nothing gets passed by reference in Java.