public class Inc{ public static void main(String argv[]){ Inc inc = new Inc(); int i =0; inc.fermin(i); i = i++; System.out.println(i); } void fermin(int i){ i++; } } who can explain why the assignment to var i has no effect? thank you
I have been reading the boards for a while and now ready to start giving back. I hope to help and not add to the confusion.
I think the difference is that primitives are passed by value and objects are passed by reference. Here is some code demonstrating the object by reference.
yes ..here the point is that i value is passed by value to the method..so if we increment i..it is local to the method...and when it is returned i is not incremented...and also..after returning from the method..i is again post incremented so i is not incremented again..hence the value of i remained 0...
I think the difference is that primitives are passed by value and objects are passed by reference.
Actually all parameter passing in Java is by value. When the formal parameter and actual paramters are object references, then a copy of the reference is passed to the method.
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop