• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Doubt in passing arrays

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am little confused in these 2 versions of passing arrays of methods.

In 1st , i pass array ot some method change_i() ,make its reference to point to other reference ,but in caller method, it remains same & value also.

In 2nd way, which i have commented in ,code snippet,I pass the array to method change_i ,change some values ,in caller method ,it shows changed.

Here ,also ,it shouldnt have been changed ,as array when passed goes to method argument change_i(int i[]) .int i[] is different from caller method's array int i[]???/

please help me out ,why 2 codes behave differently.what is the basic difference. :-(


public class ArgTest {
int i[]={0};
public static void main(String args[]){
int i[]={1};
System.out.println("in main " +i+" ->"+i[0]);
change_i(i);
System.out.println("from method " +i+"->"+i[0]);
}

public static void change_i(int i[])
{
//System.out.println(i+"->"+i[0]);
//i[0]=2;
//i[0]*=2;
//System.out.println(i+"->"+i[0]);

int j[]={2};
System.out.println("inside method before assign"+i+"->"+i[0] +" "+j+"->"+j[0]);
i=j;
System.out.println("inside method "+i +"-> "+i[0]+" "+j+"-> "+j[0]);
}

}

thank you
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use Code Tags when posting code.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See Campfire Story: Pass by Value.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much .Now i got it :-)
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic