wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes void Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "void" Watch "void" New topic
Author

void

K Robert
Ranch Hand

Joined: May 16, 2003
Posts: 116
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

Joined: Oct 02, 2003
Posts: 11160
    
  16


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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
K Robert
Ranch Hand

Joined: May 16, 2003
Posts: 116
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
 
 
subject: void
 
Similar Threads
Timer/ TimerTask
PriorityQueue Issue
Can i convert this code to use threads?
How do I exit?
implementing a timed/scheduled task