• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array manipulation

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ... can someone please tell me as to why the output in the following code is 1 and not 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[]) {
int j[] = {2};
i = j;
}
}
---------------------------------------------------------------
Thanx in advance
Samith
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Samith!
In your method
public static void change_i(i){
int j[]=2;
i=j;
}
when you say i=j;
you are changing the reference,(making reference i equals reference j)not the content of your original array which is i[0]
and so it still contains 1 and not 2.Hope this helps.
 
nachiket deshpande
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry method parameter is int i[] not i,typing mistake..
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Nachiket,
public class example {
public static void main(String args[]) {
int i[] = {1,2};
int j[] = {11,12};
i=j;
System.out.println(i[0]+""+i[1]);//prints 11,12
}
}
i hv tried this seems to be CONTENT of the arry has changed.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, here is the deal.
When u pass i as a parameter to a method. actually the complier make a copy of the reference. suppose it's "a"
so the method can be replaced by this
public static void change_i(int a[]) {
int j[] = {2};
a = j; //Line 1
}
in Line 1, when you do the assignment, actually u are assigning a to j, no i. i is still pointing to {1}.
put in another way, we have 3 reference of array now, i, j, a, You are manipulating a inside the method. changing reference of a won't affect i. But if the method change to this:
public static void change_i(int a[]) {
int j[] = {2};
a[0] = {2}; // Line 1
}
This will also affect i outside. here a do point to the same array as i. so if a changing value, i is also changing.
Hope this help.
Tracy
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys
just imagine u are passing a obj reference here and look at the problem, when u change a obj reference inside a called method and return nothing happens, if u have to make changes then u have to access the instance variables and make changes like obj.i=5; etc then only the changes will reflect. To make changes reflect here in out case u will access i[0] inside the changed method and make the change.
u can run a similar code with objects and clarify your self.
hope i have helped
thanx
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic