How value types are different to reference types? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

How value types are different to reference types?

Why doesn't it print the incremented value, is it predefined that value types will not be incremented and if it is why is it so?

27th Sep 2016, 4:09 AM
Abhishek Yadav
Abhishek Yadav - avatar
4 Answers
+ 1
public class MyClass { public static void main(String[ ] args) { int x = 5; int y = addOneTo(x); System.out.println(y); } static int addOneTo(int num) { num = num + 1; return num; } } Try to run this code. After making some enhancements you will see that it prints incremented value.
29th Sep 2016, 4:29 PM
Rohit Kumar
Rohit Kumar - avatar
+ 1
Now as you read value type only affect the value of the variable but using reference type will affect the value at the memory of the variable. Value type is like working one particular value while reference type is working on the memory, reference point of the variable.
29th Sep 2016, 4:33 PM
Rohit Kumar
Rohit Kumar - avatar
0
So no answers?
28th Sep 2016, 4:05 AM
Abhishek Yadav
Abhishek Yadav - avatar
0
Thanx
30th Sep 2016, 4:34 AM
Abhishek Yadav
Abhishek Yadav - avatar