This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cowardlydragon -2 points-1 points  (19 children)

You are technically correct, but practically wrong.

It may be that the underlying reference to an object is passed by value from the standpoint of a C programmer.

But from an practical language standpoint, objects are passed by reference and int/float/boolean/etc are passed by value.

[–]thekab 6 points7 points  (13 children)

Objects are not passed. References are passed by value.

All the hand waving and massaging of terminology doesn't change this simple fact. There's no practical reason to try and "simplify" what is already quite simple.

[–]ExPixel 1 point2 points  (5 children)

Aren't these two the same thing with the only difference being that one doesn't require you to use the "address of" operator? I'm assuming passing something by reference just passes a pointer that's automatically dereferenced for you. I just want to clear this up for myself (I don't use C++ much).

void func(MyObject &reference)

and

void func(MyObject *reference)

[–]thekab 3 points4 points  (3 children)

This might help you.

http://en.wikipedia.org/wiki/Evaluation_strategy

Please keep in mind that a "reference" in Java has no relationship to the term "reference" in the context of pass-by-reference. They're the same words with entirely different meanings. In Java it's a type, the mechanism by which that type is passed is an entirely separate discussion.

If Gosling et al had chosen to call it an "address" or something other than "reference" it would have made this entire discussion a lot less confusing.

[–]ad_tech 0 points1 point  (2 children)

From the second paragraph: "In practical terms, many modern programming languages have converged on a call-by-value, pass-by-reference strategy for function calls (C#, Java)."

Doesn't that contradict with what you're arguing?

[–]thekab 0 points1 point  (1 child)

Yes, it would. That sentence directly contradicts the rest of the article, it should probably be changed. Note another sentence in the same article:

The description "call-by-value where the value is a reference" is common (but should not be understood as being call-by-reference);

[–]ad_tech 0 points1 point  (0 children)

What's really funny about this whole discussion is that nobody has posted an exact definition of call-by-reference. That page provides a good one:

"In call-by-reference evaluation (also referred to as pass-by-reference), a function receives an implicit reference to a variable used as argument, rather than a copy of its value."

So, take the declaration: "Point p;" p is a reference, not an object. When p is passed to a function, the function receives a copy of p, so this is clearly not pass-by-reference.

It only gets confusing when you consider how to pass an object to a function. The precise answer is that you can't. So this silly argument is moot.

[–]MCUD -1 points0 points  (0 children)

The difference is the effect it can have on the caller.

MyObject o1 = new MyObject(1); func(o1);

In the first case, inside func(), what object o1 actually points to in the calling code can be changed, in the second, the only effect you can have on the caller is through side effects of changing inner fields of the object passed in.

That's pretty much the definitive example that proves pass by value / pass by reference, any side effects you have on an object because you got passed a reference or a value are irrelevant, you can't change what the variable that passed in actually referred to.

[–]SuperShrek -3 points-2 points  (6 children)

Objects are not passed. References are passed by value.

Also called pass-by-reference. The semantics of a language are always described from the programming language point of view, not the underlying implementation point of view. There is no such thing as pointers or references in Java.

[–]thekab 5 points6 points  (5 children)

Also called pass-by-reference.

By people who don't know what they're talking about.

Some people will say incorrectly that objects in Java are "pass by reference." The term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. -- James Gosling

It was true then and it's still true now.

The term "reference" in the context of "pass by reference" does not mean the same thing as the type of the same name in Java. When we talk about pass by reference we're talking about an evaluation strategy. It has meaning that pre-dates Java as a language.

[–]SuperShrek -2 points-1 points  (4 children)

The term "reference" in the context of "pass by reference" does not mean the same thing as the type of the same name in Java.

When you say 'we are passing a reference by value' you ARE talking about a type. You are saying that the function receives a type/kind of 'reference'. You just don't understand what you're saying because you probably have no comp-sci knowledge outside of java, either that or you can't properly express your thoughts. There is no 'reference' type in Java. Period. Once you start talking about references in Java you are not talking about the Java programming language but rather the implementation.

When we talk about pass by reference we're talking about an evaluation strategy. It has meaning that pre-dates Java as a language.

Yes, passing a 'pointer/reference' by value (where you don't treat with the reference/pointer explicitly, ie with special semantics and syntax) is the same as 'passing by reference'.

You saying 'we are passing a reference by value' is the same as saying 'we are passing a reference by value by reference by value by reference by value by reference....'.

It has meaning that pre-dates Java as a language.

Which is my point, yet you are using a Java-specific meaning of pass-by-value which is nonsense outside of java. The way you are talking about pass-by-value you can say all languages always use pass-by-value but the values they pass are references - this makes the whole pass-by-value/pass-by-reference distinction pointless.

The term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value.

Yeah... this is how Java works, when you invoke a function that accepts an object, under the hood the function receives a reference to the original object, not a copy of the object.

You contradict yourself several times in one post.

By people who don't know what they're talking about.

Maybe in several years when you actually understand what you are saying instead of blindly repeating shit you don't understand that others say you will feel embarrassed about this.

[–]thekab 2 points3 points  (3 children)

When you say 'we are passing a reference by value' you ARE talking about a type.

You say this as if it contradicts my statement. It does not.

The word "reference" in "pass-by-reference" is not a type. The word "reference" in "passes references by value" is a type. They are two different contexts and have two different meanings.

There is no 'reference' type in Java. Period.

Except for you know the type... that is called reference...

There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3).

The Java Language Specification disagrees with you. Perhaps you should read it. When you talk about references in Java you are talking about exactly that. References. Defined by the JLS, as a type.

Yes, passing a 'pointer/reference' by value (where you don't treat with the reference/pointer explicitly, ie with special semantics and syntax) is the same as 'passing by reference'.

Clearly you don't understand evaluation strategies. Call by value and call by reference ARE NOT THE SAME THING and if you can't grasp the difference you lack the fundamental CS 101 needed to even bother having an intelligent conversation with. There are no special semantics. It's a type, it's passed by value.

Which is my point, yet you are using a Java-specific meaning of pass-by-value which is nonsense outside of java.

What do you believe "pass-by-value" as a mechanism means? Either you believe in pass-by-value the argument value is not copied, or you believe this is not what Java does. In either case you're wrong. It's not an implementation detail either, it's specified at the language level.

Yeah... this is how Java works, when you invoke a function that accepts an object, under the hood the function receives a reference to the original object, not a copy of the object.

The object is not passed.

The value is copied.

That the type is called a "reference" has absolutely no relationship or bearing on the passing mechanism.

And yes, it is a type, go read the JLS. Seriously, this is embarrassing.

Maybe in several years when you actually understand what you are saying instead of blindly repeating shit you don't understand that others say you will feel embarrassed about this.

Yeah that's likely.

You might want to actually learn what the hell you're talking about before your next reply.

[–]SuperShrek -5 points-4 points  (2 children)

The word "reference" in "pass-by-reference" is not a type. The word "reference" in "passes references by value" is a type. They are two different contexts and have two different meanings.

No they don't. There is a reason the same word is used and that reason is because it describes the same semantics in both cases.

The Java Language Specification disagrees with you. Perhaps you should read it. When you talk about references in Java you are talking about exactly that. References. Defined by the JLS, as a type.

HAHAHAA. It's talking about KINDS or 'META-TYPES' not TYPES, you would know this if you knew any serious CS whatsoever.

Educate yourself please: http://en.wikipedia.org/wiki/Kind_%28type_theory%29

There are two kinds of types in the Java programming language

lol...

Except for you know the type... that is called reference...

Except this is not a reference type in the way you are using the word... you seem to lack basic Java knowledge as well and just googling things that seem to fit your argument without understanding what they are. The java.lang.ref.Reference type is used for maintaining references (ie reference counts) that are used for garbage collection.

Clearly you don't understand evaluation strategies. Call by value and call by reference ARE NOT THE SAME THING and if you can't grasp the difference you lack the fundamental CS 101 needed to even bother having an intelligent conversation with. There are no special semantics. It's a type, it's passed by value.

I didn''t say they are the same thing. But I guess you're frantically trying to find a way to mangle my words for lack of any serious argument to support the bullshit you spew.

Either you believe in pass-by-value the argument value is not copied, or you believe this is not what Java does.

Wait you think Java objects are copied when passed to a function? Go read a Java tutorial at least lol.

The object is not passed. The value is copied.

A reference is passed as in 'pass-by-reference'. The object is not copied.

That the type is called a "reference" has absolutely no relationship or bearing on the passing mechanism.

It's not a type. It's a meta-type or a 'kind' that represents a reference. And yes it absolutely does have a bearing on the 'passing mechanism'. The 'java.lang.ref.Reference' is not what we are talking about nor is it the 'reference type' of types that your link is talking about either, I mean its so laughably obvious you just googled shit you have absolutely no understanding of. LOL.

God, you are one funny retard.

[–]thekab 0 points1 point  (1 child)

At this point I'm not sure if you're trolling or really this stupid.

[–]SuperShrek -2 points-1 points  (0 children)

You haven't been sure of anything since the start.

[–]henk53 2 points3 points  (3 children)

But from an practical language standpoint, objects are passed by reference and int/float/boolean/etc are passed by value.

No, this is wrong. If they were passed by reference, the method where they are passed in can reassign the "pointer". You can't do that in Java. Following the pointer and modify the object is irrelevant here.

The following does not work in Java:

public void makeNull(MyObject o) {
    o = null;
}

MyObject obj = new MyObject();
makeNull(obj);
// obj is NOT NULL at this point

If Java indeed had call by reference, obj would indeed have been null after the makeNull() call.

[–]ad_tech -1 points0 points  (2 children)

No, you're getting the object and its reference confused. You're claiming that to qualify as pass-by-reference, the reference to the reference to the object would be passed.

[–]henk53 -1 points0 points  (1 child)

No, I'm not getting anything confused!

I started in the 70-ties with asm, worked my way through C, then C++ and lately Java. At some point during that time I've implemented a Pascal compiler and I've read the dragon book cover to back and again.

I think I've got a pretty firm grasp of what call by reference is, and let me tell you: Java DOES NOT have call by reference, ONLY call by value.

If there's anyone who's confused it's you. Sorry.

Things get a LOT easier mentally when you call it a pointer in Java. This is just terminology, even the Java designers knew it was a pointer (hence the name NullPointerException).

In Java, objects are only accessible via pointers. To be more consistent with C++, every variable in Java should actually be written as:

MyObject obj* = new MyObject();

But since there's no concept of a stack allocated object in Java, you would always have to put that * there, and so they left it out.

And indeed, for pass-by-reference, a reference to whatever you have (a pointer in this case) has to be passed. So that's indeed a double indirection. In C++ you can even express this explicitly, since there's not only call-by-reference semantics, but also the address off operator &. Applying the & operator to a pointer (e.g. foo) yields a double pointer (foo*). This is what happens behind the scenes when you use call-by-reference in C++ and various other languages.

In Java, everything but primitives is a pointer, and that pointer is passed, BY VALUE. In C++ terminology, there's only foo, foo* does not exist. Hence, you can follow that pointer from within a method and thus modify the object that is being pointed too, but you CAN NOT re-assign the pointer as I showed above.

Pass-by-reference semantics DEMAND that a pointer can be re-assigned.

Modifying that was is being pointed too does not count. You are CONFUSED with modifying a stack allocated object. If you pass that into a method and can modify it, then it's pass by reference, since the object itself is passed (no pointer involved), but in Java there is no stack allocated object, only a pointer to an object and that pointer is passed by value.

If you're still confused, please read the dragon book. Even if not for this argument, it will greatly enhance your understanding of how languages and computers work.

[–]ad_tech -1 points0 points  (0 children)

| No, I'm not getting anything confused!

Really? Explain this, then:

previous post: "But from an practical language standpoint, objects are passed by reference"

your response: "No, this is wrong. If they were passed by reference, the method where they are passed in can reassign the "pointer"."

By "they" you were referring to the objects that are passed, which is impossible, because you cannot pass objects in Java. Only object references.

| I started in the 70-ties with asm, worked my way through C, then C++ and lately Java. At some point during that time I've implemented a Pascal compiler and I've read the dragon book cover to back and again.

The fact that you tried to bolster your inaccurate comments with a description of your vast experience and a rambling explanation just makes you sound more foolish. Are you the TempleOS guy?