• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java "pass by reference" or "pass by value"?  RSS feed

 
junchen liu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
take a look of this:

1>


What is Expected print result? if it's null why is that? I really think it should print out "hi"

2>


again what's the expected result? can we swap it ? if no, why not, and how can we successfully make a swap function in Java?

[ November 10, 2008: Message edited by: junchen liu ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is pass by value for all data types.

Variables of object type are references to objects. That means that when you pass an object as an argument to a function, you're passing a copy of the reference to the object -- i.e., the object reference is passed by value.

Because Java is pass by value, if you change the value of a method argument inside the method, the caller does not see the change; i.e., x remains null in your program even after calling the method.

You can't write a method to swap the values of two variables passed as arguments.

Moving to Java in General (Beginner).
[ November 10, 2008: Message edited by: Ernest Friedman-Hill ]
 
junchen liu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer sir!
I am not afraid to say that I am a programmer, with 3 years of industrial experience, I might not be a good programmer, but I have worked on many projects, my code runs on on a few major government server as well. I just suprised that how far I have went without event understand such simple concept.

I blame it on myself lack of C or C++ experience, never worked with pointers, spent too much time reading APIs rather then trying to understand some fundamental concepts, I am a good sample of how programmer can get spoiled by Java and modern IDEs.


anyway let's work on the question


Because Java is pass by value, if you change the value of a method argument inside the method, the caller does not see the change; i.e., x remains null in your program even after calling the method.






I have changed the code again, and the value changed this time, I still don`t get it why.Are you saying, EVERY method call resulting the JVM make a fresh copy of the original memory location (pointer) inside the method scope, change the formal parameter, only resulting the newly created pointer pointing to another chunk of memory, the original pointer still pointing to "string";

however, before the " y = "hi";" statement, pointer Y and X was pointing to the same memory location which hold the value "string". Any change on the reference y would also changes x isn't ?

You might say that even the X and Y point to the same chunk of memory, but
change to one doesn't reflect on another one. therfor I added another method
giveMeAObj(ObjectRef objY); as you can see, as objX and ObjY point to the same object, change on objY at method level, reflected on the actual object.
Does that mean, primitives type is pass by value, object pass by reference?


You can't write a method to swap the values of two variables passed as arguments.


if I can't swap two arguments, is there anything I can swap?



[ November 10, 2008: Message edited by: Ernest Friedman-Hill ]

[ November 10, 2008: Message edited by: junchen liu ]
[ November 10, 2008: Message edited by: junchen liu ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said before, object references are passed by value. This means that objX and objY are two different memory locations that both refer to the same object. You can change that object through either reference, and you'll be able to see the change through the other reference. But if you chnage the value of objY itself -- i.e., by assigning a value to it like "objY = null", then objY will no longer refer to the same object as objX; objX itself will not change, in the same way that assigning to y doesn't change x.
 
junchen liu
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks sir, to sum up what you said;

within a method, we can manipulate actual parameters through its reference,
but we can not change it's original memory location. am I right?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!