Good programmers Java Tutorial Java main method of sharing classic interview questions

  Good programmers Java tutorial Share classic Java main method of interview questions , the following is a more classic on Java main method of interview questions, not so much Java interview questions, in fact, Java 's knowledge of some of the most basic questions, share to everyone, if wrong Please point out.

  1. without main how to define a class method?

  No, there is no main method we can not run Java classes.

  In Java 7 before you can use static initialization run Java classes. However, Java 7 start will not work.

  2.main () parameter is not a string array method needs?

  No, main () method parameter must be a string array.

  However, when introducing varying parameter, you can change the parameter string type passed as an argument to main () method. Varying parameter must have an array.

  package com.instanceofjava;public class MainMethod

  {public static void main(String args[])

  {

  }

  }

  3. We can not change the main () return type of the method?

  Can not, main () method's return type can only be empty. Any other type is unacceptable.

  package com.instanceofjava;

  public class A

  {

  public static int main(String[] args)

  {

   return 1;    //run time error : No main method found

  }

  }

  4.main () method must be static Why?

  main () method must be static.

  If the main () allows non-static, then the calling main time method, the JVM must instantiate its class.

  When instantiated, the constructor is called class have. If the class constructor has parameters, then it will appear ambiguous.

  For example, in the following procedure, in the instance of the class "A" of the time, the JVM delivered what parameters?

  package com.instanceofjava;

  public class A

  {

  public MainMethod(int i)

  {

  //Constructor taking one argument

  }

   public void main(String[] args)

  {

  //main method as non-static

  }

  5. We can not declare main () method is non-static?

  不能,main()方法必须声明为静态的,这样JVM才可以调用main()方法而无需实例化它的类。

  如果从main()方法去掉“static”这个声明,虽然编译依然可以成功,但在运行时会导致程序失败。

  package com.instanceofjava;

  public class A

  {

  public void main(String[] args)

  {

  System.out.println("indhu");         //Run time error

  }

  }

  6.我们能否重载main()方法?

  可以,我们可以重载main()方法。一个Java类可以有任意数量的main()方法。

  为了运行java类,类的main()方法应该有例如“public static void main(String[] args)”的声明。如果你对此声明做任何修改,编译也是可以成功的。但是,运行不了Java程序。你会得到运行时错误,因为找不到main方法。

  package com.instanceofjava;public class A

  {public static void main(String[] args)

  {

  System.out.println("Indhu");

   }void main(int args)

  {

  System.out.println("Sindhu");

  }long main(int i, long d)

  {

  System.out.println("Saidesh");return d;

  }

  }

  7.我们能否声明main()方法为privateprotected,或者不用访问修饰符?

  不能,main()方法必须public。你不能定义main()方法为privateprotected,也不能不用访问修饰符。

  这是为了能让JVM访问main()方法。如果你不定义main()方法为public,虽然编译也会成功,但你会得到运行时错误,因为找不到main方法。

  package com.instanceofjava;

  public class A

  {

  private static void main(String[] args)

  {

  //Run time error

  }

  }

  8.我们能否在Java中覆盖main方法?

  不能,你不能在Java中覆盖main方法。这是因为main方法是静态方法,而在Java中静态方法在编译时会结合在一起,所以你在Java中不能覆盖静态方法。

  9.我们能否在Java中终结main方法?

  你可以在Java中终结main方法。JVM对此没问题。

  10.我们能否在Java中同步main方法?

  是的,main方法可以在Java中同步,synchronized修饰符允许用于main方法的声明中,这样就可以在Java中同步main方法了。


Guess you like

Origin blog.51cto.com/14249543/2414394