This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt about Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt about Array" Watch "Doubt about Array" New topic
Author

Doubt about Array

suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
CASE 1
------
public class test {
int i = 0;
public static void main(String args[]) {
int i = 1;
change_i(i);
System.out.println(i);
}
public static void change_i(int i) {
i = 2;
i *= 2;
}
}
output is 1 because after executing the method change_i(int i) it will not retain its value, when it comes to main method the output will be 1. Thats what my understanding.
Why is not working for this CASE 2.
public class example {
int i[] = {0};
public static void main(String args[]) {
int i[] = {1};
change_i(i);
System.out.println(i[0]);
}
public static void change_i(int i[]) {
i[0] = 2;
i[0] *= 2;
}
}
Why is the output 4 in this case. Please explain me.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
"int i" is a primitive and is passed by value. Nothing that change_i() does can affect its value back in main().
"int i[]" is an array, which means it's an Object and therefore "i" is a reference. When you call change_i(), you're passing it a reference to your array, and it's changing one of the elements of that array.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
CASE 3
public class test {
int i[] = {0};
public static void main(String args[]) {
int i[] = {1};
change_i(i);
System.out.println(i[0]);
}
public static void change_i(int i[]) {
int j[] = {2};

}
}
Why is it displaying output as 1, why not 2 according previous explantion.
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
I have to add one more statement at the end
after j[] = {2};
i = j;
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The third case is a little like the first case.
You cannot change the reference i from inside change_i (in case 2 you changed what i referenced).


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
I am not clear about your explanation, could be explain to me in detail.
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104

suresh,
In this case you not modifying the Object i[] Via the reference value passed..instead you are creating a new Object j[] and modifying it. hence the original value of i[] is unchanged.
prehaps going through this camfire stories at javaranch will help you understand bettercampfire stories
-zarina
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055

You created a local copy of i inside change_i(). At the start of the method, both the local i and the i in main are pointing to the same array object. After i = j; the local copy is now pointing to the {2} array. At the end of the method, the local i goes out of scope.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787

public class Test{int i[] = {0};
public static void change_i(int i[]){
int j[] = {2};
i = j; }
static public void main(String args[]){
int i[] = {1};
change_i(i);
System.out.println(i[0]);}}
----------------------------------------------
You created a local copy of i inside change_i(). At the start of the method, both the local i and the i in main are pointing to the same array object. After i = j; the local copy is now pointing to the {2} array. At the end of the method, the local i goes out of scope.

I am not sure if I am following the point made in
above statement. Facts as I see them are:
- i is an int array object with local scope in main()
initialized with {1}
- i is passed as object reference to change_i()
- j is an int array object with local scope in
change_i() initialized with {2}
- i = j statement will change object reference
in i from {1} object to {2}.
- if we were to print i at this time, it will
print {2}
- at the end of change_i() method, the object
{2} cease to exist
- so now my best guess is that i will be pointing
to null. How can it go back to the object {1} it
was referring prior to {2}? I do not think
object references have any audit trail....
Thanks
Barkat
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
The local variable i[] in change_i() hides the instance variable i[] in the Test object.
Try changing the local variable's name from "i" to "z" and you'll understand better what's going on here.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787

The local variable i[] in change_i() hides the instance variable i[] in the Test object.
Try changing the local variable's name from "i" to "z" and you'll understand better what's going on here.

Hi Ron:
I understand that if I change i to z in change_i(), z will not have any impact on i. But here is
my point:
1) we have not created any Object yet, so there
is no instance variable i[] in existance.
2) I understand that arrays are treated as objects
and objects are passed as reference and hence
changes made to the reference or object itself
in a method, will carry through after the end
of that method.
Therefore, if reference i is changed to point to
another object (i.e {2}) in change_i(), it will
also be changed in main(). I am sure one my
above assumption is wrong. Which one...
Thanks
Barkat
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787

2) I understand that arrays are treated as objects
and objects are passed as reference and hence
changes made to the reference or object itself
in a method, will carry through after the end
of that method.

I guess in above statement should be changed to:
2) I understand that arrays are treated as objects
and objects are passed as reference and hence
changes made to object itself
in a method, will carry through after the end
of that method. Any changes to reference will not
carry through the end of that method. Here is an
example code. Changes made to object are
persistent outside the change_i() while changes
made to reference t are not.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt about Array