Can a main method in Java return something?

前端 未结 6 1929
情话喂你
情话喂你 2020-11-29 04:58

I went through an interview recently and they ask me two questions in core Java.

Q 1.

Can we write a java class with:

public s         


        
6条回答
  •  死守一世寂寞
    2020-11-29 05:39

    Q 1. Can we write a java class with : public static int main(String[] args){

    Yes, you can but you can't run that Java class.

    Example class:

    class MainTest {
    
            public static int main(String[] args) {
                    return 1;
            }
    }
    

    You will receive an error message when trying to run it:

    Error: Main method must return a value of type void in class MainTest, please 
    define the main method as:
       public static void main(String[] args)
    

    Q 2: Next question I unable to answer. He asked write a program so that your java main method could return something.

    You can use System#exit(int) to quit your program with a specific exit code which can be interpreted by the operating system.

提交回复
热议问题