Timeline for Java, how to pass by reference
Current License: CC BY-SA 3.0
Post Revisions
19 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Feb 7, 2016 at 14:44 | history | edited | Mazzy | CC BY-SA 3.0 |
deleted 68 characters in body
|
| Dec 23, 2013 at 1:11 | comment | added | Mazzy | @whaley You're competely right, I just learned that java can pass by pointer value and not pass by reference at all, thanks ;) | |
| Dec 22, 2013 at 12:18 | comment | added | whaley | @Mazzy it doesn't matter. You need to erase the expectation that Java has pass by reference anywhere, ever, and at all. Your question title seems to indicate that it does. | |
| Dec 21, 2013 at 20:27 | vote | accept | Mazzy | ||
| Dec 21, 2013 at 20:26 | comment | added | Bernhard | Mazzi, x++ is short for "take the number from the Integer object that x is pointing at, increment it by one, wrap it into a new Integer object and then let x point at that new object". As you can see, that does not contain the step "find all other pointers that point at a number that is a sum of x and some other number, increment all those by one as well and update all their references too". Which is pretty good, think of the side effects that would have, as any number can be seen ad the sum of x and some other number. Basically, all numbers in your program would change! | |
| Dec 21, 2013 at 20:23 | comment | added | Sergey Kalinichenko | @Mazzy C++ and other languages have references to objects, not to expressions. Even if you change components of an expression through a reference, the value obtained through the expression before the change is not going to be recomputed. | |
| Dec 21, 2013 at 20:18 | comment | added | Mazzy | @dasblinkenlight , that's the answer I was looking for, is this possible in for example c++ ? | |
| Dec 21, 2013 at 20:17 | answer | added | Gari BN | timeline score: 1 | |
| Dec 21, 2013 at 20:17 | answer | added | kidcuber | timeline score: -1 | |
| Dec 21, 2013 at 20:17 | comment | added | Sergey Kalinichenko |
@Mazzy When you assign xy = x+y, a new object is created (or looked up in the interning cache if the result is small). The important thing, though, is that the third object is completely independent of the first two.
|
|
| Dec 21, 2013 at 20:14 | comment | added | Sotirios Delimanolis | Your expectations are incorrect. | |
| Dec 21, 2013 at 20:14 | answer | added | Brian Roach | timeline score: 5 | |
| Dec 21, 2013 at 20:14 | comment | added | Mazzy | @whaley , that issue is a bad example as it is using int and not Integer ....... As I said, Integer is a class, int is not, big difference, if u used Integer in that function, it would work. | |
| Dec 21, 2013 at 20:13 | comment | added | whaley | Java is pass by value, always. See javaworld.com/article/2077424/learn-java/… javadude.com/articles/passbyvalue.htm?repost | |
| Dec 21, 2013 at 20:13 | comment | added | Mazzy | @dasblinkenlight x and y are instances of Integer, xy is also a reference, xy2 is a value only. so technically, if I update x, xy should update as well because they are pointing to the same number | |
| Dec 21, 2013 at 20:11 | history | edited | Simeon Visser |
edited tags
|
|
| Dec 21, 2013 at 20:10 | comment | added | Sergey Kalinichenko |
Why in the world would you expect xy or xy2 to change when you change x?
|
|
| Dec 21, 2013 at 20:10 | answer | added | Simeon Visser | timeline score: 3 | |
| Dec 21, 2013 at 20:08 | history | asked | Mazzy | CC BY-SA 3.0 |