News for hackers. Stuff that matters.
Re:Java is Pass-by-Value, Dammit by cbd1984 on Sunday March 22, 2015 @12:04AM (#9245530)
> Java passes "references by value". Meaning the target method receives a _copy_ of the pointer, not the actual pointer.

Which is exactly how C does it. I don't see how this is a difficult enough concept to grasp that we need an article explicitly pointing out how Java is just like C in this respect.

Re:Java is Pass-by-Value, Dammit by learc83 on Sunday March 22, 2015 @01:17AM (#9245691)
Not everyone knows C. A large percentage (maybe even the majority) of programmers who graduate from Java schools don't know C very well at all. For them, "it works just like C" is an insufficient explanation.
Re:Java is Pass-by-Value, Dammit by userbinator on Sunday March 22, 2015 @06:23AM (#9246262)
Or Asm, for that matter (likely even fewer programmers); at that level, basic concepts like memory addressing and pointers become almost "obvious facts of life".

As someone who started in Asm, I think pass-by-value/pass-by-reference are also intuitive there in the true "understood without needing any explanation" sense, since basically everything you'd pass to a procedure is a value of some sort, and it only depends on whether you are to interpret that value as a memory address.

Re:Java is Pass-by-Value, Dammit by Dewie on Sunday March 22, 2015 @07:57AM (#9246435)
> Or Asm, for that matter (likely even fewer programmers); at that level, basic concepts like memory addressing and pointers become almost "obvious facts of life".

In my intro-to-java-class, indirection was one of the first things we learnt when we learnt about objects: variables hold a reference to an object, not the object itself. I don't understand why pointers are somehow a great insight that is exclusive to lower-level programmers.

And if a java person ended up trying to make a method that swaps two primitive values, they'd have to learn about the distinction between passing a primitive value into a method as opposed to passing a reference to an object.

Re:Java is Pass-by-Value, Dammit by learc83 on Sunday March 22, 2015 @12:46PM (#9247395)
The problem is that most introductions stick with the primitives are pass by value, objects are pass by reference. Based on that introduction, they'd know you can't swap two primitives, but they'd think that you could swap two objects.

>I don't understand why pointers are somehow a great insight that is exclusive to lower-level programmers.

They're not. You don't need to be a lower level programmer to understand pointers, but it helps immensely if you know at least a bit of a low level language.

When you're getting into details like call-by-reference vs call-by-value, it really helps to have a mental model what the computer is doing. You don't need to be an assembly programmer, but it does help if you spend a little time studying assembly language.

Re:Java is Pass-by-Value, Dammit by tedunangst on Sunday March 22, 2015 @01:53AM (#9245765)
"It works just like Python"
Re:Java is Pass-by-Value, Dammit by ptx on Sunday March 22, 2015 @04:29PM (#9248094)
Exactly. Objects in Java work just like objects in Python: pointers passed by value. The difference is that everything in Python is an object, whereas Java additionally has primitive types where the value itself is passed by value.
Re:Java is Pass-by-Value, Dammit by rifung on Sunday March 22, 2015 @02:48AM (#9245894)
I don't know if that'd help or hurt, I've definitely seen people also say Python is pass by reference. It seems like they have the same problem.
Re:Java is Pass-by-Value, Dammit by _ZeD_ on Sunday March 22, 2015 @03:43AM (#9245972)
Storically, python devs says the python language use "pass-by-name"
Re:Java is Pass-by-Value, Dammit by icebraining on Sunday March 22, 2015 @06:13AM (#9246237)
How so? Reading [1] on pass-by-name, I don't see how it applies to Python, which evaluates the function arguments before passing them.

The example given would be, in Python,

  def double(x):
    x *= 2
  a = [4]
  double(a[0])
If Python was pass-by-name, a would now be [8], but it's actually still [4].

[1] http://www.cs.sfu.ca/~cameron/Teaching/383/PassByName.html

Re:Java is Pass-by-Value, Dammit by Dylan16807 on Sunday March 22, 2015 @12:10AM (#9245546)
Because in C you have to specifically ask for something to be a pointer. "pass by reference" is an incorrect phrasing, but at least it's an attempt. There is a crucial distinction between Java and C.

And C++ shows actual reference passing.

Re:Java is Pass-by-Value, Dammit by cbd1984 on Sunday March 22, 2015 @02:13AM (#9245822)
> "pass by reference" is an incorrect phrasing

No, it isn't.

In C, a pointer is a reference. It supports arithmetic, but it works like a reference as well, so the name applies.

What might confuse people is that in C, you do have to specifically ask for a reference to something, whereas in Java, everything which isn't a primitive type is already a reference.

It's true your code can't dereference those references such that it gets access to all of the data in a List object, for example. C works the same way, again: It's entirely possible to say something like 'typedef struct foo mfoo;' and then declare all of your functions to take pointers to type mfoo, and client code will never be able to get its hands on a struct foo object. FILE pointers work just like this, for example.

Re:Java is Pass-by-Value, Dammit by rifung on Sunday March 22, 2015 @02:44AM (#9245884)
I don't think a pointer in C is really a reference though?

If you pass a pointer to a function, then you are saying, hey allocate a new variable to put on the stack which is a pointer. If you pass a reference, then at the language level, you are saying hey I want that function to use THIS variable that I already have. While the implementation may certainly be free to use a pointer, they are not fundamentally the same concepts.

Or so I think.

Re:Java is Pass-by-Value, Dammit by nknighthb on Sunday March 22, 2015 @07:43AM (#9246419)
A pointer in C is a reference (see ISO 9899:1999 § 6.2.5) under siege by nasal demons (see § 6.3.2.3). The implementation isn't really required to do many of the things people assume it is.

(It's ironic, by the way, that in seeking to be more pedantic, you reference stacks. Stacks are not part of C.)

Re:Java is Pass-by-Value, Dammit by jjnoakes on Sunday March 22, 2015 @10:35AM (#9246844)
A C pointer is not a reference in the way we are using the word reference in this discussion (as in pass-by-reference, as in an alias for another value without applying any operators).

Sure, the C standard uses the English word "reference", as in "refers to" and "referred to", but letting that confuse you without understanding the meaning of the word in context is what lead to this Java mess in the first place. Don't bring C into it too.

Re:Java is Pass-by-Value, Dammit by nknighthb on Sunday March 22, 2015 @08:32PM (#9248852)
I'm not confused at all, thank you. I'm of the long-standing and well-informed opinion that "pass-by-reference" and "pass-by-value" are worthless terms that need to die, and that this entire conversation is an exercise in useless academic pedantry.
Re:Java is Pass-by-Value, Dammit by jjnoakes on Sunday March 22, 2015 @09:17PM (#9248978)
Based on your previous reply, you are indeed confused. Reference is a long-established term, and using it to refer to C pointers is typically considered incorrect.

In addition, pass-by-reference and pass-by-value are very useful terms for anyone interested in accurately speaking about and understanding what will happen when they run the program they write.

There are people who consider accuracy and understanding to be pedantry. More often than not, in my experience, they are a larger source of bugs than those who strive to truly understand how their tools work.

Re:Java is Pass-by-Value, Dammit by nknighthb on Monday March 23, 2015 @12:45AM (#9249531)
> Reference is a long-established term, and using it to refer to C pointers is typically considered incorrect.

I am not confused, I disagree.

> In addition, pass-by-reference and pass-by-value are very useful terms for anyone interested in accurately speaking about and understanding what will happen when they run the program they write.

The terms would be useful if people agreed on their meaning (they do not) and if languages actually followed one way or the other consistently (they do not). That is the very reason this thread exists.

If you wish to accurately speak about what will happen, then speak about it. Describe what actually happens, instead of spending your time arguing about which round hole the square peg fits in better.

> There are people who consider accuracy and understanding to be pedantry.

In practice, pass-by-value and pass-by-reference provide neither accuracy nor understanding. Stop using them, and these arguments instantly vanish.

> More often than not, in my experience, they are a larger source of bugs than those who strive to truly understand how their tools work.

That I strive to truly understand how my tools work is the very reason I reject these terms as worthless.

Re:Java is Pass-by-Value, Dammit by jjnoakes on Monday March 23, 2015 @03:21PM (#9252844)
> The terms would be useful if people agreed on their meaning (they do not)

Most people agree on the meaning (read the JSL - it says Java is pass-by-value. Read Wikipedia too. Or really any other real reference that isn't some confused people in a forum.)

> If you wish to accurately speak about what will happen, then speak about it

That's what the article does. That's what the JSL does. People in these forums who still don't get it need something more. That's what I'm giving them.

Re:Java is Pass-by-Value, Dammit by nknighthb on Monday March 23, 2015 @05:21PM (#9253418)
> Most people agree on the meaning (read the JSL - it says Java is pass-by-value.

I assume you mean the JLS, in which case it -- very wisely -- does not use the term "pass-by-value" (or any similar term I've seen) to label its semantics. It simply describes its semantics.

> Read Wikipedia too. Or really any other real reference that isn't some confused people in a forum.)

So is it "most people" or a reference that, if anything, supports my point (JLS), an un-encyclopedia, and unspecified "real references" that aren't from people you enjoy insulting?

> That's what the article does.

That's one thing the article does, yes. Why is it you think you need to point that out?

> People in these forums who still don't get it need something more. That's what I'm giving them.

I agree you are giving people in this forum "something more". I suspect we disagree as to what that "something" is.

Re:Java is Pass-by-Value, Dammit by jjnoakes on Monday March 23, 2015 @06:12PM (#9253656)
> The JLS doesn't use pass-by-value

Well, maybe not the exact letters "p-a-s-s-b-y-v-a-l-u-e" in a row like that, but sometimes you have to read and comprehend.

The JLS does define exactly what happens when parameters are passed...

http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html... http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#...

And that happens to match the definition of pass-by-value/call-by-value exactly (even Java is listed as an example language for pass-by-value)

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

> people you enjoy insulting

I haven't insulted anyone, have I? At least, not intentionally. No name-calling, sticking to the facts... I'm sorry if that somehow offends you. Some of us appreciate correctness and don't attribute the chase of it as ad hominem when it isn't.

Re:Java is Pass-by-Value, Dammit by nknighthb on Monday March 23, 2015 @06:17PM (#9253680)
> sometimes you have to read and comprehend

If you don't understand how you're being insulting here, I can't help you.

> The JLS does define exactly what happens when parameters are passed...

Yes, it's very nice in that respect.

> And that happens to match the definition of pass-by-value/call-by-value exactly (even Java is listed as an example language for pass-by-value)

It happens to match the definition of "call-by-value" used by whoever wrote that blurb on Wikipedia you seem to love so much. The irony here, of course, is that people who like to use these terms don't even agree on how they're spelled...

None of this makes the terms useful.

Re:Java is Pass-by-Value, Dammit by jjnoakes on Monday March 23, 2015 @07:46PM (#9254058)
If you google "pass-by-value" vs "pass-by-reference" and understand what they mean, in a language-agnostic way, and you understand what Java is doing when you pass parameters, then you too will come to the same conclusion and I have, and the people who wrote the wikipedia article, and everyone else.

If you like, though, feel free to provide your plethora of references and documentation that supports your claim that pass-by-reference is meaningless, and is also what Java does.

> If you don't understand how you're being insulting here, I can't help you.

There's no help needed. Asking someone to read and comprehend is no more insulting than pointing out where they are wrong. If being wrong insults you, then I suggest you refrain from posting misguided opinions in technical forums and standing by your incorrect position even in the face of overwhelming opposition from numerous sources.

If I was being insulting, I'd link you to the proggit thread and say "look, even proggit got it after a short discussion, why are the hacker news folks taking so long to get this simple concept"... but I haven't. Instead I would like to try to correct those who are mistaken. I feel like anyone who comes and reads the incorrect statements in threads like this should be able to then read the corrections so they too don't fall into the same traps you have.

Re:Java is Pass-by-Value, Dammit by nknighthb on Monday March 23, 2015 @08:23PM (#9254183)
> If you google "pass-by-value" vs "pass-by-reference" and understand what they mean, in a language-agnostic way, and you understand what Java is doing when you pass parameters, then you too will come to the same conclusion and I have, and the people who wrote the wikipedia article, and everyone else.

What conclusion would that be, exactly?

> your claim that pass-by-reference is meaningless

I didn't make that claim.

> and is also what Java does

I didn't make that claim, either. Nor would I ever. Stop putting words in my mouth.

Re:Java is Pass-by-Value, Dammit by jjnoakes on Monday March 23, 2015 @09:34PM (#9254412)
You did though. You claimed the terms were meaningless.

But if you study, you find out they they have very specific meanings.

Calling C pointers "references" is just the tip of the misunderstanding iceberg.

But if you prefer to wander around in ignorance, by all means. Just don't be surprised when your incorrect comments are constantly corrected.

Re:Java is Pass-by-Value, Dammit by nknighthb on Monday March 23, 2015 @09:58PM (#9254467)
> You did though. You claimed the terms were meaningless.

Just to be sure I didn't comment in my sleep, I went back and looked, and I see the word "meaningless" nowhere in my comments.

> But if you study, you find out they they have very specific meanings.

Some people have defined them in very specific ways. The description in the originally linked article is actually one of the ones I hate the least. That doesn't make it good, and doesn't make the terms useful.

> Calling C pointers "references" is just the tip of the misunderstanding iceberg.

What, precisely, will it take to convince you that I understand your position and simply disagree with it?

> But if you prefer to wander around in ignorance

Please stop being insulting.

Re:Java is Pass-by-Value, Dammit by jjnoakes on Monday March 23, 2015 @11:12PM (#9254689)
> I'm of the long-standing and well-informed opinion that "pass-by-reference" and "pass-by-value" are worthless terms that need to die

> The terms would be useful if people agreed on their meaning (they do not) and if languages actually followed one way or the other consistently (they do not).

Go read some actual literature about programming languages so you can get these ignorant notions out if your head.

The terms pass-by-value and pass-by-reference are well defined and have specific meanings both in the context of specific programming languages and independent of any specific programming language.

Pass by value means the value of the caller expression is logically copied in to the parameters, and direct mutations to the parameter itself are not reflected back to the caller.

Pass by reference means the opposite... The parameter is an alias for the caller's expression, and direct mutations are reflected back to the caller.

C pointers are not called references, except in an informal description of how they relate to what they refer to, by any authority on the topic.

I don't know if you are doing it intentionally or not but I'm done feeding your trolling. If you change your mind and want to learn anything else, my door is always open. But if you want to keep up this charade, whatever it is, go troll someone else. No one is going to read this thread this far down, so I feel no more obligation to correct your unfortunate misguided claims in the spirit of helping someone else who is actually looking for the right answer.

Re:Java is Pass-by-Value, Dammit by nknighthb on Monday March 23, 2015 @11:22PM (#9254722)
> If you change your mind and want to learn anything else, my door is always open.

I can't imagine what could lead you to think I'd be interested in "learning" from someone who clearly doesn't even understand what my claims/opinions are, but insists they're "ignorant" and "trolling".

What you've accomplished here is not to teach, but to harden my existing biases.

Re:Java is Pass-by-Value, Dammit by jjnoakes on Tuesday March 24, 2015 @07:39AM (#9255937)
Even the best of us can't help the unhelpable. There has to be at least a basic willingness to listen and change.

But it's no skin of my back. My motivation was less about helping you (it was clear from the start that was probably futile) but to instead leave the right answers where you left wrong ones so others who are willing to learn can find what they need to succeed.

This far down, as I said, that no longer applies.

Good day.

Re:Java is Pass-by-Value, Dammit by mannykannot on Sunday March 29, 2015 @09:57AM (#9284821)
You may be amused to learn that Mr. Noakes' sanctimonious rudeness has got him into a spot of trouble in another thread here.
Re:Java is Pass-by-Value, Dammit by rifung on Sunday March 22, 2015 @02:42PM (#9247759)
Ah good point about the stacks. However, I think the point still stands.. an argument being a pointer just defines its type, while an argument being a reference does not define its type, but that it's an alias to another variable.

Again, I think the issue is with terminology as someone else pointed out. Certainly a C pointer can be considered a reference when using the normal English definition of a reference, but "pass by reference" has a specific meaning, and in C you can't "pass by reference"

Re:Java is Pass-by-Value, Dammit by DonHopkins on Sunday March 22, 2015 @07:37AM (#9246409)
What something IS is independent of how you PASS it. An integer isn't a reference, but you can pass a reference to an integer in C++ (to implement swap, for example), while you can't do that in Java.
Re:Java is Pass-by-Value, Dammit by learc83 on Sunday March 22, 2015 @12:51PM (#9247414)
Pass by reference has a specific meaning and specific consequences.

In C to be able to simulate some aspects of pas-by-reference you need a double pointer.

Re:Java is Pass-by-Value, Dammit by slowmovintarget on Sunday March 22, 2015 @12:54AM (#9245650)
Well, he also has articles on tips for using Visual Age for Java, and a copyright notice starting in 1996, so perhaps this is yet another blast from the past that made it to HN's front page.

Because that particular argument used to be a thing.

Re:Java is Pass-by-Value, Dammit by jacquesm on Sunday March 22, 2015 @02:17AM (#9245834)
Last-Modified: Thu, 05 Mar 2015 18:22:25 GMT
Re:Java is Pass-by-Value, Dammit by slowmovintarget on Sunday March 22, 2015 @02:37AM (#9245871)
First version of the article is at least from 2002: http://web.archive.org/web/20020109181501/http://javadude.co...

Good that it's kept crisp.

Re:Java is Pass-by-Value, Dammit by have_humility on Sunday March 22, 2015 @02:57AM (#9245911)
Utterly unreliable for determining the age of an article.
Re:Java is Pass-by-Value, Dammit by mannykannot on Sunday March 22, 2015 @10:49AM (#9246887)
The number of people who don't get it, or don't realize it is different from actual pass-by-reference and that the difference has actual consequences, is reason enough for an article.

In my experience, imprecise semantics and the misunderstandings that arise from them are a/the major cause of error, in both the requirements and implementation domains.

Re:Java is Pass-by-Value, Dammit by kelnos on Sunday March 22, 2015 @04:01AM (#9245998)
I think the confusion comes from one main place.

C has the address-of operator (&). So you can pass the address of a pointer, change what that points to in a function, and then the variable outside the function has indeed changed to point to a new chunk of memory. Java only has primitives and pointers to objects, and it only allows you to pass those by value to functions. You can't take the address of a primitive or object and pass that.

So basically in C you can pass an object (struct) or even primitive by reference... or at least you can fake it with trivial syntax. In Java you just cannot do that, and I think Java's use of the term "reference" to mean "pointer" makes some people (understandably) confused.

Re:Java is Pass-by-Value, Dammit by lucio on Sunday March 22, 2015 @09:50AM (#9246722)
>You can't take the address of a primitive or object and pass that.

Not correct.

In Java, if you have an object you take the address and pass it. If you have a primitive, you pass a copy of the value. Both things are called "pass-by-value" (something that confuses a lot of people).

Re:Java is Pass-by-Value, Dammit by jjnoakes on Sunday March 22, 2015 @11:00AM (#9246928)
Java has no notion of "taking the address of an object" because you never have an expression that refers to an object directly. You only have the addresses, so there's no need (or way) to convert objects to their address.

And it's all pass by value (for all values you could have - numbers, addresses, booleans, whatever).

Re:Java is Pass-by-Value, Dammit by kelnos on Wednesday March 25, 2015 @09:04PM (#9267166)
Heh, see why this is so confusing? That is of course what I meant -- it's really that Java, passing-wise, just has primitives and addresses to objects.