+ 1

So if void doesnt return a value then whats the point if it? (As in what does void do?)

2nd Jun 2017, 11:26 PM
Joshua Lazaro
Joshua Lazaro - avatar
10 odpowiedzi
+ 13
Not all methods need to give back a value once they do their task. They simply do their work when they're called, and don't produce a value. But all methods must have a return type, so void is used to return nothing.
2nd Jun 2017, 11:35 PM
Tamra
Tamra - avatar
+ 11
class Demo1 { public static void main(String[] args) { System.out.println("Hello world!"); } } this does not return anything. But print some message.
2nd Jun 2017, 11:32 PM
김정제(Legacy)
김정제(Legacy) - avatar
+ 10
Void function and method do not return value but do some work like print some value.
2nd Jun 2017, 11:30 PM
김정제(Legacy)
김정제(Legacy) - avatar
+ 10
@Joshua Lazaro If you use same content more than once. It is better to make the content to function
2nd Jun 2017, 11:39 PM
김정제(Legacy)
김정제(Legacy) - avatar
+ 4
To tell the caller: "Hey, I'm a method, I do some useful task, but I do not return anything!" So the caller, knowing this, can not expect something to be returned.
2nd Jun 2017, 11:42 PM
Aibek T.
Aibek T. - avatar
+ 3
Void in Java is a uninstantiable placeholder type. The compiler requires that all Method signatures contain a return type. Void is a placeholder type that is used to tell the compiler that nothing is being returned and that the return keyword is not required for this method.
2nd Jun 2017, 11:38 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Ahhh, okay Thanks!!!!
2nd Jun 2017, 11:40 PM
Joshua Lazaro
Joshua Lazaro - avatar
+ 3
Yes, but that is not the same as returning a value or object from a method. int x = getInt(); String stuff = printSomething("Stuff"); // this would produce an error as nothing is returned from the method to be stored in the stuff variable. public static void printSomething(String something) { System.out.println(something); } public static int getInt() { return 42; }
2nd Jun 2017, 11:43 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
But cant you just use a simple system.out.println to print something out?
2nd Jun 2017, 11:33 PM
Joshua Lazaro
Joshua Lazaro - avatar