Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

output not clear

 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.I am running this program and it is printing value of y two times.why is it so? By my understanding it should print 42 only once.

class XCopy
{
public static void main(String [] args) {
int orig =42;
XCopy x = new XCopy();
int y=x.go(orig);
System.out.println(orig + " " +y);
}

int go(int arg) {
arg = arg *2;
return arg;
}
}
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should see section Pass by value & Pass by reference in java books
And then you TELL me why its working so...


HTH
V
 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont have any java book right now with me..i m studying thr internet..please please help me out
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go rakhee,
See this
Hopefully you will understand O/P after reading it.

HTH
V
[ May 02, 2008: Message edited by: Vishal Matere ]
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another recent posting about pass-by-value here. It quotes a couple of links, one the same as Vishal Matere posted (and it is a good link). Note the JavaRanch FAQ quoted in that other thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic