Hi All,
I am going through the java tutorials on the sun web site. I noticed
that their examples for the main method has a void return type. In
other languages you tend to use an int return type and send either a 1
or a 0 to the operating sytem when exiting.
I was wondering if you are meant to do this in java and if not why
doesn't java return a value to operating system as is typical in other
languages.
Below is a code example.
public class hello {
public static void main(String[] args) {
System.out.println("hello");
}
}
Cheers
Andrew Thompson - 08 Nov 2006 04:46 GMT
Sub: Quick Question
For 'quick answers', try more meaningful titles..
> I am going through the java tutorials on the sun web site. I noticed
> that their examples for the main method has a void return type. In
> other languages you tend to use an int return type and send either a 1
> or a 0 to the operating sytem when exiting.
Note that a Java application does not necessarily exit
at the end of the main. A good example is if a Frame is
constructed and set visible - GUIs have their own thread(s).
> I was wondering if you are meant to do this in java and if not why
> doesn't java return a value to operating system as is typical in other
> languages.
It can.
System.exit(0) // for a 'normal' exit code,
// (where n is a non-zero integer)
System.exit(n) // for anything else
Andrew T.
Simon Brooke - 08 Nov 2006 08:58 GMT
> Hi All,
>
> I am going through the java tutorials on the sun web site. I noticed
> that their examples for the main method has a void return type. In
> other languages you tend to use an int return type and send either a 1
> or a 0 to the operating sytem when exiting.
You mean, you do in C.
> I was wondering if you are meant to do this in java and if not why
> doesn't java return a value to operating system as is typical in other
[quoted text clipped - 7 lines]
> }
> }
System.exit( 1).

Signature
simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/
"The result is a language that... not even its mother could
love. Like the camel, Common Lisp is a horse designed by
committee. Camels do have their uses."
;; Scott Fahlman, 7 March 1995
Eric Sosman - 08 Nov 2006 15:58 GMT
C Student wrote On 11/07/06 23:42,:
> Hi All,
>
[quoted text clipped - 6 lines]
> doesn't java return a value to operating system as is typical in other
> languages.
Because in Java the program is not necessarily finished
when main() is finished. A Java program can consist of many
different execution threads, and the fact that just one of
them has terminated doesn't mean they all have.
A fairly common pattern is for main() to initialize the
application and then return, having completed its job. The
bulk of the application keeps on running, using other threads.

Signature
Eric.Sosman@sun.com
www.pulpjava.com - 08 Nov 2006 23:15 GMT
A void method is void of a return. That's why it's void.
There are some great tutorials on Java and J2EE development here:
www.mcnz.com
Cheers!
-Cameron McKenzie
www.pulpjava.com
www.scja.com
www.examscam.com
www.technicalfacilitation.com
> C Student wrote On 11/07/06 23:42,:
> > Hi All,
[quoted text clipped - 16 lines]
> application and then return, having completed its job. The
> bulk of the application keeps on running, using other threads.