Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java about pass by value,why?

 
Jin chengong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Value{
public int i = 15;
}

public class Test{
public static void main(String args[]){
Test t = new Test();
t.first();
}
public void first(){
int i =5;
Value v = new Value();
v.i = 15;
second(v,i); <<<<<here....
System.out.println(v.i);
}
public void second(Value v,int i){
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i+" "+i);
}
}
the answer is 15 0
20
anyone who can tell me why?
 
Marvin Domingo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I'm a beginner as well, about to start on chapter 5 or head first java, you're asking why 15 0 20 is the ouput?, maybe because of this method



I don't know how to explain it, but from the title of your post itself explains it (this is my way of learning, to interfere with some other beginners code )
[ December 23, 2007: Message edited by: Marvin Domingo ]
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Buddies,

Perhaps, these links might help you.

  • Story PassBy
  • Pass by References for objects


  • Good luck!
     
    Jin chengong
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thank you for you reply!
    i will take a look at!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic