Java single dimention arrays equating two variables of different size -


class myarray{      public static void main(string args []){      int x[]={2,3};     int y[]={4,5,6};     system.out.println(x);/*gives [i@5445a*/     system.out.println(y);/*[i@5442c */     x=y;     system.out.println(x); /*gives same of y (i.e [i@5442c  ). happen  here?*/     system.out.println(x[2]);/* gives 6*/     } } 

but happen when use "x=y" ?does address of y goes x or else?is garbage value?

array variables references class-typed variable. x , y contain references array somewhere in memory. let's @ assignment:

but happen when use "x=y" ?

yes, objects in java, address array y stored in x. result x , y reference same array , array referenced x lost (and garbage collected). if instead want copy of y assigned x try:

x = arrays.copyof(y, y.length) 

a tip: try using following code if want print contents of array:

system.out.println(arrays.tostring(x)); 

for more info arrays class try: http://docs.oracle.com/javase/6/docs/api/java/util/arrays.html


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -