Why does main() in java returns void ?

R

Razvan

Hi,




Why does the main() function in java returns void ? In C/C++ this is
considered BAD style. Why the Java designers took this approach ?

I am not contesting the decision as I am a newcomer in Java, but I
would like to know why this design decision was made.

With this approach the only way to return some values to the OS (or
JVM, which can return it to the OS) is to call System.exit(). But a
plain "return" won't work. Is this the real intention - to disallow
the use of "return" for sending an exit code to the OS ? Is so, why do
that ?


I am trying to give my own explanation to this; if I am wrong please
correct me.

In C/C++ if you wanted to exit the application you had to use the
exit() function with the exception of the main() function where the
return() function would do the job. For consistency reasons, in Java,
you must always call System.exit() when returning a value to the OS
while the plain return() function can only be used to transfer values
between function calls (but not to the underlying OS).





Regards,
Razvan
 
C

Christophe Vanfleteren

Razvan said:
Hi,




Why does the main() function in java returns void ? In C/C++ this is
considered BAD style. Why the Java designers took this approach ?

I am not contesting the decision as I am a newcomer in Java, but I
would like to know why this design decision was made.

With this approach the only way to return some values to the OS (or
JVM, which can return it to the OS) is to call System.exit(). But a
plain "return" won't work. Is this the real intention - to disallow
the use of "return" for sending an exit code to the OS ? Is so, why do
that ?


I am trying to give my own explanation to this; if I am wrong please
correct me.

In C/C++ if you wanted to exit the application you had to use the
exit() function with the exception of the main() function where the
return() function would do the job. For consistency reasons, in Java,
you must always call System.exit() when returning a value to the OS
while the plain return() function can only be used to transfer values
between function calls (but not to the underlying OS).

Because returning a value from main is broken in multithreaded programs.

consider this:

public static void main(String[] args) {
Thread t = new Thread() {
//do some long running stuff here
};
t.setDaemon(false);
t.start();
}

This thread could still be running long after the main method itself has
ended. Since the JVM doesn't halt execution until all non-daemon threads
have finished running, returning from the main method doesn't mean your
program ended.
 
C

Chris Smith

Razvan said:
Why does the main() function in java returns void ? In C/C++ this is
considered BAD style. Why the Java designers took this approach ?

Incidentally, this is considered bad style in C and C++ just because
it's the *wrong* signature for main, not for any universal reason
independent of programming languages. It's one of those things that is
not really supposed to work, but might on your implementation.

In Java, the reason main returns void is threads. C and C++ were both
designed as languages before multithreading was a widely known
technique, and both had threads grafted onto them at a later date. Java
was designed from the beginning to be a multithreaded environment, and
frankly, it would be unusual to write any non-trivial Java application
that doesn't use more than one thread. So the idea that a program moves
linearly from the beginning to the end of main is a bit outdated.

An artifact of this is that, being designed before threads, C and C++
both assume that the process will end when the main method returns. To
preserve that expectation even when multithread is used, threading for C
and C++ ends up being specified such that the entire process will exit
at the end of main, which means abruptly terminating any other remaining
threads.

Java doesn't do that. In fact, it's quite common to have the main
thread exit at the very beginning of an application, and use remaining
threads to do the main work. The vast majority of GUI applications, for
example, use the main method to create and display a component, and then
just return, allowing the component to live on its own. When the main
thread returns, the process is not ended, and in fact is just getting
started. That would be the wrong time to specify an exit code for the
application.

To give an example of that quandary, if main returned a process error
code, what would this code do?

public class ConfusedReturns
{
public static int main(String[] args)
{
new Thread(new Runnable() {
public void run()
{
Thread.sleep(1000);
System.exit(2);
}
}).start();

return 1;
}
}

main returns 1, but a second after main has finished, the other thread
says to exit with 2. So should 1 or 2 be the process error code? So
you see, when returning from main doesn't terminate the process,
returning an error code from main just doesn't make sense.

So in the end, Java decided that a process that doesn't specifically
signal an error should return no error (0). If you do need to signal an
error, then you should use System.exit, which lets you do so.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

Java
was designed from the beginning to be a multithreaded environment, and
frankly, it would be unusual to write any non-trivial Java application
that doesn't use more than one thread. So the idea that a program moves
linearly from the beginning to the end of main is a bit outdated.

Even the simplest program has several threads operating behind the
scenes. Do a thread dump in an applet. You will be surprised.
 
M

marcus

Razvan,
in the java world it is considered "BAD style" to compare everything
negatively to c/c++. I strongly recomment Peter van der Linden's Just
Java books, which have a strong element of coaching c programmers into
the java mindset.

BTW, there are hundreds, if not thousands, of programming languages in
the world. you will do well to learn the new language from the
fundamentals, rather than comparing the new language item by item ("how
do i do this", "why can't I do that") to the language you are familiar
with. That isn't really learning a new language, but kludgeing your old
habits and mindset onto a new syntax. mega BAD style.

-- clh
 
J

Java Architect

I don't think Razvan's comparison was negative. It came off as a question
(wish I had an answer) and a possible answer.

Also, learning by comparison is not bad style at all. It's how most people
learn.

Don't be so touchy :)
 
A

Andrew Thompson

....
I don't think Razvan's comparison was negative. It came off as a question
(wish I had an answer) and a possible answer.

I think marcus' answer had important points to be
considered ny the OP. Saying "'b' language does not
allow what 'a' language allows" is more often a
misunderstanding of some of the fundamental comcepts
of 'b' language, which possibly makes the need
unnecessary.
Also, learning by comparison is not bad style at all. It's how most people
learn.

Don't be so touchy :)

Back at ya'. ;-)
 
L

Liz

Razvan said:
Hi,




Why does the main() function in java returns void ? In C/C++ this is
considered BAD style. Why the Java designers took this approach ?

I am not contesting the decision as I am a newcomer in Java, but I
would like to know why this design decision was made.

With this approach the only way to return some values to the OS (or
JVM, which can return it to the OS) is to call System.exit(). But a
plain "return" won't work. Is this the real intention - to disallow
the use of "return" for sending an exit code to the OS ? Is so, why do
that ?


I am trying to give my own explanation to this; if I am wrong please
correct me.

In C/C++ if you wanted to exit the application you had to use the
exit() function with the exception of the main() function where the
return() function would do the job. For consistency reasons, in Java,
you must always call System.exit() when returning a value to the OS
while the plain return() function can only be used to transfer values
between function calls (but not to the underlying OS).





Regards,
Razvan

How's this one ;-)
output is
HI
HI
---------
class Test2 {
public static void main(String[] args) {
String a[] = {"hi"};
main(a, 0);
System.out.println("HI");
}
public static int main(String[] args, int arg) {
System.out.println("HI");
return 0;
}
}
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top