Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating reference to a primitive - Roberts-Heller's cert guide?

 
divyasheel sharma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How to create reference to a primitive :
This is from Page 21 RH Guide for JCP Exam,
I couldn't make out what this means:
Can someone explain the code for me please :
public class PrimitiveReference {
public static void main(String args[]) {
int[] myValue = {1};
modifyIt(myValue);
System.out.println("myValue contains"+myValue[0]);
}
public static void modifyIt(int [] value) {
value[0]++;
}
}
Thanks
Divya
 
Scott Pedigo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the point that the example is trying to illustrate is that a copy of the *reference* to the integer array is passed into the method on the stack, not a separate copy of the array itself. So when in the method the value of the array element is changed, the original is changed. If a copy of the array contents were passed in on the stack, then only the copy on the stack would be changed, and the original would remain unchanged (which is not the case). The output should therefore show the change.
 
divyasheel sharma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve!
But how do I know that its the copy or the separate copy that is passed to the method?
Please explain bit more.
thanx
Divya
 
divyasheel sharma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve!
But how do I know that its the copy or the separate copy that is passed to the method?
Please explain bit more.
thanx
Divya
 
Pat Barrett
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When an array is passed into a method it is a reference, not a copy of a reference.
HTH
Pat B.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Primitives always pass a copy of the primitive. Objects always pass a copy of the reference to the object.
myValue holds a reference to the array (that is an object). modifyIt(myValue) is taking in a copy of that reference.

This campfire story explains it in such amuzing detail:
http://javaranch.com/campfire/StoryPassBy.jsp
[This message has been edited by Cindy Glass (edited February 16, 2001).]
 
divyasheel sharma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cindy!
I shall check up with campfire!
Rgds,
Divya
------------------
Eat Java,Drink Java...surf only Javaranch!
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have any doubts, just compile/run the code...you will see that the output is
myValue contains 2
 
divyasheel sharma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Cindy!
Campfire stories are great for understanding, I shall try out more on this and if I have doubts I shall come back.
Rgds
Divya

------------------
Eat Java,Drink Java...surf only Javaranch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic