Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

void

 
K Robert
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following:
public static void main(String[] args)
{
System.out.println("Something here...")
}
"void" indicates that this method will peform a task (displaying a line of text), but doesn't return any information when it completes the task. Well, wouldn't the string be information that is returned? I am confused.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this method will peform a task (displaying a line of text), but doesn't return any information when it completes the task

in this case, main does not return anything when it's done. like you said, this method DOES something (printing the line). AFTER THAT, it doesn't return anything.
In C, your main method could return a value to the operating system. This could be written/used to see if the program exited "normally" or "abnormally". I've never seen a main() in Java that did return something, but i'm not saying you can't.
does that help?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ken,
When talking about computers, you have to be creful to define your terms carefully. "returned" has a very specific meaning: it means that the data is available to whatever code calls the method, like

Here "substring" is a method declared to return type "String", and we see how this code calls substring and gets the returned information and puts it in a variable "returnedInformation", which gets the value "Hello".
Now in your example, the "information" is displayed to the screen; if some other code invoked your main() method, it would not have access to what had been displayed. Thus this method returns nothing.
Does this answer your question?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String information is what is written out to the console but nothing is returned. Perhaps another example:

getRandomInt() writes a String to the console but it returns an int.
 
K Robert
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay! So, the method does something in "itself" but doesn't produce a variable that can be used by another class or method? So, in layman's terms: I think of something in my head but, because it's "void", I don't let anyone know what that thought is?
Thanks for the help!
Ken
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic