+ 1
So if void doesnt return a value then whats the point if it? (As in what does void do?)
10 Respuestas
+ 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.
+ 11
class Demo1
{
public static void main(String[] args)
{
System.out.println("Hello world!");
}
}
this does not return anything. But print some message.
+ 10
Void function and method do not return value but do some work like print some value.
+ 10
@Joshua Lazaro
If you use same content more than once. It is better to make the content to function
+ 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.
+ 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.
+ 3
Ahhh, okay Thanks!!!!
+ 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;
}
+ 1
But cant you just use a simple system.out.println to print something out?