+ 1

Meaning of 'void'

hi All, I'm having trouble understanding the concept of methods returning/not returning data. for example : class MyClass { static void sayHello() { System.out.println("Hello World!"); } public static void main(String[ ] args) { sayHello(); } } in the Java tutorial it says that the sayHello() method doesn't return any data because it is classified as 'void' but, as I see it ( wrongly obviously :) ), the method does return data - it prints "Hello World". What am I not getting ? Thanks in advance for any help given ~ ——————————EDIT————————— After reading all the answers, I guess my question is : What is the difference between the original code ( written above ) and this code, in terms of syntax, as it has the same result ? class MyClass { // I wrote ‘String’ instead of ‘void’ static String sayHello() { System.out.println("Hello World!"); } public static void main(String[ ] args) { sayHello(); } }

3rd Nov 2018, 2:10 PM
Robert Iticovici
Robert Iticovici - avatar
10 RĂ©ponses
+ 9
It prints data not returns it. Returning data is hard to explain. I used to think that the returned data kinda replaces the function call. class MyClass { static String sayHello() { return "Hello World!"; } public static void main(String[ ] args) { System.out.println(sayHello()); } }
3rd Nov 2018, 2:16 PM
Toni Isotalo
Toni Isotalo - avatar
+ 7
Example: Return Value from Method class Square { public static int square() { // return statement return 10 * 10; } public static void main(String[] args) { int result; result = square(); System.out.println( "Squared value of 10 is: "+ result); } }
3rd Nov 2018, 3:31 PM
Danijel Ivanović
Danijel Ivanović - avatar
3rd Nov 2018, 3:34 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 5
In you're code, the sayHello() function is not returning anything. Even though it is printing out text, that value cannot be returned to the place where the function is called (in this case, the main() function), and would only be able to do that if you were to set the function as a string and return the text rather than print it directly in the function (i.e. returning a value allows you to assign that value to a variable or use it in a function, but because you're just printing it out, you wouldn't be able to do that)
3rd Nov 2018, 2:20 PM
Faisal
Faisal - avatar
+ 5
Return Type: Java Methods must specify the return type. They can be any primitive type or an object. If the method is not returning anything, then we must use void as return type.
3rd Nov 2018, 3:21 PM
Danijel Ivanović
Danijel Ivanović - avatar
3rd Nov 2018, 2:12 PM
PureLogicality
PureLogicality - avatar
+ 4
How to write methods efficiently https://www.sololearn.com/post/44876/?ref=app
29th Nov 2018, 11:36 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 3
Examples: static int getHello() { return "Hello!"; } void int sayHello() { System.out.println("Hello!"); } You could put it this way, when I use getHello() what I get in return is "Hello!" which I could then store in a variable, perform calculations, or even use it straight away. sayHello() just says "Hello!". I won't be able to do anything with it except say hello.
3rd Nov 2018, 2:28 PM
jtrh
jtrh - avatar
0
a little more explaining of my question : the method sayHello in my example only prints "Hello World" when it is called, it doesn't do so automatically. From what I understand, no method does anything until it is called - so why specify that a method returns nothing (void) when it is obvious that it will only do something when we call it ?
3rd Nov 2018, 2:55 PM
Robert Iticovici
Robert Iticovici - avatar
0
When you want to reuse a block of code at different times. Another good use would be used to do calculations: static int sum(int a, int b) { return a + b; } A function can accept 'arguments' and return a calculated result. The way you would use the above method would be: int result = sum(1, 2); <- results in 3 You would probably want to do more complicated calculations than that again and again so it would be good to define a function to calculate as you please. It is a good way of simplifying code and making it understandable.
3rd Nov 2018, 3:01 PM
jtrh
jtrh - avatar