0
Why does this method return something while void is being used?
Hi guys, This is my first post, thanks in advance for any replies. I learnt that void means that a method does not return anything. How come that the method 'sayHello()' is still able to return a string? class MyClass { static void sayHello() { System.out.println("Hello World!"); } public static void main(String[ ] args) { sayHello(); } } // Outputs "Hello World!"
5 Answers
+ 18
It doesn't return anything to another function⊠It just prints in the consoleâŠâŠ
+ 2
void tell to compiler that the method doesn't return a value. Otherwise you had to put a type of return e.g. String,int,etc... and use a return statement in the method.
+ 2
Here main method is calling the sayHello() method and that sayHello() method is not returning anything back to the main method. Hello World! is not being returned back to the main method. It will be just printed.
0
Thank you for your answer.
That's exactly the thing i don't understand.
Then what is void doing here?
edit:
So Java acts different on returning a method (System.out.println();) than returning a value of a certain datatype.
In other words; the void word is only applicable on the returning of data types and not on methods.
Thank you guys.
0
Thanks,
For now this is clear to me, thank you community!