0
Need help java newbie question
why does this code still prints "helloworld" when there is a word void before the sayHello() method?(i thought void means that it will not return anything?) class MyClass { static void sayHello() { System.out.println("Hello World!"); } public static void main(String[ ] args) { sayHello(); } } // Outputs "Hello World!"
2 Answers
+ 2
think of it like this...
static void Hello(){System.out.println("Hello World!");
main//
Hello(); //output "Hello World"
//is very similar to
static String x = "Hello World!";
//main
System.out.println(x); // output "Hello World!"
void is a return type it dosent return a string or an int it returns nothing when you call it its only printing hello world because the println method is attached to it..
0
It doesn't return anything. The words "Hello, World" are simply printed to the screen, and no value is returned to the main method after the sayHello() method is called.