I just start learning Java, now really confused about "void" can someone explain it with...

Quora Feeds

Active Member
Joshua Engel

A method is allowed to return a value, like this:

int calculate(int x, int y) {
int result = x+y;
return result;
}


You can call a method to perform some work and get that value back. So, if you write:

int z = calculate(3, 4);


Then after the method is done, z will be equal to 7.

What happened to result? It's gone. It lived only during the "scope" of the calculate method. Its value (7) was returned, but the variable is gone. There was never anything special about result. We could have called it sum or value or anything else. The only reason it's relevant to the return value is that it's in the return statement.

Unlike result, return is a special keyword of the Java language, telling the method to stop, and saying what the overall value of the method is. Other special keywords include int and void, the names of built-in types.

You don't always need a result. Sometimes, the method call is enough. For example, you could do this:

void calculateAndPrint(int x, int y) {
int result = x+y;
System.out.println("The result was: " + result);
return;
}


There's nothing to return. The point of the method was to calculate the value and print it out. You still use the return statement, but since there's no value actually returned, it just tells the method that it's over. (In fact, if it's the last line, Java ignores it.)

Since calculateAndPrint doesn't return anything, this code is an error:

int z = calculateAndPrint(3, 4); // ERROR!


The compiler will tell you that you can't assign a void. void is just a placeholder, telling the compiler not to expect a return. The language spec says you have to have some type there, and void is the special type indicating that there won't be anything. And if calculateAndPrint tried to return a value, that would also be an error.

In general, you do want methods to return something. Otherwise, all of its work is in the side effects, which can be hard to debug because side effects aren't as orderly as the nice, simple flow of inputs turning into return values. Printing something to the console is one side effect, and println is a void method.

Sometimes void methods alter the state of the object. For example, if result were a field, rather than a local variable:

class Calculator {
int result;

void calculateAndStore(int x, int y) {
result = x+y;
}
}



When you call calculate(3, 4), the result is computed and stored as a field, rather than in the local variable. Because it's declared outside the method, its lifetime goes past the method, and it lives as long as the object does. One way to use this:

Calculator calc = new Calculator();
calc.calculateAndStore(3, 4);
System.out.println("result is: " + calc.result);


Nothing got returned from calculateAndStore. Instead, it was stored inside the calc object, in a field called result as a side effect. In general, I try to avoid that kind of side-effect-oriented programming, but that's a matter of personal taste. I think that whenever possible, most methods should do their work and return it as a value rather than putting it as a side effect. When you can't do that, you'll have void as the return type, indicating that there isn't any return at all.

See Questions On Quora

Continue reading...
 
Top