Skip to main content
deleted 18 characters in body
Source Link
Scott Stanchfield
  • 30.7k
  • 9
  • 53
  • 67

I just noticed you referencedI'm the author of the my articleblog post you're talking about. To clarify a few things:

I just noticed you referenced my article.

I'm the author of the blog post you're talking about. To clarify a few things:

Removed historical information (that is what the revision history is for)—the answer should be as if it was written right now; see e.g. <https://meta.stackexchange.com/a/131011> (near "Changelogs").
Source Link
Peter Mortensen
  • 31.5k
  • 22
  • 110
  • 134

Update

Update

Add more explanation as to why C's swap(&x, &y) isn't an exception to pass-by-value, and can also be simulated in Java.
Source Link
Scott Stanchfield
  • 30.7k
  • 9
  • 53
  • 67

Update

A discussion in the comments warrants some clarification...

In C, you can write

void swap(int *x, int *y) {
    int t = *x;
    *x = *y;
    *y = t;
}

int x = 1;
int y = 2;
swap(&x, &y);

This is not a special case in C. Both languages use pass-by-value semantics. Here the call site is creating additional data structure to assist the function to access and manipulate data.

The function is being passed pointers to data, and follows those pointers to access and modify that data.

A similar approach in Java, where the caller sets up assisting structure, might be:

void swap(int[] x, int[] y) {
    int temp = x[0];
    x[0] = y[0];
    y[0] = temp;
}

int[] x = {1};
int[] y = {2};
swap(x, y);

(or if you wanted both examples to demonstrate features the other language doesn't have, create a mutable IntWrapper class to use in place of the arrays)

In these cases, both C and Java are simulating pass-by-reference. They're still both passing values (pointers to ints or arrays), and following those pointers inside the called function to manipulate the data.

Pass-by-reference is all about the function declaration/definition, and how it handles its parameters. Reference semantics apply to every call to that function, and the call site only needs to pass variables, no additional data structure.

These simulations require the call site and the function to cooperate. No doubt it's useful, but it's still pass-by-value.

Update

A discussion in the comments warrants some clarification...

In C, you can write

void swap(int *x, int *y) {
    int t = *x;
    *x = *y;
    *y = t;
}

int x = 1;
int y = 2;
swap(&x, &y);

This is not a special case in C. Both languages use pass-by-value semantics. Here the call site is creating additional data structure to assist the function to access and manipulate data.

The function is being passed pointers to data, and follows those pointers to access and modify that data.

A similar approach in Java, where the caller sets up assisting structure, might be:

void swap(int[] x, int[] y) {
    int temp = x[0];
    x[0] = y[0];
    y[0] = temp;
}

int[] x = {1};
int[] y = {2};
swap(x, y);

(or if you wanted both examples to demonstrate features the other language doesn't have, create a mutable IntWrapper class to use in place of the arrays)

In these cases, both C and Java are simulating pass-by-reference. They're still both passing values (pointers to ints or arrays), and following those pointers inside the called function to manipulate the data.

Pass-by-reference is all about the function declaration/definition, and how it handles its parameters. Reference semantics apply to every call to that function, and the call site only needs to pass variables, no additional data structure.

These simulations require the call site and the function to cooperate. No doubt it's useful, but it's still pass-by-value.

What you described was passing a pointer into a function in C. The function would need to explicitly de-reference that pointer, just as I describe for Java. (The fact that in C you can create a pointer to a var just clouds the issue; it's still not reference semantics)
Source Link
Scott Stanchfield
  • 30.7k
  • 9
  • 53
  • 67
Loading
Removed the incorrect statement that "Java is exactly the same as C", and added an explanation what is possible in C
Source Link
fishinear
  • 6.4k
  • 3
  • 42
  • 92
Loading
clarify that the pointer can be changed but the change won't be visible outside the method
Source Link
Scott Stanchfield
  • 30.7k
  • 9
  • 53
  • 67
Loading
Added a little clarification as to why I say "pointers"/"addresses". Please do not change terms like this in my answer
Source Link
Scott Stanchfield
  • 30.7k
  • 9
  • 53
  • 67
Loading
Rollback to Revision 11
Source Link
Scott Stanchfield
  • 30.7k
  • 9
  • 53
  • 67
Loading
Removed usage of the word _pointer_ as well as the word _address_. Also tightened things up a good bit and removed some confusing C-like language.
Source Link
Gray
  • 117.2k
  • 24
  • 305
  • 360
Loading
removed unnecessary stuff
Source Link
user3453226
user3453226
Loading
Changed back to "his". You changed it to the improper "it's" instead of the proper possessive "its". My style is to treat the Dog as a "he"; that makes it a little more personable. Your edit fixed nothing and introduced grammatical errors. Please only correct actual problems.
Source Link
Scott Stanchfield
  • 30.7k
  • 9
  • 53
  • 67
Loading
added 2 characters in body
Source Link
Nathaniel Ford
  • 21.3k
  • 20
  • 98
  • 112
Loading
-signature
Source Link
Unihedron
  • 11.1k
  • 13
  • 65
  • 72
Loading
Added comment about the dog's name being Max after foo was run
Source Link
Scott Stanchfield
  • 30.7k
  • 9
  • 53
  • 67
Loading
added 2 characters in body
Source Link
Scott Stanchfield
  • 30.7k
  • 9
  • 53
  • 67
Loading
b/c -> because
Source Link
Wusiji
  • 599
  • 1
  • 7
  • 26
Loading
Post Made Community Wiki by Eng.Fouad
some formatting
Source Link
Joachim Sauer
  • 309.2k
  • 59
  • 567
  • 624
Loading
added 2897 characters in body
Source Link
Scott Stanchfield
  • 30.7k
  • 9
  • 53
  • 67
Loading
deleted 312 characters in body
Source Link
Scott Stanchfield
  • 30.7k
  • 9
  • 53
  • 67
Loading
Source Link
Scott Stanchfield
  • 30.7k
  • 9
  • 53
  • 67
Loading