Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about Array

 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to add one more statement at the end
after j[] = {2};
i = j;
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not clear about your explanation, could be explain to me in detail.
 
zarina mohammad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic