0

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!

  • 2
    I would say it prints a message to standard output, but it does not return anything to the calling statement. – Matt Cremeens Aug 20 '15 at 20:02
  • https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html might be worth a read (also note that to return something you have to use a [`return`](https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.17) statement) –  Aug 20 '15 at 20:45
  • @slartidan Done. See below. Thanks. – Matt Cremeens Aug 20 '15 at 20:48
  • 1
    what you're describing isn't a return value, it's a side-effect. see [What is a side effect?](http://programmers.stackexchange.com/q/40297/7460) – Nathan Hughes Aug 20 '15 at 21:03

8 Answers8

3

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

Matt Cremeens
  • 4,951
  • 7
  • 38
  • 67
1

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.

Filipp Voronov
  • 4,077
  • 5
  • 25
  • 32
1

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.

rajah9
  • 11,645
  • 5
  • 44
  • 57
  • This answer is wrong and / or misleading to the extent that it suggests that you "should" call a method that returns a value with the equals sign and a variable to receive the value on the left-hand side. It's true that in many practical cases, that is what you'll do, but there's no requirement to capture the return value. "sayHello" does not return a value, so it's wrong (compiler error) to make it the right-hand side of an assignment. "giveANumber" returns a value, which you may *optionally* assign if it's useful to you. There are several other answers to this question that are better. – Brick Aug 21 '15 at 03:36
  • @Brick, the answer is neither wrong nor misleading. You say "optionally" in your comment; I say "should" in my answer. – rajah9 Aug 21 '15 at 12:56
  • Your answer says that the difference has to do with the way that the method is called, and that is false. The difference is inherent to the method declaration itself. I think a reader confused on the issue might conclude, for example, that `giveANumber()` is a void method if you call it without assigning the return to a variable. But that's wrong. The method `giveANumber` is not a void method no matter how you call it. In your example, if all you want is "Hi" printed, you're totally good in this case *not* capturing the 42 that's returned. In fact, in that case, you *should not* assign it. – Brick Aug 21 '15 at 13:51
1

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • # Consider the following ARRAY of UNIONs - elements are either EMPTY or an INT... or "undefined" # `[]UNION(VOID,INT) eis = (EMPTY, 666, SKIP); FOR i TO UPB eis DO printf(($"eis["d"]="g"; "$,i, CASE eis[i] IN (VOID):"e", (INT):"i" OUT "~" ESAC )) OD` # Output "was": eis[1]=e; eis[2]=i; eis[3]=i; # – NevilleDNZ Aug 24 '15 at 11:52
  • @NevilleDNZ snif, thanks. For others: Algol68 used reversed key words as end tokens: DO OD, IF FI, CASE ESAC, and `CASE i IN ... : ..., .... OUT ... ESAC` was the switch statement. – Joop Eggen Aug 24 '15 at 13:11
  • Re: do ~ od and case ~ esac blocks .... I suspect it was Dijkstra who coined the term&concept "guarded commands" ... Cf. https://www.cs.utexas.edu/users/EWD/transcriptions/EWD04xx/EWD418.html – NevilleDNZ Aug 24 '15 at 13:30
  • EWD's paper is dated 1974, so could post-date Algol68's usage. cf. "[Guarded commands. non-determinacy and a calculus for the derivation of programs](http://www.cs.utexas.edu/users/EWD/ewd04xx/EWD472.PDF)". by Edsger W.Dijkstra – NevilleDNZ Jan 23 '16 at 08:32
0

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.

humazed
  • 74,687
  • 32
  • 99
  • 138
0

"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.

Brick
  • 3,998
  • 8
  • 27
  • 47
0

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"?.

Community
  • 1
  • 1
Ramn Singh
  • 36
  • 3
0

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?

Dan Forbes
  • 2,734
  • 3
  • 30
  • 60