• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays  RSS feed

 
srinivas sridaragaddi
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I have couple of doubts please clarify them....


1)


output:4

arrays are objects so the changes made to it would affect
is this understanding correct


2)



output:1

whats happening here,i was expecting that it would print
2
 
Teo Framoe
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
2:
i is local in change_i.
At first change_i's i are refering to the same array object as i in main,
but then you make the change_i's i refere to {2}.
Main's i remains unchanged.
Regards,
Teo
[ October 15, 2007: Message edited by: Teo Framoe ]
 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
somehow i am still confused with this concept too, would it be passed by value, or passed by reference?
or making changes in the method would change the value for the instance?

in this case it is still contradict to me.
 
Kishore Kumar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java, everything is passed by value only. There is no passed by reference.
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the second case the output is 1 because of this statement
public static void change_i(int i[]). in this case the i declared is local to the function .if System.out.println is put inside this function then the output will be 2 .Now when S.O.P is encountered in Main, the array thus reffered is the array in which 1 is stored (Since the life of the array in which 2 is stored dies once the program is returned to main).
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the first case when the call is made, j also points to heap pointed by i, so changes made in j also affects the changes made in i.


"Time and tide waits for none"
 
Geetha Arthanari
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain briefly.
I don't understand these two examples clearly?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!