why not 6? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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??

11th Nov 2016, 5:26 PM
Lauren Ldp
Lauren Ldp - avatar
17 Answers
+ 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..
11th Nov 2016, 5:57 PM
Manoj
Manoj - avatar
+ 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 ;)
11th Nov 2016, 11:58 PM
George Rabbat
George Rabbat - avatar
+ 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; } }
11th Nov 2016, 6:07 PM
John Kovanis
John Kovanis - avatar
+ 4
try public static void main(String[] args){ int x = 5; addOne(x); } static void addOne(int y){ y += 1; System.out.println(y); }
11th Nov 2016, 5:41 PM
Wanderlei Borges
Wanderlei Borges - avatar
+ 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.
11th Nov 2016, 5:44 PM
Giulio Pellitta
Giulio Pellitta - avatar
+ 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;}
20th Feb 2017, 6:41 AM
David Oats
David Oats - avatar
0
write this.num = this.num + 1;
11th Nov 2016, 5:36 PM
Maurizio Urso
Maurizio Urso - avatar
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; }
11th Nov 2016, 5:42 PM
Maurizio Urso
Maurizio Urso - avatar
0
Oh thank you guys! I understand perfectly, it is very helpful.
11th Nov 2016, 6:17 PM
Lauren Ldp
Lauren Ldp - avatar
0
add, System.out.println(num); then you see the value returned is 6.
12th Feb 2017, 9:02 PM
srivasu
0
the value of num doesn't return to x
25th Feb 2017, 2:38 AM
ddd
0
In Java x=4.5 System.out.println(x==++x);
21st Feb 2018, 5:24 PM
Mohd Mustafa
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); } }
29th Aug 2020, 3:53 PM
Amanpreet Singh
Amanpreet Singh - avatar
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"
8th Oct 2020, 12:06 PM
Bishal Kumar Sarkar
Bishal Kumar Sarkar - avatar
- 1
in line 9 : x=num+1;
11th Nov 2016, 9:21 PM
store
- 1
because of void. void never return a value. if you want return a value, dont use void. use int func () etc...
8th Dec 2016, 1:03 PM
Cenk Camkıran
Cenk Camkıran - avatar
- 2
it doesnt work..
11th Nov 2016, 5:41 PM
Lauren Ldp
Lauren Ldp - avatar