user5200061
user5200061

Reputation:

Java Void Methods Return

Java: Why is a method called void (ie. it doesn't return anything) if it returns this:

System.out.println("Something");

For example:

public static void sayHello() {
        System.out.println("Hello");
}

It does return something, this message!

Upvotes: 0

Views: 2360

Answers (8)

Dan Forbes
Dan Forbes

Reputation: 2824

sayHello() has no return statement; therefore, it is a void method. By your assumption, the only truly void method would look like this: public static void wowSuchVoid(){ }. What's even the point?

Upvotes: 0

Ramn Singh
Ramn Singh

Reputation: 36

It's simple and straightforward because you are asking him to execute something not returning something . your method returns a void which means nothing. Being a c programmer, In C a method that returns nothing is called procedure. for more checkout What is the difference between a "function" and a "procedure"?.

Upvotes: 0

Brick
Brick

Reputation: 4272

"Return" in this case means that a value is passed back through the stack that could be assigned to a variable in the calling code. That's not the case in your example.

Object o = sayHello(); // WRONG - Compile error

A void method can do things - In your case print to the screen. That's not a "return" in these described here.

Since the question shows at the top of the page as "Java Void Methods Return" with a capital "V" on "Void" it may also be worth noting that Java has a class "Void" in addition to the keyword "void." A method declared to return Void does return something - but if that's your case, you should check the documentation for that class because it's kind of a special case.

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109613

In programming language history there was Algol68, possibly the best procedural language of all. It was the first fully defined language, and everything was typed. It was so to say and expression language.

In it VOID was a type with a single value SKIP. A PROC () VOID, a method, could be coerced to VOID, doing a call.

Upvotes: 1

Filipp Voronov
Filipp Voronov

Reputation: 4197

This method does something (prints "Hello"), but it doesn't return anything. If it returned a value, you'd be able to do this:

aVariableToAssignReturnValue = sayHello(); //you can't do it!

Read this for example.

Upvotes: 1

rajah9
rajah9

Reputation: 12339

Consider these two routines.

public void sayHello() { System.out.println("Hello"); }

public int giveANumber() { System.out.println("Hi"); return 42; }

How would you call them?

sayHello();
int i = giveANumber();

Since sayHello is void, there's no equals sign and nothing on the left-hand side when you call it. However, since giveANumber returns an int, you should call it with the equals sign and an integer to receive the value on the left-hand side.

Upvotes: 1

humazed
humazed

Reputation: 76962

It doesn't return anything that can be stored in variable.

so it's simply don't return anything.

but it can do things like print to console.

Upvotes: 0

Matt Cremeens
Matt Cremeens

Reputation: 5151

I would say it prints a message to standard output, but it does not return anything to the calling statement.

Upvotes: 3

Related Questions