This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Arrays  RSS feed

 
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
 
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 ]
 
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.
 
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.
 
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"
 
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!