This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in arrays" Watch "doubt in arrays" New topic
Author

doubt in arrays

radhika madini
Greenhorn

Joined: Jan 10, 2001
Posts: 18
Hi everyone,
I have doubt in following prgm.

I ran this program and it is displaying i[0] as 1.
my question is why it is not displaying the value 2 which is obtained by (i=j) stmt.If i keep return stmt in change_i method then it is displaying i[0] as 2.but I don't know the reason of what happeneing there.
can anybody explain me the reason in detail..
Thanks
radhika

[This message has been edited by Ajith Kallambella (edited January 11, 2001).]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
I am moving this thread to Programmer Certification Study forum.

Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Pratap Reddy
Ranch Hand

Joined: Jan 05, 2001
Posts: 36
either way I am getting i[0] as '1'. in change_i you are assigining value of j[] to local var i[]. Try to change the value in main i[] to 2 then you will get 2.
ashwini srinivasan
Greenhorn

Joined: Nov 07, 2000
Posts: 26
hi radhika,
int i[] is changed to {1} in main method.
Calling the method change_i(i) will pass a copy of i to the method and copy value of i is changed to 2. But the original value of i is unaltered. Hence the output of i[0] is 1.
If u put System.out.println(i[0]) in the method change_i() method, then the value of i get changed to j...i,e to {2}.
sunil choudhary
Ranch Hand

Joined: Nov 10, 2000
Posts: 141

Hi Radhika,I tried your code but it is giving errors with the return statement.If I just give return it just gives 1 as the O/p

giving error as
'return with value from void change_i(int [])
[This message has been edited by Ajith Kallambella (edited January 11, 2001).]


"Learning is weightless, a treasure you can always carry easily." -Chinese Proverb
Pratap Reddy
Ranch Hand

Joined: Jan 05, 2001
Posts: 36
Hi sunil, try just return;
radhika madini
Greenhorn

Joined: Jan 10, 2001
Posts: 18
Hi Ashwini,
what you said is correct.I know that copy of reference is passed but see the code below.

After compiling and running it is printing i[0] as 4 but not 1..
can anybody say whats the reason for that..
sunil you have to put return stmt as i kept in above prgm with return type as int[] and return stmt inside prgm as return i;..or you can also keep return type as int and return stmt as return i[0].. both will work with out errors..
regards
radhika

[This message has been edited by Ajith Kallambella (edited January 11, 2001).]
radhika madini
Greenhorn

Joined: Jan 10, 2001
Posts: 18
Hi all,
In the above prgm i kept return stmt..
without return stmt also it is printing i[0] as 4..
help please...
regards
radhika
Zheng Huang
Ranch Hand

Joined: Dec 20, 2000
Posts: 49
Try this.
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[]) {
int j[] = {2};
i[0] = j[0];
}
}
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Eventhough a copy of the array reference is passed and modified by the change_i() method, note that in your main program you are reassigning the returned reference to old reference. That's why you see the changes.
Try the following program. It is self-explanatory.

Hope that helps!
Ajith
radhika madini
Greenhorn

Joined: Jan 10, 2001
Posts: 18
Hi Ajith,
I think u are saying that as we used return stmt ,value of i[0]is changed otherwise not.. but see the simple code below..public class example3 {
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]=4;
}
}
here in main() method i[0]is 1 right? we are not using any return stmt but just calling change_i() in main()..result is i[0]=4 after calling that method.. value is changed..
I ran this program and it is displaying 4..
I don't know the reason for that..
I think you didn't notice this thing..
can anybody explain me whats happening there..
radhika
radhika madini
Greenhorn

Joined: Jan 10, 2001
Posts: 18
Hi zheng,
I ran your program and it is changing array value..
From this I came to know that if we assign one array name to other array name like i=j the elements in i are not changed
where as if we assign elements of one array to elements of other array like i[0]=j[0] then elements in array i will be changed..
Am i right in understanding...
radhika
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
radhika,
I should have said this before - In Java, whenever objects are passed as parameters to a method, they are passed by reference. On the other hand, all primative data types (such as int, float, double, or char) are passed by value. And since array is an Object, it always gets passed by reference.
Infact developers use this "trick" to simulate pass-by-reference behaviour for primitives. They just wrap a primitive value in a single-element array and pass in to the method so that when the method call returns, you can use the changed value which is in the array!
Sorry for not clarifying this earlier. I completely misunderstood your question
Ajith
Zheng Huang
Ranch Hand

Joined: Dec 20, 2000
Posts: 49
To radhika,
Now your understanding is right.
Sandeep Nachane
Ranch Hand

Joined: Dec 06, 2000
Posts: 57
The parameter passing startegy in Java is always call by value and never call by reference regardless of the type of object ( sorry ajith, but you are incorrect to say that objects are passed by reference ). Also note that name of the array is the reference to the whole array i.e The variable which is the name of the array contains reference (or address) to the whole array object. In the example by Radhika it is variable i.
Now let us go back to radhika's code

Here when i is passed to change_it() method, essentially a copy of the reference variable holding whole array object is passed to the method. Now one can only change the state of the passed object not the object itself (remember it is a copy.. changing the object will not do any good to you ). Going back to the line in the code where i = j . This would mean you are changing the object itself and not the state of the object (which in this case of array object would be i[0], i[1] etc.. ) and changing the object reference is making changes only to the copy of the reference not the actual reference, and hence the result
Clear as mud ?... Please correct me if I am wrong .
-Sandeep Nachane
------------------
Visit my homepage at
www.tipsmart.com
[This message has been edited by Sandeep Nachane (edited January 11, 2001).]
[This message has been edited by Sandeep Nachane (edited January 11, 2001).]
[This message has been edited by Sandeep Nachane (edited January 11, 2001).]


<A HREF="http://www.tipsmart.com" TARGET=_blank rel="nofollow">www.tipsmart.com</A>
sunilkumar ssuparasmul
Ranch Hand

Joined: Dec 13, 2000
Posts: 142
public class ArraySample {
int i[] = {0};
public static void main(String args[]) {
int i[] = {1};
System.out.println("i before change"+i.hashCode());
change_i(i);
System.out.println("i after change"+i.hashCode());
System.out.println(i[0]);
}
public static void change_i(int i[]) {
int j[] = {2};
System.out.println(" i in change"+i.hashCode());
i = j; ///1
System.out.println(" i in change"+i.hashCode());
System.out.println("j in change"+j.hashCode());
}
}
Try the above code with both changing line 1 to i[0]=j[0];
u will get the difference
HTH
sunil.s
------------------
"Winners don't do different things
They do things differently"


"Winners don't do different things<br /> They do things differently"
dheeraj chopra
Greenhorn

Joined: Jan 12, 2001
Posts: 2
u r trying to instantiate a value to a member which is non static in the static method.So the value assigned will never be reached upto that instance variable it will be given to the local variable only.

------------------
dheeraj


dheeraj
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Sandeep,
Sorry but I have to disagree with you. Objects ( non-primitives ) and arrays are always passed by reference. Eventhough the reference itself is passed by value, it is just another handle to the same object and hence it is nothing but pass by reference.
Here is an extract from Sun's Java tutorial

For a method to modify an argument, it must be of a reference type such as an object or array. Objects and arrays are also passed by value, but the value of an object is a reference. So the effect is that arguments of reference types are passed in by reference. Hence the name. A reference to an object is the address of the object in memory. Now, the argument in the method is referring to the same memory location as the caller.

Also checkout the following links
Sun tutorial about passing values to methods
Marcus Greens SCJP tutorial about passing values to methods
Parameter passing tech doc from JDC

Ajith
Sandeep Nachane
Ranch Hand

Joined: Dec 06, 2000
Posts: 57
Ajith,
It makes more sense now that your lastest post says "Objects ( non-primitives ) and arrays are always passed by reference, Eventhough the reference itself is passed by value.."
I am still skeptical about the wording "Objects ( non-primitives ) and arrays are always passed by reference"
-Sandeep Nachane
Originally posted by Ajith Kallambella:
Sandeep,
Sorry but I have to disagree with you. Objects ( non-primitives ) and arrays are always passed by reference. Eventhough the reference itself is passed by value, it is just another handle to the same object and hence it is nothing but pass by reference.
Here is an extract from Sun's Java tutorial
Also checkout the following links
Sun tutorial about passing values to methods
Marcus Greens SCJP tutorial about passing values to methods
Parameter passing tech doc from JDC

Ajith


------------------
Visit my homepage at
www.tipsmart.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in arrays