+ 7
why not 6?
public class MyClass { public static void main(String[ ] args) { int x = 5; addOneTo(x); System.out.println(x); } static void addOneTo(int num) { num = num + 1; } } int x is 5, addOneTo method take 5 value, so I think finally output will be 6. but the answer is 5. why not 6??
17 Antworten
+ 16
addOneTo(x) -> when u call this function u r actually giving it the value of x, i.e 5 in this case.. so addOneTo(5).
so what the function addOneTo does is keep the value passed to it in another int variable 'num' then it increases the value of 'num' by 1..
so it's actually increasing the value of 'num' and not 'x'... and you have printed 'x' in SOP.. so it will show the value of x which is 5.. and which never changed..
+ 7
num is new variable and copy the value of x to it
when you changed num you changed variable called num not x.
its called passing by value parameters
find how to pass it by reference ;)
+ 6
try this!
public class Program {
public static void main(String[] args) {
int x=5;
x = addOne(x);
System.out.println(x);
}
static int addOne(int y)
{
y +=1;
return y;
}
}
+ 4
try
public static void main(String[] args){
int x = 5;
addOne(x);
}
static void addOne(int y){
y += 1;
System.out.println(y);
}
+ 4
In short, Java is call by value (or pass by value). So the function addOne does not change the integer passed as an argument, but just its value.
This is clear enough for basic types like integers or booleans. However, for objects it is a bit more subtle. When an object is passed, it is actually the reference to the object itself that is passed. So if you use a setter method on the object within the function, you can see its effect also outside the function (after the function call has completed).
On the other hand, since Java is call by value, if you assign the variable another object this has no effect outside the scope of the function.
+ 1
the value x is not passed by reference to value num so num won't increment 5 to 6. you will have to put.............
static void addOneTo (int num)
{this.num += 1;}
0
write this.num = this.num + 1;
0
yes because you are using the variable of the class inside a method...
and i have done a mistake. write
static void addOneTo() {
this.x = this.x + 1;
}
0
Oh thank you guys! I understand perfectly, it is very helpful.
0
add, System.out.println(num); then you see the value returned is 6.
0
the value of num doesn't return to x
0
In Java
x=4.5
System.out.println(x==++x);
0
If you follow this you will got 6
public class MyClass {
public static void main(String[ ] args) {
int x = 5;
addOneTo(x);
System.out.println(x);
}
static void addOneTo(int num) {
num = num + 1;
System.out.println(num);
}
}
0
public class MyClass {
public static void main(String[ ] args) {
int x = 5;
addOneTo(x); // this method doesn't have system.out.println().
System.out.println(x); // that's why print this value = 5
}
static void addOneTo(int num) { // doesn't have system.out.println(). Thats why 6 can't print
num = num + 1;
}
}
// Outputs "5"
- 1
in line 9 :
x=num+1;
- 1
because of void. void never return a value. if you want return a value, dont use void. use int func () etc...
- 2
it doesnt work..