Quick Question

C

C Student

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
 
A

Andrew Thompson

C Student wrote:

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

Simon Brooke

C Student said:
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
languages.

Below is a code example.

public class hello {
public static void main(String[] args) {
System.out.println("hello");
}
}

System.exit( 1).

--
(e-mail address removed) (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
 
E

Eric Sosman

C Student wrote On 11/07/06 23:42,:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top