Basic Java Knowledge

Source: Internet
Author: User

1java Language Advantages:

(1) Java is a purely object-oriented language

(2) Platform independence

(3) Java provides a lot of built-in library classes

(4) provides support for Web application development

(5) Good safety and robustness

(6) Eliminate confusing concepts such as pointers in C + + that are difficult to understand

2, why need public static viod mian (String [] args) This method

Answer: Java Program entry

Static indicates that this is a static method, where the code in the method is stored in a static code area, and as long as the class is loaded, it can be accessed directly through the class name by using the method without the need to access it by instantiating the object. Mian ().

Viod indicates no return value.

Main () is a special method name that is recognized by the Java Virtual machine.

String[] args provides a means for programmers to interact with programs at the command line

Extension: does main () have other definition formats?

1) since public and static have no sequencing, the following definition format is also true

static public void Mian (string[] args)

2) The main () method can also be defined as final.

public static final Viod main (string[] args)

3) You can also use synchronized to modify the Mian () method.

static public sybnchronized void Mian (string[] args)

In summary: Regardless of the definition, you must ensure that the return value of the main () method is void and has static and public keyword adornments. Because Mian () is a program's entry method, it cannot be decorated with the abstract keyword.

3. Can there be more than one Mian () method in the same. java file?

Although the Mian () method can be defined in each class, only the Mian () method in the public-decorated class with the same file name can be used as the entry method for the program. The following example:

Package test;
Class t{
public static void Mian (string[] args) {
System.out.println ("T mian");
}
}
public class Miantest {
Program Entry function
public static void Main (string[] args) {
System.out.println ("Test mian entrance");
}
}

Operation Result:

Test Mian Entrance

4. Implement the main () method to output "Hello world!" before execution

Scenario: a static block of code is implemented because a static block of code is called when the class is loaded

Package test;

public class HelloWorld {
public static void Main (string[] args) {
System.out.println ("Hello world1111!");
}
static{
System.out.println ("Hello world22222!");
}
}

Program Run Result:

Hello world22222!
Hello world1111!

Basic Java Knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.