A simple Java program -- overview/notes

Here is the simplest Java program:

public class FirstSample
{
    public static void main(String[] args)
    {
        System.out.println("This time we don't use 'Hello, world!'");
    }
}

1. Java is case sensitive

If there is an uppercase or lowercase spelling error, such as writing main as Main, the program cannot run.

2. Keywords

a) keyword public

The keyword public is called an access modifier and is used to control the level of access to this code by other parts of the program.

b) keyword class

The keyword class indicates that all content in a Java program is contained in a class. Think of a class as a container for logic that defines the behavior of your application.

Classes are the building blocks of all Java applications, and everything in a Java program must be placed in a class.

3. Class names and their naming conventions

The keyword class is followed by the class name. The class name must start with a letter, followed by any combination of letters and numbers, but Java reserved words cannot be used, and the length is basically unlimited.

Naming convention:

1) The class name is a noun starting with a capital letter (for example, FirstSample in the code uses this naming convention.)

2) If the name consists of multiple words, the first letter of each word should be capitalized. This method of using uppercase letters in the middle is called camel case, which itself is CamelCase.

4. Source code naming

The source code file name must be the same as the class name of the public class, and use .java as the extension, and pay attention to capitalization. For example, the code file name of this example must be FirstSample.java .

5. Compile and run the Java program

If the file is named correctly and there are no typos in the source code, the code is compiled and you get a file containing the bytecode for the class. The Java compiler automatically names this bytecode file as  FirstSample.class and stores it in the same directory as the source file, as shown in the figure:

Finally, run the program with the command:

java FirstSample

Be careful not to add the .class extension. After the program is executed, "This time we don't use 'Hello, world!'" will be displayed on the console.

6. Specify the main method in the class

java ClassName

When using this command to run a compiled program, the Java virtual machine always starts execution from the code of the main method in the specified class, so in order to be able to execute the code, the source code of the class must contain a main method.

※ Every Java program must have a main method, the format is as follows:

public class ClassName
{
    public static void main(String[] args)
    {
        program statements
    }
}

7. Braces { }

In Java, sections of a program (often called blocks) are delimited by curly braces { }. The code of any method in Java must start with "{" and end with "}".

The Java compiler ignores whitespace, and you can choose any brace style you like.

8. Java statement

In this FirstSample example, the code snippet:

{
    System.out.println("This time we don't use 'Hello, world!'");
}

A pair of curly braces indicates the beginning and end of the method body. This method contains only one statement, and Java statements can be regarded as sentences in this language.

※ In Java, each statement must end with a semicolon.

PS Carriage return is not the end of the statement, if necessary, a statement can span multiple lines.

The main method body contains only one statement, and its function is to output a text line to the console.

Here the System.out object is used and its println method is called.

9. Period (.) call method

A period (.) is used to call a method. The general syntax used by Java is:

object.method(parameters)

Equivalent to a function call.

10. println method

The println method takes a string parameter, displays the string parameter on the console, and then terminates the output line.

Each call to println displays output on a new line.

※ In Java, strings must be delimited by double quotes.

A method in Java can have no parameters or one or more parameters. Empty parentheses are required even if a method has no parameters. For example, the println method without parameters just prints a blank line.

PS System.out also has a print method that does not add a newline after the output. For example, System.out.print("Hello") does not wrap after printing "Hello", and the next output immediately follows the letter "o".

In summary, the information summarized on the code is as follows:

 

Guess you like

Origin blog.csdn.net/shufenanbei/article/details/131877031