Java program foundation

Keywords: Java less

Basic structure

//Single line notes
/*Multiline comment/
/*Special multiline comment*/

/**
 * Comments that can be used to automatically create documents
 */
public class Hello {
    public static void main(String[] args){
        // Output text to screen:
        System.out.println("Hello, Sam!");
        /* Multiline comment start
        Note Content
        End of annotation
         */
    }
} // End of class definition

The basic unit class of the program - class, and the object name is Hello (naming rules: start with capital letters, underline, etc., consistent with the file name)
The public modifier indicates that the object is public
Class, static void main(String[] args (the method specified by Java entry program must be a static method, the method name must be main, and the parameter in parentheses must be a String array.)

Java program basic organization

Basic structure of Java program:

// A code block
var foo = 'bar';
public class Goodbye {
    public static void main(String[] args) {
        // Output text to screen:
        System.out.println("Hello, Byebye!");
        /* Multiline comment start
        Note Content
        End of annotation */
    }
} // End of class definition

Public is the access modifier, class is public
Several methods can be defined in class. The method name is main, and the return value is void, indicating that there is no return value
1. Integer type: byte, short, int, long
2. Float type: float(f), double
3. Character type: char(') with single quotation mark
4. boolean type: boolean

Variables and data types

Variables of basic type: for example, int x=1
Variable of reference type:

public class Hello {
    public static void main(String[] args){
        int x = 500; // Variable definition, indicating type
        float f1 = 3.14f; // For floating-point data, add the f suffix
        float f2 = 3.14e5f; // 3.14x10^5 represented by scientific counting method
        char a = 'S'; // Character type, single quotation mark only one character
        char qq = 'in'; // Can represent other characters
        final double Pi = 3.14; // Variable before final becomes constant
        var ss = new StringBuilder(); //Omit variable type with var
        x = x +100;  // Second use does not need to be defined
        int n = x;
        System.out.println(x);
        System.out.println("x = " + (x + n));
    }

The equal sign is an assignment statement. Variables can be reassigned. The basic types include: integer, floating-point, Boolean, and character
Compulsory transformation must not go beyond the scope
Floating point number operation floating point number is often unable to express accurately, so floating point number operation will produce errors; judge whether the absolute value of the difference between two floating points is less than a very small number
Boolean operation character and character array type

String character

public class Hello {
    public static void main(String[] args){
        // Output text to screen:
        System.out.println("Hello, Sam!");
        /* Multiline comment start
        Note Content
        End of annotation
         */
        char a = 'S'; // Character type, single quotation mark only one character
        char qq = 'in'; // Can represent other characters
        String a11 = "ADSDCVs"; 
        //Escape character


Test code block

/**
 * Comments that can be used to automatically create documents
 */
public class Hello {
    public static void main(String[] args){
        // Output text to screen:
        System.out.println("Hello, Sam!");
        /* Multiline comment start
        Note Content
        End of annotation
         */
        int x = 500; // Variable definition, indicating type
        float f1 = 3.14f; // For floating-point data, add the f suffix
        float f2 = 3.14e5f; // 3.14x10^5 represented by scientific counting method
        char a = 'S'; // Character type, single quotation mark only one character
        char qq = 'in'; // Can represent other characters
        String a11 = "ADSDCVs"; // Contains several strings
        final double Pi = 3.14; // Variable before final becomes constant
        boolean age_1 = x == 500; // true
        int[] ns = new int[6]; // Define array variables. Once an array is created, it cannot be changed
        int[] ns1 = new int[] { 68, 79, 91, 85, 62, 99 };
        int[] ns2 = { 68, 79, 91, 85, 62 };
        var ss = new StringBuilder(); //Omit variable type with var
        x = x +100;  // Second use does not need to be defined
        int n = x;
        System.out.println(x);
        System.out.println(age_1);
        System.out.println("x = " + (x + n));

        //operation
        int i = (100+200)*(99-88); //3300
        int x1 = 12345/67; // 184
        int y = 123456 % 67; //Use% for remainder operation
        // Overflow will not report an error, and a strange result will be obtained
        int a1 = n << 1;  // 00000000 00000000 00000000 00001110 = 14
        // Bitwise operation &, |, ` and or not
        // Type cast
        short s = 12345;
        int i1 = 12345;
        int x2 = s + i1; // s Auto transform to int
        //short y= s + i1; / / compilation error!
        String[] names = {"ABC", "XYZ", "zoo"};
        String s0 = names[1];
        names[1] = "cat";
        System.out.println(names[1] + s0 + names[2]);
        System.out.println(s0); // Is s "XYZ" or "cat"?
    }
} // End of class definition
Published 4 original articles, won praise 0, visited 345
Private letter follow

Posted by geoffism on Wed, 29 Jan 2020 02:18:40 -0800