Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Return and System.out.println  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we create a program and set the entry point to:



From my understanding void is a datatype, and a datatype is just a word in java that holds onto a particular value, and so void by definition holds onto no value, so when we go to return void it returns nothing because it holds onto nothingness. But how is that true when the program returns "Hello World", that's a string but void is defined as holding nothing but how did it return "Hello World"?


Also, is public static void main(String[] args){} just one big method? and everything within its brackets is the methods definition? if so, why don't we ever call main onto objects? really confused about some of these fundamentals

THank you
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println() doesn't return anything; I wouldn't say it returns "void". A method with a void return type simply states that when the method returns, there is no value attached to it.

public static void main(String[] args){} is indeed a method like any other. Java looks for a method with this signature to start with. You could, in theory, call main() from an object, but it would be a horrible idea.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66198
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing to an output stream is no way, shape or form "returning" anything. It's just writing to an output stream.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider void as a sort of analogue to null. If you write null it means no object; if you write void it means no type returned. If null means non‑object, then consider void as non‑datatype. I believe the only place you can use void is instead of the return type of method. There is also a Void class, though I cannot think of a use for it.
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:But how is that true when the program returns "Hello World", that's a string but void is defined as holding nothing but how did it return "Hello World"?

That's not true at all. The program does not return anything. What it does is it prints the string "Hello World" to System.out, which is usually associated with the system console. So, yes, when you run your program you'll see the string "Hello World" displayed on the console but that did not happen because of a return value, it happened because of what the System.out.println() method did when you called it on line 2.
 
Liutauras Vilda
Marshal
Posts: 4636
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this method:
This method return type is int, nevertheless, before it returns sum of a and b as an int, it will print Hello World and sum of a and b to the console.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless your method has a return statement in it, it doesn't return anything.
 
Liutauras Vilda
Marshal
Posts: 4636
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Unless your method has a return statement in it, it doesn't return anything.
With exception. There are some situations, when you do have a return statement but still don't return anything, but rather just exit method.

But yes, in most cases you'll encounter in near future, Stevens statement will be correct.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Stevens Miller wrote:Unless your method has a return statement in it, it doesn't return anything.
With exception. There are some situations, when you do have a return statement but still don't return anything, but rather just exit method.

But yes, in most cases you'll encounter in near future, Stevens statement will be correct.

Actually, my statement is correct even for the example you gave. While a method with a return in it might not return anything, a method that returns something must have a return statement in it. Justin's main method has no return statement in it. Therefore, it never returns anything.
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:
From my understanding void is a datatype, and a datatype is just a word in java that holds onto a particular value, and so void by definition holds onto no value, so when we go to return void it returns nothing because it holds onto nothingness. But how is that true when the program returns "Hello World", that's a string but void is defined as holding nothing but how did it return "Hello World"?


Also, is public static void main(String[] args){} just one big method? and everything within its brackets is the methods definition? if so, why don't we ever call main onto objects? really confused about some of these fundamentals

THank you


Justin, let's say for instance we have two methods that in essence do similar things.

We have:



and



The difference is that you are not printing something via your method to output (like the void method demonstrated here). In the public String getHelloWorld() method, we are returning an actual String literal, but we are not printing anything to output. Now if you wanted to print something in your main method it could be like this.



I hope that helps.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:. . . Actually, my statement is correct even for the example you gave. . . .
This is not SM being overpedantic, but an example of the precise way you have to think when you are programming. The computer is very deterministic and will take everything completely literally. So you must be very precise with what you say.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Stevens Miller wrote:. . . Actually, my statement is correct even for the example you gave. . . .
This is not SM being overpedantic...

For once.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!