File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes question about argument passing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about argument passing" Watch "question about argument passing" New topic

question about argument passing

Wenyu Deng

Joined: Mar 27, 2003
Posts: 6
anyone can tell me the myValue[0] is 1 or 2? thanks a lot,the code is below:
public class PrimitiveReference{
public static void main(String args[]){
int [] myValue={1};
System.out.println("myValue is"+myValue[0]);
public static void modifyIt(int[] value){
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The best way to know is to run it!
The general rule is the method cannot change the parameter, so it cannot make the caller's variable myValue point to a different array. But the method can change what's in the array, so it can change value[0] from 1 to 2.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I will tell you what I did, I analysed the code and predicted the answer. Then I compiled the code, and I was correct.
Have you done that?

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Wenyu
I suggest that you can run your programme yourself that must get the result
After run your programme,if you do not know why, you can talk about it

Francis Siu
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
I predict that you want to ask the question which concept of the programme is pass by reference or pass by value?
Is it right?
or you want to ask why?
I agree. Here's the link:
subject: question about argument passing
It's not a secret anymore!