Java basic syntax

Posted by FuriousIrishman on Mon, 17 Jan 2022 17:43:13 +0100

Java basic syntax

notes

notes:

Usually write code, the amount of code is small, and you can understand it, but you can't understand it after it is complex

Writing notes is a very good habit

There are three types of notes:

1. Single line note

//

2. Multiline comment

/*

*/

3. Document notes

/**

*/

The writing code should be standardized

Note: interesting code comments

identifier

keyword

I can basically finish it

class, public, static, void, etc

Identifier notes:

1. All identifiers should be in letters (A-Z or A-Z), dollar sign $, or underscore () start

2. The first character can be followed by letters (A-Z or A-Z), dollar sign ($), underscore () Or any of the numbers

3. Keywords cannot be used as variable or method names

4. Identifiers are case sensitive

5. Examples of legal identifiers: age, $salary_ value,__ 1_value

6. Examples of illegal identifiers: 123abc, - salary, #abc

7. It can be named in Chinese

data type

Strongly typed language

It is required that the use of variables should strictly comply with the regulations, and all variables must be defined before they can be used

Weakly typed language

Not strictly defined

Two data types of Java

1. Basic type

Numeric type: integer type: byte, short, int, long; Floating point type: float, double; Character type: char

, Boolean type

2. Reference type

Class, interface, array

What are bytes

bit: the smallest unit of data storage in a computer

Byte: it is the basic unit of data processing in the computer. It is customarily expressed in capital B

1B=8bit

Characters: letters, numbers, words and symbols used in computers

Type conversion

1. Java is a strongly typed language

Low to high

byte,short,char->int->long->float->double

2. In the operation, different types of data are converted to the same type, and then the operation is carried out

variable

1. Variables are variable quantities

2. Java is a strongly typed language, and each variable must declare its type

3. Java variable is the most basic storage unit in a program. The elements include variable, variable type and scope

//Data type variable name = value; You can declare multiple variables of the same type separated by commas

matters needing attention:

1. Each variable has a type, which can be either a basic type or a reference type

2. Variable name must be a legal identifier

3. Variable declaration is a complete statement, so each declaration must end with a semicolon

Variable scope

1. Class variable

2. Instance variable

3. Local variable

constant

capital

Naming conventions for variables

1. All variables, methods and class names: see the meaning of the name

2. Class member variables: initial lowercase and hump principle: monthSalary except the first word, the following words are capitalized lastname lastName

3. Local variables: initial lowercase and hump principle

4. Constants: uppercase letters and underscores: MAX_VALUE

5. Class name initial capitalization and hump principle: Man, GoodMan

6. Method name: initial lowercase and hump principle: run(),runRun()

operator

1. Arithmetic operators:

2. Assignment operator

3. Relational operator

4. Logical operator

5. Bitwise operator

6. Conditional operator

7. Extended assignment operator

Ternary operator

?:

Package mechanism

Generally, the company domain name is inverted as the package name;

In order to use the members of a package, we need to explicitly import the package into the Java program. Use the "import" statement to complete this function

import package1[.package2].(classname|*);

com.chen.xxx

import com.kuang.base.*;// Import all

JavaDoc

Overview (Java Platform SE 8 ) (oracle.com)

Java API help documentation

//Generate javaDoc document javaDoc parameter Java file through command line
//javadoc -encoding UTF-8 -charset UTF-8 Doc.java
//Homework: learn to use IDEA to generate JavaDoc documents! 
//However, an error was encountered: javadoc: error - unable to read Input length = 1
//Found at: https://blog.csdn.net/qq_27721169/article/details/81087119

above

Included Java code:
base part
Demo1:

package base;

public class Demo01
{
    public static void main(String[] args)
    {
        //Eight basic data types

        //integer
        int num1 = 10; //Most commonly used
        byte num2 = 20;
        short num3 = 30;
        long num4 = 30l; //The long type needs an L after the number

        //Decimals: floating point numbers
        float num5 = 50.1F;
        double num6 = 3.1415926535897932384;

        //character
        char name = 'A';
        //String. String is not a keyword, but a class
        //String name = "Chen Hao";

        //Boolean: Yes No
        boolean flag = true;
        //boolean flag = false;

        //reference type
    }
}

Demo2:

package base;

public class Demo02 {
    public static void main(String[] args) {
        //Integer extension: binary ob decimal octal 0 hexadecimal 0x

        int i  = 10;
        int i2 = 010; //Octal 00
        int i3 = 0x11; //Hex 0x 0~9 A~F 16

        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println("==================================");
        //===============================================
        //Floating point expansion? How to express banking business? money
        //BigDecimal math tool class
        //===============================================
        //float;   The finite discrete rounding error is approximately close to but not equal to
        //double:
        //It is best to use floating point numbers for comparison
        //It is best to use floating point numbers for comparison
        //It is best to use floating point numbers for comparison

        float f = 0.1f; //0.1
        double d = 1.0/10; //0.1

        System.out.println(f==d); //false

        float d1 = 23131313113121321f;
        float d2 = d1+1;
        System.out.println(d1==d2); //true
        //================================================
        //Character expansion?
        //================================================
        System.out.println("==================================");
        char c1 = 'a';
        char c2 = 'in';

        System.out.println(c1);

        System.out.println((int)c1); //Force conversion

        System.out.println(c2);

        System.out.println((int)c2);

        //All characters are still numbers in nature
        //Encoding Unicode table: (97 = a 65 = A) 2 bytes 65536 excel 2 16 = 65536
       // U0000 UFFFF

       char  c3 = '\u0061';
        System.out.println(c3); //a

        //Escape character
        // \t tab
        // \n line feed
        // ······

        System.out.println("Hello\nWorld");
        System.out.println("==================================");
        //The following two sentences know what he wants to express, but they can't run. I wonder directly? There is a new space here, but I don't know how to use it
        //String sa = new String(original:"hello world");
        //String sb = new String(original:"hello world");
        //System.out.println(sa==sb);

        String sc = "hello world";
        String sd = "hello world";
        System.out.println(sc==sd);
        //Object from memory analysis

        //Boolean extension
        boolean flag = true;
        if(flag==true){}  //Novice
        if(flag){} //an old hand
        //Less is More!  The code should be concise and easy to read
    }
}

Demo3:

package base;

public class Demo3
{
    public static void main(String[] args)
    {
     int i = 128;
     double a = i;
     byte b = (byte)i; //out of memory
         System.out.println(a);
        System.out.println(b);
     //Cast (type) variable name high -- low
     //Automatic conversion low high
        /*
        Note:
        1,Boolean values cannot be converted
        2,Cannot convert an object type to an unrelated type
        3,When converting high capacity to low capacity, force conversion
        4,There may be memory overflow or accuracy problems during conversion!
        */
        System.out.println("==================================");
        System.out.println((int)23.7); //23
        System.out.println((int)-45.89f);  //-45

        System.out.println("==================================");
        char c = 'a';
        int d = c+1;
        System.out.println(d);
        System.out.println((char)d);

        System.out.println("==================================");
        //When operating large numbers, pay attention to the overflow problem
        //JDK7 new feature, numbers can be separated by underscores
        int money = 10_0000_0000;
        int years = 20;
        int total = money*years;//-1474836480, overflow during calculation
        long total2= money*(long)years;
    }
}

Demo4:

package base;

public class Demo04
{
    public static void main(String[] args)
    {
     //int a,b,c;
     //int a=1,b=2,c=3;  This style is not advocated, and the readability of the program should be improved
        String name = "chenhao";
        char x = 'X';
        double pi = 3.14;
    }
}

Demo5:

package base;

public class Demo05
{
    //Class variable static
    static double salary = 2500;

    //Attributes: Variables

    //Instance variable: subordinate to object; If no initial value is assigned, the default value is 0.0
    //Boolean: false by default
    //All are null except the basic type;
    String name;
    int age;

    //main method
    public static void main(String[] args) {
        //Local variables; Values must be declared and initialized
        int i = 10;

        //Variable type variable name = new base Demo05();
        System.out.println(i);

        //Class variable static
        System.out.println(salary);
    }
}

Demo6:

package base;

public class Demo06
{
    //Modifier, no order exists
    static final double PI = 3.14;

    public static void main(String[] args) {
        System.out.println(PI);
    }
}

Operator operator section:
Demo1:

package operator;

public class Demo1
{
    public static void main(String[] args) {
        //Binary operator
        //Ctrl + D: copy current row to next row
        int a = 10;
        int b = 20;
        int c = 10;
        int d = 10;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b);

    }
}

Demo2:

package operator;

public class Demo2 {
    public static void main(String[] args) {
        long a = 123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;

        System.out.println(a+b+c+d); //long
        System.out.println(b+c+d); //int
        System.out.println(c+d); //int
    }
}

Demo3:

package operator;

public class Demo3 {
    public static void main(String[] args) {
        //Results returned by relational operators: correct, wrong, Boolean

        int a = 10;
        int b = 20;
        int c = 21;

        //Remainder is modular operation
        System.out.println(c%a); // c/a  21/10=2····1

        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
    }
}

Demo4:

package operator;

public class Demo4 {
    public static void main(String[] args) {
        //++-- self increasing and self decreasing unary operators
        int a = 3;

        int b = a++;  //After executing this line of code, assign a value first, and then increase it automatically
        //a++  a = a+1
        System.out.println(a);

        int c = ++a;

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        //Power operation 2 ^ 3 2 * 2 * 2 = 8 many operations will use tool classes to operate!
        double pow = Math.pow(3,2);
        System.out.println(pow);

    }
}

Demo5:

package operator;

//Logical operator
public class Demo5 {
    public static void main(String[] args) {
        //And (and) or (or) not (negative)
        boolean a =true;
        boolean b =false;

        System.out.println("a&&b:"+(a&&b));
        System.out.println("a||b:"+(a||b));
        System.out.println("!(a&&b):"+!(a&&b));

        //Short circuit operation
        int c = 5;
        boolean d = (c<4)&&(c++<4);
        System.out.println(d);
        System.out.println(c);
    }
}

Demo6:

package operator;

public class Demo6 {
    public static void main(String[] args) {
        /*
        A = 0011 1100
        B = 0000 1101

        A&B = 0000 1100
        A|B = 0011 1101
        A^B = 0011 0001
        ~B  = 1111 0010

        2*8 = 16 2*2*2*2
        High efficiency!!!
        <<  *2
        >>  /2

        0000 0000    0
        0000 0001    1
        0000 0010    2
        0000 0011    3

         */

        System.out.println(2<<3);
    }
}

Demo7:

package operator;

public class Demo7
{
    public static void main(String[] args) {
        int a = 10;
        int b = 20;

        a+=b;//a= a+b
        a-=b;//a= a-b

        System.out.println(a);

        //String connection +, string
        System.out.println(""+a+b);
        System.out.println(a+b+"");
    }
}

Demo8:

package operator;

import base.Demo02;
//The above is the correct import statement
public class Demo8
{
    public static void main(String[] args) {
        // x? y:z
        //If x==true, the result is y; otherwise, the result is z
        int score = 80;

        String type = (score<60)?"fail,":"pass";
        System.out.println(type);
    }
}

Topics: Java