a simple compile, but I expected different output.

So I expected the following to output 134:

publicclass Test{

publicstaticvoid main(String[] args){

int num;

num=34;

calc(num);

System.out.println("num="+num);

}

staticint calc(int num){

num+=100;

return num;

}

}

It is not yet clear to me why this still outputs num=34.... It looks like the calc functions takes the value of num to be 34, but does nothing with it...

Thank you in advance for any help with this beginner's question.

[1042 byte] By [DeChristoa] at [2007-11-26 15:39:33]
# 1
Because you are passing by calue not refernce.You are just adding 100 to the nu which is local to calc and not the num inside main.Recieve the num from calc to change the num inside main
qUesT_foR_knOwLeDgea at 2007-7-8 21:57:57 > top of Java-index,Java Essentials,New To Java...
# 2
int num =34;System.out.println("num="+ calc(num) );
masuda1967a at 2007-7-8 21:57:57 > top of Java-index,Java Essentials,New To Java...
# 3

Because you are passing by value not reference.You are just adding 100 to the num which is local to calc method and not the num inside main.Recieve the num from calc to change the num inside main.

This way

public class test {

public static void main(String[] args) {

int num;

num=34;

num=calc(num);

System.out.println("num="+num);

}

static int calc(int num){

num+=100;

return num;

}

}

qUesT_foR_knOwLeDgea at 2007-7-8 21:57:57 > top of Java-index,Java Essentials,New To Java...
# 4
Thanks for the input... I see what you mean, its still hard to grasp for a newcomer like me.
DeChristoa at 2007-7-8 21:57:57 > top of Java-index,Java Essentials,New To Java...
# 5
> Thanks for the input... I see what you mean, its> still hard to grasp for a newcomer like me.Thats ok
qUesT_foR_knOwLeDgea at 2007-7-8 21:57:57 > top of Java-index,Java Essentials,New To Java...
# 6
Hey the dukes
qUesT_foR_knOwLeDgea at 2007-7-8 21:57:57 > top of Java-index,Java Essentials,New To Java...
# 7
How do I give dukes to someone? If I assign dukes to a question, do they automatically go to the first responder?
DeChristoa at 2007-7-8 21:57:57 > top of Java-index,Java Essentials,New To Java...
# 8
....ok, found out how.
DeChristoa at 2007-7-8 21:57:57 > top of Java-index,Java Essentials,New To Java...