Reputation: 83
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?
Upvotes: 2
Views: 42885
Reputation: 1
It boils down to what a method call does to your CPU. And in that world there is always a result of a call.
CPU has a specified number of 'variables' that are called registers. Special one on most architectures is called "accumulator" (A register on x86 types) and after executing a call it holds the return value or a pointer (location in memory) to this value. This register is a physical location on each core and it always exists.
When you write a high level language program, you tell the compiler what type the result of the call is. void means the result must be ignored. In practice it will have a random value from some previous operation or just the first method's argument.
In Java, void type makes it impossible to examine the value. Which is good, because there is no sense in it. In some languages you could still retrieve the value.
When your program is translated to an executable form, all the methods are written to a location in memory. Routine of calling such method on the CPU varies between languages and architectures, but most likely would involve:
Upvotes: 0
Reputation: 719616
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:
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 ...
Upvotes: 6
Reputation: 81
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.
Upvotes: 1
Reputation: 2881
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.
Upvotes: 1
Reputation: 403
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.
Upvotes: 2
Reputation: 2465
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
Upvotes: 1