• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cindy!
I shall check up with campfire!
Rgds,
Divya
------------------
Eat Java,Drink Java...surf only Javaranch!
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic