0

Hey I really got confuse with the return in java.Please help me to solve my misery...Thanks in advance:)

public static void main(String[] args){


    System.out.println(addThem(1,2));

}

public static int addThem(int a, int b){

    int c = a + b;

    return c;
}

I can understand that in the code above return "c" as a new variable/result of addition. Here is another method that make me confuse>>

public static void main(String[] args){

    int d = 5;
    tryToChange(d);

    System.out.println("tryToChange d = " + d);     
}

public static void tryToChange (int d){

    d = d + 1;
    System.out.println("tryToChange d = " + d);
}

when I try to execute this code it return

tryToChange d = 6
tryToChange d = 5

it shows that the value in method "tryToChange" is returning the value of 5+1, It also returns value.but why is it void?

Tobb
  • 11,850
  • 6
  • 52
  • 77
Mr.Curiosity
  • 69
  • 2
  • 4
  • 2
    possible duplicate of [what is the difference between pass by reference and call by reference?](http://stackoverflow.com/questions/3660180/what-is-the-difference-between-pass-by-reference-and-call-by-reference) – Uwe Plonus Oct 06 '14 at 08:16
  • For that last bit ("it shows that the value in method "tryToChange" is returning the value of 5+1, it also returns value.but why is it void"), note that printing out a value is not the same thing as returning a value. – Dennis Meng Oct 08 '14 at 04:23
  • @DennisMeng yes sir! you have just make it more clearer for me to understand^^ Thanks:) – Mr.Curiosity Oct 08 '14 at 07:59

6 Answers6

1

In both methods, the variable d is a method-scope variable. This means that it is only available for the method itself. Further, this means that the variable d is actually two separate variables, one in each method. The method tryToChange only changes it's own local variable (declared as a parameter in the signature), it does not change the d variable in the main method (which is a separate variable).

So in main, d will get the value 5, and keep this value throughout the execution. In tryToChange, it will recieve the value 5, add 1 to it and print the result (6). Upon returning, the d-variable of the main method will be printed (and this is unchanged, so it will print 5.)

In Java, primitive values (int, char, etc) are passed by value. This means that changing such a parameter will not change anything outside the method itself. On the other hand, we have pass by reference, which applies for objects (for instance lists), which means that you can actually modify the list itself (but not the variable holding the list in the calling method.)

public void a() {
    final List<String> myList = new ArrayList<>();
    b(myList); 
}

public void b(List<String> something) {
   something.add("something"); //Changes myList in a().
   something = new ArrayList<>(); //Does nothing to myList in a().
}
Tobb
  • 11,850
  • 6
  • 52
  • 77
1
  1. tryToChange does not return a value, it just prints it to out.
  2. Primitives are passed-by-value in java, so the second printing to out prints 5 which is the value of the local variable d. See: is-java-pass-by-reference-or-pass-by-value

You could "wrap" your int in a class to see how pass-by-reference (well, actually it's pass by value still but the reference is the value) behaves:

class IntWrapper {
   public int d;

   public IntWrapper(int d) {
       this.d = d;
   }
}

public static void main(String[] args){

    IntWrapper wrapper = new IntWrapper(5);
    tryToChangeWrapped (wrapper); // will print 6

   System.out.println("tryToChange d = " + wrapper.d); // will print 6
}

public static void tryToChangeWrapped (IntWrapper wrapper){

    wrapper.d = wrapper.d + 1; // or wrapper.d += 1 or wrapper.d++
    System.out.println("tryToChangeWrapped d = " + wrapper.d);
}
Community
  • 1
  • 1
heikkim
  • 2,955
  • 2
  • 24
  • 34
0

Your tryToChange() function is not returning any value. hence it's void. Its very clear that you are calling tryToChange Function which performs the task of printing the value passed to it.

Codeek
  • 1,624
  • 1
  • 11
  • 20
0

First:

All primitives in java are pass-by-value so it just pass the value to the method tryToChange and tryToChange just prints it after incrementation also it does not return any value as its return type is void.

Second:

While in main it just takes its local variable d and prints it locally. that's why the output is like.

tryToChange d = 6
tryToChange d = 5
Qadir Hussain
  • 1,263
  • 1
  • 10
  • 26
0

You just need to change your code

public static void main(String[] args) {
    int d = 5;
    System.out.println("Current value if d: " + d);
    d = tryToChange(d);// assign returning value to d 
    System.out.println("New value of d: "+d);
}

public static int tryToChange(int d) { // needs to return value from here
    d = d + 1;
    return d;
}

Out put:

Current value if d: 5
New value of d: 6
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

tour both case are different. first of all you need to understand the use of return statement. "Return" statement always return the value.

in your first case :-

public static void main(String[] args){


    System.out.println(addThem(1,2));

}

public static int addThem(int a, int b){

    int c = a + b;

    return c;
}

you are calling addThem() method. that add two value and the sum is stored into the c variable then next you are returning the variable c. but in actuality variable c is not returning but c variable value that is the sum of (a+b) is returning. so in this case jvm returning the value of variable c not the variable c.

in your second case :-

public static void main(String[] args){

    int d = 5;
    tryToChange(d);

    System.out.println("tryToChange d = " + d);


}

public static void tryToChange (int d){

    d = d + 1;
    System.out.println("tryToChange d = " + d);
}

now you are caling the tryToChange() method. in that when this method gets called it take the value of variable d not the variable d. and tryTOChange() method defination gets the value of d.

here d variable in both method main() and tryTOChange() are different. because d variable declare as a local variable. so when main() method call the tryTochange() method in both d variable has same value. but when d is incremented by one then d variable's value in main method is 5 or in tryTOchange() is 6 because of different scope. so when sop print d in tryToChange() prints 6 and in main method it prints 5. and when tryTochange() method finish there body it is not returning no value to main method. that why d variable in main method has value 5