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?

26th Aug 2021, 12:32 PM
William Davis
William Davis - avatar
4 Respostas
+ 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.
26th Aug 2021, 12:46 PM
Abhay
Abhay - avatar
+ 1
No, It is not returning anything. It is simply displaying x on console.
26th Aug 2021, 3:53 PM
Harshit Jawla
Harshit Jawla - avatar
+ 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.
26th Aug 2021, 5:50 PM
Tim
Tim - avatar