Answers.Body: <p>I've encountered experienced C and C++ developers who are convinced that Java is pass-by-reference for objects. One example I've used is this one:</p>
  
  <pre><code>void changeInt(int x){
    x = 42;
  }
  
  void changeString(String x){
    x = "hello world";
  }
  
  void testChanges(){
    int i = 0;
    changeInt(i);
    System.out.println(i); // still 0
  
    String s = "test";
    changeString(s);
    System.out.println(s); //still test
  }
  </code></pre>
  
  <p>The <code>changeInt()</code> function fails to change the value of the parameter, because it's pass-by-value. But then the <code>changeString()</code> function also fails to change the value of the parameter- because it's also pass-by-value.</p>
  
  <p>I also really like <a href="https://javaranch.com/campfire/StoryCups.jsp" rel="noreferrer">Cup Size -- a story about variables</a> and its follow-up <a href="https://javaranch.com/campfire/StoryPassBy.jsp" rel="noreferrer">Pass-by-Value Please</a> to explain the difference between pass-by-value and pass-by-reference from a Java perspective.</p>
  
  <p>Now, as for whether you actually bring this up to your teacher, I'm not sure. If I were you I'd probably just bite my tongue and move on with my life.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-04T20:51:41.283Z
Answers.Text: Have you done this with other class objects, e.g. `class Foo { int x; string s; }` and `changeFoo(Foo x){ x.t = "Hello World!"; }`? $100 says that your original Foo now contains the string `"Hello World!"`
Answers.UserDisplayName: Draco18s no longer trusts SE
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-04T21:48:21.987Z
Answers.Text: @Draco18s Okay, and I'm not sure what the point is? I'm only talking about changing the reference of the argument. Are you trying to say that your code proves that Java is pass-by-reference? Because it doesn't.
Answers.UserDisplayName: Kevin Workman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-09T15:43:59.800Z
Answers.Text: You make a great point! I think I need to incorporate it into my answer. In any case, I just voted yours up above mine. :-)
Answers.UserDisplayName: Ellen Spertus
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-11-13T13:48:29.490Z
Answers.Text: Good enough for the Java Community on their Island.
Answers.UserDisplayName: Sam Ginrich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-15T23:29:30.393Z
Answers.LastActivityDate: 2018-03-15T23:29:30.393Z
Answers.UserDisplayName: Kevin Workman
Answers.Body: <p>In an introductory Java class, I think it's fine to say that primitives are passed by value and that objects are passed by reference. That tells students what they need to know, although I might phrase it differently. (I'd say that primitives and references are copied when they're passed.)</p>
  
  <p>In an upper-division programming languages course, I'd go into more detail about calling conventions and the full story about pass-by-value and pass-by-reference (and pass-by-name, so I can tell <a href="http://wiki.freepascal.org/Niklaus_Wirth" rel="nofollow noreferrer">the joke about Niklaus Wirth</a>), if there's time.</p>
  
  <p>While I love for students to catch my mistakes and white lies, I think it might be nitpicky to do so in this case. If you feel a need to bring it up, go to the professor's office hours and express confusion reconciling what he said with what you've read online (perhaps citing a specific source). If he's a decent professor, he'll admit to having oversimplified or he'll accept the new knowledge.</p>
  
  <p><strong>Update</strong></p>
  
  <p>After reading <a href="https://cseducators.stackexchange.com/a/4490/283">Kevin Workman's answer</a>, I see the problem with using the term "call by reference". I agree that no good can come from misusing that term. My second and third paragraphs still stand.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-15T17:39:42.287Z
Answers.Text: Of course, one hopes that the prof knows that she/he over simplified. Still, I'd warn the students at the time that I'm lying/simplifying/...
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-16T02:31:54.243Z
Answers.Text: “For every complex problem there is an answer that is clear, simple, and wrong.” -- H.L. Mencken.  There are too many Java tyros out there---some of them writing real-world, commercial code---who do not understand the difference between an instance and a variable.  Telling them that Java "passes objects by reference" may be easier than telling them the deeper truth, but it does not help them in the long run.
Answers.UserDisplayName: Solomon Slow
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-16T02:53:37.790Z
Answers.Text: Wait, so, what's the joke?
Answers.UserDisplayName: Ben I.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-16T13:26:14.347Z
Answers.Text: @BenI., Read all the way down to the last sentence on the page. Niklaus Wirth... Call by name... Call by value...
Answers.UserDisplayName: Solomon Slow
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-03-15T17:24:47.600Z
Answers.LastActivityDate: 2018-10-09T15:47:21.853Z
Answers.LastEditDate: 2018-10-09T15:47:21.853Z
Answers.UserDisplayName: Ellen Spertus
Answers.Body: <p>It may well be an intentional white lie. This suddenly reminds me back when over 20 years ago, in the department coffee room one professor said that it is better to flat out intentionally lie to oversimplify in the beginning. The students, overwhelmed with all these new and alien concepts already, cannot possibly have the capacity to follow and understand the complete truth at that point anyway. They must first understand the general and important ideas that we veterans take for granted and forget how difficult those ideas really are, before they are able to understand the nitty gritty details of the complete truth.</p>
  
  <p>Back then the grad student me just couldn't understand why he would do that, although I did wisely hold my tongue. And now having taught intro Java myself for fifteen years, at some point I tell my students that I do exactly this, and one of these semesters (as usual, it is always the next one) I am going to carry with me one of those old time clicker counters and press it every time I tell such a white lie, and at the end of the course it should show a value of about thirty.</p>
  
  <p>For example, in the beginning when we write the first classes and methods, I say something like "When you write a Java method, the order in which you write its statements is absolutely essential, because changing the order of two statements in a million line program could totally change the behaviour of not just that one method but the entire program. However, the order in which you write the fields and methods in the class can never make a difference to anything."</p>
  
  <p>Click. Of course it can, since initializers are executed in the order of declaration. (And this order would probably affect the results of reflection also... but as I like to say in my classes whenever any questions of this nature spontaneously emerge, I type the question in Google search box and say "O Stack Overflow, enlighten us again with your wisdom" before pressing the return key to make students chuckle when I click the first link of the results page to read the Stack Overflow answer.) But at that point, it is enormously more important to make the students fully understand that methods are not executed in the order that they are written, but in the order in which the outside world invokes them. Any minor technical inaccuracy that I need to do to make them understand this is well worth the price.</p>
  
  <p>Remember that we are dealing with people who need to be explained the difference between returning a value from the method versus printing it on the console.</p>
  
  <p>When we get to arrays, I don't use the terms "pass by value" and "pass by reference", but simply say that when giving an array object as argument to a method, the method receives only the memory address where that array object is stored. The caller and the method therefore share the very same object, so any modifications that the method makes to the array object will remain there after the control has returned to the caller. And then compare the example methods</p>
  
  <pre><code>public int[] reverse(int[] a)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public void reverseInPlace(int[] a)
  </code></pre>
  
  <p>In the last lecture of the course, I go through a bunch of interesting and important and interesting little details that I tell them would logically have belonged to the earlier lectures, but at that point would have only confused them. But now, having worked through all my labs and listened to the previous lectures, they are ready to learn and understand them. One of those little details just so happens to be the computer science terms "pass by value" and "pass by reference" (and why not take a peek at Wikipedia page "<a href="https://en.wikipedia.org/wiki/Evaluation_strategy" rel="nofollow noreferrer">Evaluation strategy</a>" while we are at it, to give them a quick sneak preview of how programming paradigms other than that of Java really exist out there), which inspired me to write this response when I saw this question.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-15T13:20:20.043Z
Answers.Text: I have occasionally lied to students about some technical detail, but always, immediately, in the next sentence, tell them I lied so that they can note it and follow up when the truth is available. But you have to warn them, ethically. Misconceptions can be hard to erase and the follow up might be missed for one reason or another. So, if you lie, do it _honestly_. You should never put a student in the position of needing to _unlearn_ something.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-15T12:27:13.240Z
Answers.LastActivityDate: 2018-03-15T12:27:13.240Z
Answers.UserDisplayName: Ilkka Kokkarinen
Answers.Body: <p>The correct statement, of course is that Java passes a reference by value. That is not the same as passing by reference. You can easily show the difference, by assigning a new object to the parameter variable within the method and then noticing that the code that invoked the method doesn't see the new object. It would be pass-by-reference only if the caller sees the new object. </p>
  
  <p>The confusion comes from the fact that the objects themselves are usually mutable and so within the called method, the receiver gets to make modifications. But both the caller and the called method have references to the same object. So the caller can see changes within the object made by the method. But it can't see a new object that replaces the parameter. </p>
  
  <p>However, if you assign to the parameter variable within the method, then the caller and the called method will see different objects. </p>
  
  <p>Yes, you should correct the teacher, but it is a bit risky, depending on the personality of the teacher. The best way, perhaps, is to prepare some demonstration code and show it to the teacher in the office. You can then, perhaps, offer to <em>clarify</em> the real meaning in Java, using your code. </p>
  
  <p>Personalities can be messy, of course, and not every teacher likes to be contradicted. But the students have a right to know the "real deal" if they are going to program in Java. </p>
  
  <p>Notice that Java and C++ are superficially similar, but deeply different. Learning both together is, itself, a cause of confusion. But in this case, the contrast can be informative. </p>
  
  <p>If nothing else, you can show the teacher this post from a 40 year veteran of programming, much of that in OO languages, including both C++ and Java (and author of books in both). </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-14T11:18:08.420Z
Answers.Text: It is amazing how many people get this distinction wrong, and how people can write correct code which, when you press them, their understanding contradicts completely! As always when this discussion crops up, I'll cite how much I love function definitions in FORTRAN, where you can explicitly state `IN`, `OUT`, or `INOUT`, and nobody has to worry about implicit semantics.
Answers.UserDisplayName: VisualMelon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-13T19:29:49.487Z
Answers.LastActivityDate: 2018-03-13T19:29:49.487Z
Answers.UserDisplayName: Buffy
Answers.Body: <p>This is a tricky situation for you.  You are correct that Java does not pass by reference, but your approach must depend on the personality of your professor.</p>
  <p>If the prof seems like the sort of person who would warmly accept this sort of feedback, I might directly mention it to them.  (I have done so, and it has been fine, though it often takes an exceptionally well-adjusted person to be able to take such feedback gracefully.)</p>
  <p>A second option would be to raise your hand and ask about the difference between pass by reference, and passing references by value.  This might trigger the prof to simply self-correct.</p>
  <p>A third approach would be to email the professor anonymously.  This is a generally a jerk move, and not recommended.</p>
  <p>A fourth approach, which you have alluded to in your question, would be to pose the question as a pedagogic one.  &quot;Professor, I noticed that you told the class that Java passed Objects by reference.  Was there some reason you didn't want to mention passing references by value?  Was that just to avoid confusing people?&quot;</p>
  <p>As to whether there could be a pedagogical advantage to distinguishing the way that primitives and Objects are passed in a rough way, my instinct from years of teaching this topic is that there could be.  I object to using the specific words &quot;pass by reference&quot; (because those words have a specific meaning, and don't apply here), but pointing out that there is a distinction right at the start is important.  The behaviors of the two types of parameters will appear to be quite different to the beginner.</p>
  <p>When I first introduce the topic, I <strong>simply want to call attention to the fact that something is going on here that the student needs to pay attention to</strong>, and I will be less concerned with whether every detail is understood at the start.  Your professor could, indeed, be (somewhat clumsily) applying such a strategy.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-03-13T19:54:14.233Z
Answers.LastActivityDate: 2021-01-21T15:06:22.267Z
Answers.LastEditDate: 2021-01-21T15:06:22.267Z
Answers.UserDisplayName: Ben I.
Answers.Body: <p>The usual "explanation" like "Java passes primitive types by value and other by reference" is rather confusing.</p>
  
  <p>In languages like C, C++, Fortran,  etc. variables <strong>contain</strong> data. Then a variable is a chunk of memory with a name.</p>
  
  <p>The explanation of parameter passing by value or reference) doesn't work well for languages (Java, PHP, Lisp, Python etc) where variables are names for <strong>references</strong> to data sitting somewhere in memory.</p>
  
  <p>What is transmitted is the reference to the data. In all cases.
  The explanation fits well for primitive type : in</p>
  
  <pre><code>int n = 3;
  </code></pre>
  
  <p>consider n as holding a reference to an immutable constant integer value. And a call to <code>foo(n)</code> transmits the reference to that constant.</p>
  
  <p>Of course, it happens that the variables of primitive types are not really implemented as variables containing references to objects. It's a tradeoff for efficiency in Java. </p>
  
  <p>We'd better avoid using concepts which are pertinent for some  - historically predominant - Fortran, Cobol, Algol, Pascal ...) langages to another category where they are much less adequate.</p>
  
  <p>Not all languages have PICTURE and USAGE like COBOL. So we don't use these concepts to explain other programming languages.</p>
  
  <p>EDIT: maybe the explanations about parameter passing focus too much on passing <em>variables</em>.  I remember old-time FORTRAN where the statement</p>
  
  <pre><code>  call FOO(2+2)
  </code></pre>
  
  <p>could modify some constant in memory :-)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-16T11:30:51.980Z
Answers.LastActivityDate: 2018-03-19T08:54:34.303Z
Answers.LastEditDate: 2018-03-19T08:54:34.303Z
Answers.UserDisplayName: Michel Billaud
Answers.Body: <p>The proper way to think about this is to ask, "What is a variable storing?"  Variables of primitive type store a bit pattern that represents their datum.  Variables of object type store the memory address of the object they are pointing at on the heap.  </p>
  
  <p>When you pass a variable to a function, a copy of the value it holds is made and is placed in the function's local symbol table.  This occurs whether the variable is of object or primitive type. Thinking this way, what we have here is pass by value.  </p>
  
  <p>If you pass a variable pointing at a mutable object and the callee invokes a mutator method, the state of the object residing on the heap will be changed.  If the callee reassigns the variable, there is no effect. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-11-13T13:04:16.340Z
Answers.Text: This was commen understanding before the Java Island felt like hijacking Computer Science and pasting all kind of platforms with it's dogmatism on base of a langauge that does not even know de-referenciation. Think the development started with the authors of C calling a pointer "value".
Answers.UserDisplayName: Sam Ginrich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-18T21:54:14.950Z
Answers.LastActivityDate: 2018-03-18T21:54:14.950Z
Answers.UserDisplayName: ncmathsadist
Body: <p>I am an undergrad student. I am an experienced programmer. For reasons out of the scope of this question, I ended up in a class where basic things about C++ and Java are being taught (but I already know those).</p>
  
  <p>I know very well the difference between "pass-by-value" and "pass-by-reference". I know very well that Java is "pass-by-value".</p>
  
  <p>My teacher was talking about pointers and a bit later he showed an example of pass-by-reference in C++, comparing passing "by pointer", by value and by reference. So far so good.</p>
  
  <p>But then he said "on the other hand, Java has a rule, primitive types are always passed by value, and non-primitive types are automatically passed by reference".</p>
  
  <p>My question: I know that technically what he said is wrong. But I didn't say anything. No one else said anything, the other students appeared to "learn" that. I would like to have corrected him, but how could I have done so? Alternatively, is there a good education reason to teach this way, perhaps a "white lie" of some sort, to avoid overwhelming students with details?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-03-13T19:31:18.637Z
Comments.Text: And welcome to CSEducators. Bring your teacher here, too. You pose a common dilemma for students that is both thought provoking and fraught.
Comments.UserDisplayName: Buffy
Tags: <java>
Title: My teacher said that java is pass-by-reference when the type is not a primitive
ThreadText: # My teacher said that java is pass-by-reference when the type is not a primitive
  
  (Asked by: username_0 on 2018-03-13)
  
  I am an undergrad student. I am an experienced programmer. For reasons out of the scope of this question, I ended up in a class where basic things about C++ and Java are being taught (but I already know those).
  
  I know very well the difference between "pass-by-value" and "pass-by-reference". I know very well that Java is "pass-by-value".
  
  My teacher was talking about pointers and a bit later he showed an example of pass-by-reference in C++, comparing passing "by pointer", by value and by reference. So far so good.
  
  But then he said "on the other hand, Java has a rule, primitive types are always passed by value, and non-primitive types are automatically passed by reference".
  
  My question: I know that technically what he said is wrong. But I didn't say anything. No one else said anything, the other students appeared to "learn" that. I would like to have corrected him, but how could I have done so? Alternatively, is there a good education reason to teach this way, perhaps a "white lie" of some sort, to avoid overwhelming students with details?
  
  **Tags:** java
  
  > **Comments:**
  > **username_1**: And welcome to CSEducators. Bring your teacher here, too. You pose a common dilemma for students that is both thought provoking and fraught.  
  
  ---
  
  ## 7 answers
  
  ### Answer by username_2 on 2018-03-15. Score: 5
  
  I've encountered experienced C and C++ developers who are convinced that Java is pass-by-reference for objects. One example I've used is this one:
  
  ```
  void changeInt(int x){
    x = 42;
  }
  
  void changeString(String x){
    x = "hello world";
  }
  
  void testChanges(){
    int i = 0;
    changeInt(i);
    System.out.println(i); // still 0
  
    String s = "test";
    changeString(s);
    System.out.println(s); //still test
  }
  
  ```
  
  The `changeInt()` function fails to change the value of the parameter, because it's pass-by-value. But then the `changeString()` function also fails to change the value of the parameter- because it's also pass-by-value.
  
  I also really like [Cup Size -- a story about variables](https://javaranch.com/campfire/StoryCups.jsp) and its follow-up [Pass-by-Value Please](https://javaranch.com/campfire/StoryPassBy.jsp) to explain the difference between pass-by-value and pass-by-reference from a Java perspective.
  
  Now, as for whether you actually bring this up to your teacher, I'm not sure. If I were you I'd probably just bite my tongue and move on with my life.
  
  > **Comments:**
  > **username_3**: Have you done this with other class objects, e.g. `class Foo { int x; string s; }` and `changeFoo(Foo x){ x.t = "Hello World!"; }`? $100 says that your original Foo now contains the string `"Hello World!"`  
  > **username_2**: @username_3 Okay, and I'm not sure what the point is? I'm only talking about changing the reference of the argument. Are you trying to say that your code proves that Java is pass-by-reference? Because it doesn't.  
  > **username_4**: You make a great point! I think I need to incorporate it into my answer. In any case, I just voted yours up above mine. :-)  
  > **username_5**: Good enough for the Java Community on their Island.  
  
  ---
  
  ### Answer by username_4 on 2018-03-15. Score: 4
  
  In an introductory Java class, I think it's fine to say that primitives are passed by value and that objects are passed by reference. That tells students what they need to know, although I might phrase it differently. (I'd say that primitives and references are copied when they're passed.)
  
  In an upper-division programming languages course, I'd go into more detail about calling conventions and the full story about pass-by-value and pass-by-reference (and pass-by-name, so I can tell [the joke about Niklaus Wirth](http://wiki.freepascal.org/Niklaus_Wirth)), if there's time.
  
  While I love for students to catch my mistakes and white lies, I think it might be nitpicky to do so in this case. If you feel a need to bring it up, go to the professor's office hours and express confusion reconciling what he said with what you've read online (perhaps citing a specific source). If he's a decent professor, he'll admit to having oversimplified or he'll accept the new knowledge.
  
  **Update**
  
  After reading [username_2's answer](https://cseducators.stackexchange.com/a/4490/283), I see the problem with using the term "call by reference". I agree that no good can come from misusing that term. My second and third paragraphs still stand.
  
  > **Comments:**
  > **username_1**: Of course, one hopes that the prof knows that she/he over simplified. Still, I'd warn the students at the time that I'm lying/simplifying/...  
  > **username_6**: “For every complex problem there is an answer that is clear, simple, and wrong.” -- H.L. Mencken.  There are too many Java tyros out there---some of them writing real-world, commercial code---who do not understand the difference between an instance and a variable.  Telling them that Java "passes objects by reference" may be easier than telling them the deeper truth, but it does not help them in the long run.  
  > **username_7**: Wait, so, what's the joke?  
  > **username_6**: @BenI., Read all the way down to the last sentence on the page. Niklaus Wirth... Call by name... Call by value...  
  
  ---
  
  ### Answer by username_8 on 2018-03-15. Score: 2
  
  It may well be an intentional white lie. This suddenly reminds me back when over 20 years ago, in the department coffee room one professor said that it is better to flat out intentionally lie to oversimplify in the beginning. The students, overwhelmed with all these new and alien concepts already, cannot possibly have the capacity to follow and understand the complete truth at that point anyway. They must first understand the general and important ideas that we veterans take for granted and forget how difficult those ideas really are, before they are able to understand the nitty gritty details of the complete truth.
  
  Back then the grad student me just couldn't understand why he would do that, although I did wisely hold my tongue. And now having taught intro Java myself for fifteen years, at some point I tell my students that I do exactly this, and one of these semesters (as usual, it is always the next one) I am going to carry with me one of those old time clicker counters and press it every time I tell such a white lie, and at the end of the course it should show a value of about thirty.
  
  For example, in the beginning when we write the first classes and methods, I say something like "When you write a Java method, the order in which you write its statements is absolutely essential, because changing the order of two statements in a million line program could totally change the behaviour of not just that one method but the entire program. However, the order in which you write the fields and methods in the class can never make a difference to anything."
  
  Click. Of course it can, since initializers are executed in the order of declaration. (And this order would probably affect the results of reflection also... but as I like to say in my classes whenever any questions of this nature spontaneously emerge, I type the question in Google search box and say "O Stack Overflow, enlighten us again with your wisdom" before pressing the return key to make students chuckle when I click the first link of the results page to read the Stack Overflow answer.) But at that point, it is enormously more important to make the students fully understand that methods are not executed in the order that they are written, but in the order in which the outside world invokes them. Any minor technical inaccuracy that I need to do to make them understand this is well worth the price.
  
  Remember that we are dealing with people who need to be explained the difference between returning a value from the method versus printing it on the console.
  
  When we get to arrays, I don't use the terms "pass by value" and "pass by reference", but simply say that when giving an array object as argument to a method, the method receives only the memory address where that array object is stored. The caller and the method therefore share the very same object, so any modifications that the method makes to the array object will remain there after the control has returned to the caller. And then compare the example methods
  
  ```
  public int[] reverse(int[] a)
  
  ```
  
  and
  
  ```
  public void reverseInPlace(int[] a)
  
  ```
  
  In the last lecture of the course, I go through a bunch of interesting and important and interesting little details that I tell them would logically have belonged to the earlier lectures, but at that point would have only confused them. But now, having worked through all my labs and listened to the previous lectures, they are ready to learn and understand them. One of those little details just so happens to be the computer science terms "pass by value" and "pass by reference" (and why not take a peek at Wikipedia page "[Evaluation strategy](https://en.wikipedia.org/wiki/Evaluation_strategy)" while we are at it, to give them a quick sneak preview of how programming paradigms other than that of Java really exist out there), which inspired me to write this response when I saw this question.
  
  > **Comments:**
  > **username_1**: I have occasionally lied to students about some technical detail, but always, immediately, in the next sentence, tell them I lied so that they can note it and follow up when the truth is available. But you have to warn them, ethically. Misconceptions can be hard to erase and the follow up might be missed for one reason or another. So, if you lie, do it _honestly_. You should never put a student in the position of needing to _unlearn_ something.  
  
  ---
  
  ### Answer by username_1 on 2018-03-13. Score: 2
  
  The correct statement, of course is that Java passes a reference by value. That is not the same as passing by reference. You can easily show the difference, by assigning a new object to the parameter variable within the method and then noticing that the code that invoked the method doesn't see the new object. It would be pass-by-reference only if the caller sees the new object.
  
  The confusion comes from the fact that the objects themselves are usually mutable and so within the called method, the receiver gets to make modifications. But both the caller and the called method have references to the same object. So the caller can see changes within the object made by the method. But it can't see a new object that replaces the parameter.
  
  However, if you assign to the parameter variable within the method, then the caller and the called method will see different objects.
  
  Yes, you should correct the teacher, but it is a bit risky, depending on the personality of the teacher. The best way, perhaps, is to prepare some demonstration code and show it to the teacher in the office. You can then, perhaps, offer to *clarify* the real meaning in Java, using your code.
  
  Personalities can be messy, of course, and not every teacher likes to be contradicted. But the students have a right to know the "real deal" if they are going to program in Java.
  
  Notice that Java and C++ are superficially similar, but deeply different. Learning both together is, itself, a cause of confusion. But in this case, the contrast can be informative.
  
  If nothing else, you can show the teacher this post from a 40 year veteran of programming, much of that in OO languages, including both C++ and Java (and author of books in both).
  
  > **Comments:**
  > **username_9**: It is amazing how many people get this distinction wrong, and how people can write correct code which, when you press them, their understanding contradicts completely! As always when this discussion crops up, I'll cite how much I love function definitions in FORTRAN, where you can explicitly state `IN`, `OUT`, or `INOUT`, and nobody has to worry about implicit semantics.  
  
  ---
  
  ### Answer by username_7 on 2018-03-13. Score: 2
  
  This is a tricky situation for you. You are correct that Java does not pass by reference, but your approach must depend on the personality of your professor.
  
  If the prof seems like the sort of person who would warmly accept this sort of feedback, I might directly mention it to them. (I have done so, and it has been fine, though it often takes an exceptionally well-adjusted person to be able to take such feedback gracefully.)
  
  A second option would be to raise your hand and ask about the difference between pass by reference, and passing references by value. This might trigger the prof to simply self-correct.
  
  A third approach would be to email the professor anonymously. This is a generally a jerk move, and not recommended.
  
  A fourth approach, which you have alluded to in your question, would be to pose the question as a pedagogic one. "Professor, I noticed that you told the class that Java passed Objects by reference. Was there some reason you didn't want to mention passing references by value? Was that just to avoid confusing people?"
  
  As to whether there could be a pedagogical advantage to distinguishing the way that primitives and Objects are passed in a rough way, my instinct from years of teaching this topic is that there could be. I object to using the specific words "pass by reference" (because those words have a specific meaning, and don't apply here), but pointing out that there is a distinction right at the start is important. The behaviors of the two types of parameters will appear to be quite different to the beginner.
  
  When I first introduce the topic, I **simply want to call attention to the fact that something is going on here that the student needs to pay attention to**, and I will be less concerned with whether every detail is understood at the start. Your professor could, indeed, be (somewhat clumsily) applying such a strategy.
  
  
  ---
  
  ### Answer by username_10 on 2018-03-16. Score: 1
  
  The usual "explanation" like "Java passes primitive types by value and other by reference" is rather confusing.
  
  In languages like C, C++, Fortran, etc. variables **contain** data. Then a variable is a chunk of memory with a name.
  
  The explanation of parameter passing by value or reference) doesn't work well for languages (Java, PHP, Lisp, Python etc) where variables are names for **references** to data sitting somewhere in memory.
  
  What is transmitted is the reference to the data. In all cases.
  The explanation fits well for primitive type : in
  
  ```
  int n = 3;
  
  ```
  
  consider n as holding a reference to an immutable constant integer value. And a call to `foo(n)` transmits the reference to that constant.
  
  Of course, it happens that the variables of primitive types are not really implemented as variables containing references to objects. It's a tradeoff for efficiency in Java.
  
  We'd better avoid using concepts which are pertinent for some - historically predominant - Fortran, Cobol, Algol, Pascal ...) langages to another category where they are much less adequate.
  
  Not all languages have PICTURE and USAGE like COBOL. So we don't use these concepts to explain other programming languages.
  
  EDIT: maybe the explanations about parameter passing focus too much on passing *variables*. I remember old-time FORTRAN where the statement
  
  ```
    call FOO(2+2)
  
  ```
  
  could modify some constant in memory :-)
  
  
  ---
  
  ### Answer by username_11 on 2018-03-18. Score: 1
  
  The proper way to think about this is to ask, "What is a variable storing?" Variables of primitive type store a bit pattern that represents their datum. Variables of object type store the memory address of the object they are pointing at on the heap.
  
  When you pass a variable to a function, a copy of the value it holds is made and is placed in the function's local symbol table. This occurs whether the variable is of object or primitive type. Thinking this way, what we have here is pass by value.
  
  If you pass a variable pointing at a mutable object and the callee invokes a mutator method, the state of the object residing on the heap will be changed. If the callee reassigns the variable, there is no effect.
  
  > **Comments:**
  > **username_5**: This was commen understanding before the Java Island felt like hijacking Computer Science and pasting all kind of platforms with it's dogmatism on base of a langauge that does not even know de-referenciation. Think the development started with the authors of C calling a pointer "value".  
  
  ---
  
  