Java zero foundation notes

Posted by danville on Fri, 21 Jan 2022 19:18:16 +0100

HelloWorld

Of course, the first step in learning is to write this. You must have a sense of ceremony

This is a personal class note, written from HelloWorld.

I study Java zero foundation free class https://www.bilibili.com/video/BV12J41137hu?p=33&share_source=copy_web

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello,World!");
    }
}

01 - Notes

  • There are three types of comments: single line comments, multi line comments and document comments
  • This is shown in the code. We don't know much about the document comments for the time being
public class HelloWorld {
    public static void main(String[] args) {
        //Single-Line Comments  
        System.out.println("Hello,World!");
        /*
        multiline comment 
         */
        
        //JavaDoc document comments / * **/
        /**
         *
         */
    }
}

02 - identifiers and keywords

I identifier

All components of Java need names. Class names, variable names, and method names are all called identifiers.

II keyword

abstractassertbooleanbreakbytecasecatchcharcalssconst
continuedefaultdodoubleelseenumextendsfinafinallyfloat
forgotoifimplementsimportinstance ofintinterfacelongnative
newpackageprivateprotectedpublicreturnstrictfpshortstaticsuper
switchsynchronizedthisthrowthrowstransienttryvoidvolatilewhile

Naming conventions for identifiers:

  1. See the name and know the meaning

  2. Follow the hump naming method. (that is, one high and one low, one high and one low...). Humps are good for separating words from words.

  3. Class name and interface name have special requirements. Class name and interface name are capitalized. The first letter of each subsequent word is capitalized.

  4. The variable name and method name are lowercase, and the first letter of each subsequent word is uppercase.

  5. All constant names are capitalized and underlined between words.

    public class Demon01 {
        public static void main(String[] args) {
    
            String Ahello = "1";
            String hello = "1";
            String $hello = "1";
            String _hello = "1";
    
            String Man = "ah";
            String man = "ah";
    
            String Glory of Kings = "Stubborn bronze";
        }
    }
    

03 - data type

Eight Java data types:

(1) Integer type: byte, short, int, long
(2) Decimal type: float, double
(3) Character type: char
(4) boolean type: boolean

1. Integer data type

  • Byte: 1 byte, 8 bits, 256 States, value range [- 128127]
  • short: 2 bytes, 16 bits, 65536 States, value range [- 3276832767]
  • Int: 4 bytes, 32 bits. The default integer type is int, with a value range of about 2.1 billion
  • Long: 8 bytes, 64 bits. Long type represents a long type constant. L or l should be added. It is recommended to add L

2. Decimal data type

  • float: 4 bytes, 32 bits, single precision, can be accurate to 6 ~ 7 bits, declare a decimal type, add f or F, it is recommended to add F
  • Double: 8 bytes, 64 bits, double precision, accurate to 15 ~ 16 bits. The decimal type is double by default

3. Character data type

  • char: 2 bytes, 16 bits. The character represents each symbol in the Unicode (universal code) coding table. Each symbol is enclosed in single quotation marks. The first 128 symbols are the same as those in the ASCII table

4. Boolean data type

  • boolean: it occupies one bit and has two values: true and false. One represents true and the other represents false. It is generally used to represent logical operation

04 - data type expansion

public class Demon02 {
    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 = 40L;//long type should be followed by l

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

                //character
                char name = 'country';
                //String, string is not a keyword, class
                //String name = "first name";

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

public class Demon03 {
    public static void main(String[] args) {
        //Integer expansion: binary 0b decimal octal 0 hexadecimal 0x

        int i = 10;
        int i2 = 010;//octal number system
        int i3 = 0x10;//hexadecimal

        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 finite discrete rounding error is approximately close to but not equal to
        //double


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

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

        float d1 = 123445556676543f;
        float d2 = d1+1;

        System.out.println(d1==d2);//true


        //=====================================================================================
        //Character expansion?
        //=====================================================================================
        char c1 ='a';
        char c2 ='in';

        System.out.println(c1);
        System.out.println((int)c1);//To force a line to a number

        System.out.println(c2);
        System.out.println((int)c2);//Forced line feed

        //The essence of all strings is numbers
        //Encoded Unicode table: (97 = a, 65 = a) accounts for 2 bytes. It can represent 0 - 65536 characters at most. The early Excel has 2 ^ 16 = 65536
        //Interval U0000 - UFFFF

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

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

}

public class Demon04 {
    public static void main(String[] args) {
        String sa = new String("hello world");
        String sb = new String("hello world");
        System.out.println(sa==sb);//false

        String sc = "hello world";
        String sd = "hello world";
        System.out.println(sc==sd);//true

        //Boolean extension
        boolean flag = true;
        if (flag==true){}//Novice
        if (flag){}//an old hand
    }

}

05 type conversion

java is a strongly typed language

public class Demon05 {
    public static void main(String[] args) {
        int i = 128;
        byte b = (byte)i;//Content overflow, 0-127
        double b1 = i;

        //Cast (type) variable name high -- low
        //Automatic conversion low high
        //Low ------------------------------- high
        //Byte, short, char - > int - > long - > float - > double decimal takes precedence over integer

        System.out.println(i);
        System.out.println(b);
        System.out.println(b1);

        /*
        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 content 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);
    }
}
public class Demon06 {
    public static void main(String[] args) {
        //Pay attention to overflow when operating large numbers
        //JDK7 new features, numbers can be separated with underscores
        int money = 10_0000_0000;
        int years = 20;
        int total = money*years;//-1474836480, overflow during calculation
        long total2 = money*years;//The default is int. there is a problem before the conversion?

        long total3 =money*((long)years);//First convert a number to long
        System.out.println(total3);

        //Note that L is better capitalized

    }
}

06 variable constant scope

public class Demon07 {
    public static void main(String[] args) {
        //int a=1,b=2,c=3; It's best to write three lines to make the program readable
        int a=1;
        int b=2;
        int c=3;
        String name = "ming";
        char x = 'x';
        double pi = 3.14;

    }
}
public class Demon08 {

   //Class variable static
    static double salary = 2500;
    //Attributes: Variables

    //Instance variable: subordinate to object; If you do not initialize yourself, the default value of this type is 0.0
    //Boolean: false by default
    //Except for the basic type, the other default values are null;
    String name;
    int age;

    //main method 
    public static void main(String[] args){

        //Local variables: values must be declared and initialized
        //int i; // That won't work
        int i = 10;

        System.out.println(i);

        //Variable type variable name = new base Demon08
        Demon08 demon08 = new Demon08();
        System.out.println(demon08.age);
        System.out.println(demon08.name);

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


    }


    //Other methods
    public void add(){


    }
}

public class Demon09 {
    
    //Modifier does not exist in order
    //final static is also OK
    static final double PI =3.14;

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


07 basic operator

Java supports the following operators

  • Arithmetic operators: +, -, *, /,%, + +, –
  • Assignment operator:=
  • Relational operators: >, <, > =, < =, = =,! =, instanceof
  • Logical operators: & &, ||!
  • Bitwise operators: &, |, ^, ~, > >, <, > > >
  • Conditional operator?:
  • Extended assignment operators: + =, - =, * =/=

Arithmetic operator, assignment operator

package operator;

public class Demon01 {
    public static void main(String[] args) {
        //Binary operator
        //CTRL + D copies the current row to the next row
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;

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

System.out.println(a/b);

If you output a/b, that is, 10 / 20 = 0.5, but the output is 0, because int is rounded down,

Add double in front and output 0.5

package operator;

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

        System.out.println(a+b+c+d);//Long, a long result is long
        System.out.println(b+c+d);//Int
        System.out.println(c+d);//Int
    }
}

Relational operator

package operator;

public class Demon03 {
    //Results returned by relational operators: correct, wrong, Boolean
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        int c = 21;

        System.out.println(c%a);//  The remainder C / a = 1 one

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


}
package operator;

public class Demon04 {
    public static void main(String[] args) {

        //++-- self increasing and self decreasing unary operation
        int a = 3;

        int b = a++;//Assign a value to b first, and then increase it. / / b is 3, and a is 3
        //a = a + 1
        System.out.println(a);         //At this time, a is 4
        //a++ a = a + 1;
        int c = ++a;//Increase automatically first and then assign a value. / / at this time, a is 5 and c is 5

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


        //exponentiation 
        double pow = Math.pow(2, 3);
        System.out.println(pow);

    }
}

Logical operator

package operator;

//Logical operator
public class Demon05 {
    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));//Logic and operation: the result is true only when both variables are true
        System.out.println("a || b:"+(a || b));//Logical or operation: one of the two is true and the result is true
        System.out.println("! (a && b):"+(! (a && b)));//If true, it becomes false; if false, it becomes true

        //Short circuit operation
        int c = 5;
        boolean d = (c<4)&&(c++<4);//c < 4 is already false, so there is no need to calculate later. So c doesn't change
        System.out.println(d);
        System.out.println(c);
    }

}

Bitwise Operators

package operator;

public class Demon06 {
    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

        Ask 2 * 8 = 16 how to calculate fast

        <<   *2    Shift left
        >>   /2    Shift right

         */
        System.out.println(2<<3);

    }
}

String connector+

package operator;

public class Demon07 {
    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 connector+
        System.out.println(a+b);
        System.out.println(""+a+b);//Output 1020 because the previous String
        System.out.println(a+b+"");//Output 30, because it is calculated before changing the type

    }
}

Ternary operator

package operator;

//Ternary operator 
public class Demon08 {
    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);
    }
}

Next installment Preview: Java process control

Topics: Java