Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Beginner question about main

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Beginner question about main

    Hello everybody,

    this is my first post about Java ever so be kind

    I know C and C++ quite good i would say.However i decided that learning JAVA is a must for me.So i started with a tutorial i found on the net.The first question i had has been unresolved up to now,so i ask you
    The question
    Why function main has a void return type?In C and C++ it is int in order to notify if it has terminated sycceddfully or not.Why not the same in Java?Is there a reason?(maybe yes,else it would be the same i guess)


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Beginner question about main

    There's some discussion from around the net:

    Why is main() in java void? - Stack Overflow
    Why does main() in java returns void ?

    The main jist is because C/C++ were originally designed for single threaded applications. In these cases a program's end point should be when the main method returns.

    However, in a multi-threaded application, the main method (and main thread) terminating does not necessarily mean the application has finished running. Thus, in these applications a main method return value simply does not make sense. This is the approach Java took from the beginning, even though not all Java applications must be multi-threaded.

    This does not mean that a Java application cannot return a value, though. It simply means that a different mechanism is used.

    In Java this is done using the System.exit() call.

    // somewhere in the code
    if(DRAMATIC_ERROR)
    {
        System.exit(-1); // something went wrong
    }

    This call works by terminating all java threads and is guaranteed to be the end of your program (well, almost. There are a few caveats with shutdown hooks, finalization, and such).

    There is also another option of calling System.halt() which forcibly terminates the JVM, meaning it is the absolute end of your program (or is close enough to it). However, under almost all circumstances it is recommend that you use System.exit() because it does allow shutdown hooks and other tasks to run.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Beginner question about main

    I think i got it.I do a little search for multi-threaded applications on the net though!So thank you

Similar Threads

  1. Beginner question about for loops...
    By ninjaBob in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2012, 03:57 PM
  2. Loop Question - Very new beginner
    By Callcollect in forum Loops & Control Statements
    Replies: 12
    Last Post: January 18th, 2012, 04:01 AM
  3. Beginner question on where to put an array
    By Diplo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 3rd, 2011, 03:56 PM
  4. Beginner Question
    By jrt224 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2011, 12:56 PM
  5. [SOLVED] Simple question from a beginner
    By jimmylee7706 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 09:57 PM