File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in arrays

 
radhika madini
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am moving this thread to Programmer Certification Study forum.

Ajith
 
Pratap Reddy
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 141
Java Redhat Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Pratap Reddy
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sunil, try just return;
 
radhika madini
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To radhika,
Now your understanding is right.
 
Sandeep Nachane
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
sunilkumar ssuparasmul
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
dheeraj chopra
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic