Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on language Fundamentels..

 
Frank Jacobsen
Ranch Hand
Posts: 358
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when the following program is compiled and run. Select the one correct answer.


a: The program does not compile.
b: The program prints 0.
c: The program prints 1.
d: The program prints 2.
e: The program prints
The Correct answer is C, but why ???
I can se that the j array are castet to the i array, and this must be the i, that it�s created in the static methoed !
[ Added UBB [CODE] tags to preserve whitespace and improve readibility ]
[ May 27, 2002: Message edited by: Jessica Sant ]
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Frank
You question already has discussed in this thread:
http://www.coderanch.com/t/236886/java-programmer-SCJP/certification/Passing-array-method
Jamal Hasanov
www.j-think.com
 
bob towers
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a really nice question there are two versions of it:
public static void change_i(int i[]) {
int j[] = {2}; i = j; }
 
bob towers
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, i'm having keyboard problems, i'll try again:
This is a really nice question there are two versions of it:
VERION A:
public static void change_i(int i[]) {
int j[] = {2};
i = j;
}
and
VERSION B:
public static void change_i(int i[]) {
i[0] = 2;
i[0] *= 2;
}
In version A you pass a copy of a reference to an array. Within the method body you change the value of *THE COPY* but you do not change the value of any of the contents of the *ORIGINAL* array.
In version B you use the copy of the reference of the array to address its *CONTENTS* and change their value.
If i'm not mistaken you found this question on javaprepare's site. I think these versions neatly delineate the distinction between a reference to an array and referencing the contents of the array..... or at least, i found it useful.
best wishes
bob_towers@hotmail.com
 
Steven Sun
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Bob got the key!
 
Ashwani Mangy
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
I know that the problem have been explained very well but I just want to add one thing. In Java parameter passing strategy is call-by-value and not call-by-reference. Call-by-reference would have allowed values in the actual parameters to be changed via formal parameters.
Formal parameter can only change the state of the object whose reference value was passed to the method.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a demonstration of how parameters are passed in Java, go here and click on the appropriate link under "Parameter Passing in Java":
http://www.geocities.com/mcglonec1978/javacert/javacert.html
Corey
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic