Program 1:
public class ValAndRefTypes {
public static void main(String[] args) {
int x=5;
addOneTo(x);
System.out.println(x);
}
static int addOneTo(int num){
num=num++;
return(num);//Outputs 5
}
Program 2:
public class ValAndRefType1 {
public static void main(String[] args) {
int a=2,b=3;
System.out.println("The sum is: " + Add(a,b));
}
static int Add(int num,int num1){
num++;
num1++;
return(num+num1); //Outputs 7
}
Why does the first program not output the incremented value of the variable 'x', but the second program outputs the incremented value of variables 'a' and 'b'?
I also would like to ask whether this has any relation with Value types and Reference types.
TIA for the answer!