0
Void and Main
So in this code, Sololearn said that you when you place void with Main, it means that it doesn't return anything. I get that somthing like: static int sum(int val1, int val2){ return val1 + val2; } it returns the sum of val1 and val2. but in: public static void main(String[] args){ System.out.println(x); } Isn't this returning the x to the user?
3 Antworten
+ 3
It isn't , it is printing the value to standard output .
void functions don't return a value . If there was no void keyword , then you had to specify what type of value it should return and use return statement , even then it would return it to the standard output since a program calls the main method not the user.
+ 1
No, It is not returning anything.
It is simply displaying x on console.
+ 1
This might be off topic and I'm not sure if this works in Java, but in C# you can use the keyword return to exit/end a void method.