Why does main() in java returns void ?

Discussion in 'Java' started by Razvan, Jul 9, 2004.

  1. Razvan

    Razvan Guest

    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
     
    Razvan, Jul 9, 2004
    #1
    1. Advertising

  2. Razvan wrote:

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

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Jul 9, 2004
    #2
    1. Advertising

  3. Razvan

    Chris Smith Guest

    Razvan wrote:
    > 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
     
    Chris Smith, Jul 9, 2004
    #3
  4. Razvan

    Roedy Green Guest

    On Fri, 9 Jul 2004 07:59:27 -0600, Chris Smith <> wrote
    or quoted :

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

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 9, 2004
    #4
  5. Razvan

    marcus Guest

    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

    Razvan wrote:
    > 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
     
    marcus, Jul 11, 2004
    #5
  6. 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 :)

    "marcus" <> wrote in message
    news:...
    > 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
    >
    > Razvan wrote:
    > > 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

    >
     
    Java Architect, Jul 11, 2004
    #6
  7. On Sun, 11 Jul 2004 02:56:53 -0700, Java Architect wrote:
    > "marcus" <> wrote in message
    >> Razvan,
    >>> Why does the main() function in java returns void ? In C/C++ this is
    >>> considered BAD style. Why the Java designers took this approach ?

    ....
    >>> In C/C++ ...

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

    ....
    > 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'. ;-)

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Jul 11, 2004
    #7
  8. Razvan

    Liz Guest

    "Razvan" <> wrote in message
    news:...
    > 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;
    }
    }
     
    Liz, Jul 12, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    566
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    812
    The Real OS/2 Guy
    Oct 28, 2003
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,072
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    5
    Views:
    850
    S.Tobias
    Jul 22, 2005
  5. int main(void) Why void?

    , Apr 6, 2006, in forum: C Programming
    Replies:
    6
    Views:
    510
Loading...

Share This Page