File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes java about pass by value,why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java about pass by value,why?" Watch "java about pass by value,why?" New topic
Author

java about pass by value,why?

Jin chengong
Greenhorn

Joined: Oct 29, 2007
Posts: 10
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

Joined: Dec 17, 2007
Posts: 22


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

Joined: Apr 20, 2006
Posts: 3355

Hello Buddies,

Perhaps, these links might help you.

  • Story PassBy
  • Pass by References for objects


  • Good luck!


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Jin chengong
    Greenhorn

    Joined: Oct 29, 2007
    Posts: 10
    thank you for you reply!
    i will take a look at!
     
    Don't get me started about those stupid light bulbs.
     
    subject: java about pass by value,why?