Can someone explain a void return type in Java?

The only void return type I have seen has System.out.println statements in a method. So once the method is called those Strings will be printed.
Couldn't you make the return type string and have the string returned instead of doing void return type?

If the void return type method has other methods in it could you make the return type the value which the method gives which would return the outcome of that method?

When is it that you could only use the void return type?


Can someone explain a void return type in Java?

The void type is used to declare that a method does not return a value.

Couldn't you just make the return type String and set the string equal to the parameter to make it show up when the method is called?

Hypothetically "you" (or in this case, the designers of the PrintStream API) could do that, but there is no point in doing it. I am struggling think of a plausible use-case where it would make sense to use the println argument String ... if it was returned as a result.

Bear in mind the primary goals of a good API design include1:

  • to support the common use-cases well, and
  • to be easy for programmers to understand.

Methods that return values that either don't make sense or that are rarely (if ever) used are (IMO) poorly designed.

If the void return type method has other methods in it could you make the return type method which would return the outcome of that method?

Well, I guess so. But you've got the same issues as above. If the result is either rarely used or is hard to understand (and therefore hard to use correctly) then it is probably bad design to return it.

When is it that you could only use the void return type?

One case is where you are implementing or overriding a method in an interface or a superclass, and that method is declared with a void return type.

But in isolation, there are no cases where you can only use void. (But there are lots of cases where good design says that it is best to use void!)


1 - There other goals too. Please don't take this out of context ...


A void return type simply means nothing is returned. System.out.println does not return anything as it simply prints out the string passed to it as a parameter.

And..

Couldn't you just make the return type String and set the string equal to the parameter to make it show up when the method is called?

I have no idea what you are trying to say with this.


Simply put, void returns nothing and expects nothing to be returned.

you can read more about return here:

http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html


The void return type is used to say that the method doesn't return anything.

It can be a subject of debate whether methods should return void, one of the arguments (we might do that if there was only one), or this (we might do that if we want to support chaining of methods calls).

So this decision is part of class design.


Void is used when your method doesn't return anything.

Print statement is for printing not for returning. If you want a String to be returned from your method. remove void and put String keyword there. And as the last line of the method type the return statement. return s; s is your String variable.

If you write a method without return statement . you have to write that method as void . void means you are not returning anything. returning anything is closed.