Good Java programmers share Javamain ten interview questions

  As Java will be asked a lot of questions when engineers interview, then in Javamain what issues will be asked about aspects of it? Today, a good programmer prepare some common for everyone Javamain the basics of the problem, to help you prepare before the interview:

 

  1 , no main method of how to define a class?

 

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

 

  In Java7 before, you can use static initialization run Java classes. However, Java7 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.

 

  packagecom.instanceofjava;

  publicclassMainMethod

  {

  publicstaticvoidmain(Stringargs[])

  {

  }

  }

 

  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.

 

  packagecom.instanceofjava;

  publicclassA

  {

  publicstaticintmain(String[]args)

  {

  return1;//runtimeerror:Nomainmethodfound

  }

  }

 

  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?

 

  packagecom.instanceofjava;

  publicclassA

  {

  publicMainMethod (you)

  {

  // Constructortakingoneargument

  }

  publicvoidmain(String[]args)

  {

  //mainmethodasnon-static

  }

 

  5 , we can not declare main () method is non-static?

 

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

 

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

 

  packagecom.instanceofjava;

  publicclassA

  {

  publicvoidmain(String[]args)

  {

  System.out.println("indhu");//Runtimeerror

  }

  }

 

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

 

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

 

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

 

  packagecom.instanceofjava;

  publicclassA

  {

  publicstaticvoidmain(String[]args)

  {

  System.out.println("Indhu");

  }

  voidmain(intargs)

  {

  System.out.println("Sindhu");

  }

  longmain(inti,longd)

  {

  System.out.println("Saidesh");

  returnd;

  }

  }

 

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

 

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

 

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

 

  packagecom.instanceofjava;

  publicclassA

  {

  privatestaticvoidmain(String[]args)

  {

  //Runtimeerror

  }

  }

 

  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/2413861