0

I recently read this post: Is Java "pass-by-reference" or "pass-by-value"?

The first answer was far too terse and specific to give me a firm understanding, and everyone else created 15 page essays explaining what should be a simple topic. Are the following rules correct (which are pretty simple if true):

1.) Named things associated with a type that aren't primitives ("objects references" in Java talk) are pointers in C++.

2.) . on a Java pointer is the same as using -> on a C++ pointer.

3.) = on a Java pointer is the same as using = on a C++ pointer.

4.) All functions pass by value (and hence pass Java pointers by value).

(This ignores memory management differences like the fact that = on a Java pointer is fine whereas = on a C++ pointer means you might need to worry about memory management)

The most confusing element to the puzzle is that no one I have seen has conveyed this set of rules/understanding of what Java is doing in clear English (opting instead for 15 pages and diagrams). The second most confusing element is that Java uses the word "reference" in a way that is different from C++. If you want an analogy between what Java is doing in terms of C++, pointers and the rules I created are the simplest terms to digest Java's behavior.

Community
  • 1
  • 1
user904963
  • 1,520
  • 2
  • 15
  • 33
  • All of your observations are correct. Java has no such thing as "C++ references". That's why you can't write a swap function in Java. – fredoverflow Aug 02 '14 at 10:33

1 Answers1

2

Point 1 is correct.

Object o = new Object();

Variable o is storing a pointer to where the object is in memory.

Point 2 is also correct.

Object o = new Object();

o.hashCode();

This is calling the method hashCode of the object stored in o.

Point 3, correct again.

Dog d = new Dog();

Dog d2 = new Dog();

System.out.println(d == d2);//This will print false

d2 = d;

System.out.println(d == d2);//This will print true

In the first output d does not equal d2 because the pointers to the objects are different. In the second output d does equal d2 because they both are pointing to the same location.

Point 4 is technically correct.

Primitives are passed by value. A When passing objects to a function it is passing a copy of the pointer to the object and not a copy to the object itself. The pointer to the object is being passed by value to the function. Hopefully this snippet can help explain it.

public void example(int i, Object o) {}

public static void main(String[] args) {

    Object o = new Object();

    example(3, o);//This is passing the number 3 to the function. It also passes by vlue a pointer to where object o is stored in memory.
}
gyroninja
  • 111
  • 1
  • 1
  • 5
  • 1
    Java is ALWAYS pass by value. Even when passing an object, it passes by value. So: `void example(int i, Object o) { o = null; }`, using your example, `o` in the `main` method is not going to be null. Read this: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – user3439065 Aug 02 '14 at 06:53
  • I love how people are voting you down. Their pedantry really makes the simplicity of the situation unreachable for new Java programmers coming from C++. – user904963 Aug 02 '14 at 07:10
  • 1
    The last paragraph is confusing/incorrect. Passing objects to a function in java **is not** like passing objects by reference in C++. – juanchopanza Aug 02 '14 at 07:11
  • @user904963 Well, it turns out the answer is wrong. It is not a matter of pedantry. It shows you have failed to grasp this concept, given that you selected this answer. – juanchopanza Aug 02 '14 at 07:11
  • That is correct. In the snippet on point 4 I say it's passing a pointer by value. It is making a copy of the pointer and passing it to the function. Above that I mention to treat it as being passed by reference from a C++ standpoint as to think as if it was passing a pointer to the value in memory where the object is stored instead of making a copy of the entire object itself. Also with references you are not allowed to reassign them. Reassigning the values passed in to a function is bad practice, it is why some people mark there function arguments as final. I'll reexplain this in my post. – gyroninja Aug 02 '14 at 07:12
  • @juanchopanza I saw the mumbojumbo about things being passed by reference. It's obviously wrong since references in C++ can never be reassigned to associate with a different object in memory, but everything else was fine. A small error that needs a simple edit to correct doesn't mean the answer should be voted down or not selected. He should just correct his answer. – user904963 Aug 02 '14 at 07:13
  • @user904963 It is not mumbo jumbo. It has a very specific meaning in C++. That is what makes the last paragraph at the very least confusing. – juanchopanza Aug 02 '14 at 07:14
  • 1
    @gyroninja Passing by reference in C++ is quite different to passing a pointer. – juanchopanza Aug 02 '14 at 07:15
  • @juanchopanza I agree it's confusing. But he contradicts his point beneath it twice (once in writing and another in the comment in his example). The twofold contradiction happens to be correct, which hopefully arms someone with the ability to understand the answer (esp. when combined with my question). I'd still like to see him edit out the usage of C++ references in his description, though. He might be a Java doodoo head and be using reference in the Java sense (which is basically just "pointer"). – user904963 Aug 02 '14 at 07:20
  • java is always pass by value? i heard objects and arrays are passed by reference ! – user2837260 Aug 02 '14 at 08:10
  • 1
    @user2837260 You cannot pass objects at all in Java. All you can pass in Java is primitive values and references. By value. – fredoverflow Aug 02 '14 at 10:36
  • @FredOverflow umm..i guess i did pass objects during multi threading implentation – user2837260 Aug 06 '14 at 10:11