value type but it affects the same as object | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

value type but it affects the same as object

public class MyClass { static void addOneTo(int num) { System.out.println(num + 1) ; } public static void main(String[ ] args) { int x = 5; addOneTo(x); } } can anybody explain it's value type but it affects by the method addOneTo(x)

15th Oct 2017, 8:16 PM
Nour Eldin CHARIFH
Nour Eldin CHARIFH - avatar
3 Answers
+ 17
the code is fine & will work ☺ //method ::: addOneTo () //parameter ::: x //output ::: 6
15th Oct 2017, 8:23 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 15
method is addOneTo //see last line u typed @nour
16th Oct 2017, 2:40 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 3
5 is passed to the method addOneTo(); So, num is 5. So, 5 + 1 is printed. Pass by values means num and x are independant. If I increment num, x will not be incremented too.
15th Oct 2017, 8:36 PM
Rrestoring faith
Rrestoring faith - avatar