Basic content of Java se

JavaSE Basics

Java comments

public class Helloworld {
    public static void main(String[] args) {
        System.out.println("hellowprld");
        //Output a Hellowprld
        /*I'm a multiline comment


         */
        /**
         *
         * I'm a document comment
         */
    }
}

identifier

public class Demo01 {
    public static void main(String[] args) {
        String Alloh="dhij";
        String hello="jdgh";
        String _dja="ugd";
        String 1hauf="bhuf";
        String #jfbhg="gy";// error
        String class=" b";//error
    }
}

All components of java need names. Class names, variable names and method names are called markers

All identifiers should be alphabetic, dollar sign ($), underscore () start

The first character can be followed by letters, dollar characters, underscores, or any combination of numbers

Keywords cannot be used as variable or method names

Identifiers are size sensitive

Examples of legal identifiers: age, $salary_ value

Examples of illegal identifiers: 123age,-salary,#abc

It can be named in Chinese, but generally not

data type

Strongly typed language: it is required that the use of variables must strictly comply with the regulations, and all variables must be defined before use

Weakly typed language: may not meet the requirements, such as JavaScript

Java data types fall into two broad categories:

  • Basic type
  • reference type

Basic type:

Numeric type: integer type, byte, short, int, long

Type: float,double

Character type char: two bytes

boolean: occupies one byte, and the values are only true and false

public class Demo02 {
    public static void main(String[] args) {
       // String a=10;//string is a string, but 10 is an int
        String a="hello";
        int num=10;
        System.out.println(a);
        System.out.println(num);
        //Four representations of integers
        int num1=10;
        byte num2=20;
        short num3=30;
        long num4=244891l;//The number of long type should be followed by l
        //Floating point representation
        float num5=24.32478F;//Add F after float type
        double num6=3.1415926;
        //character
        char name='A';
        //string is not a keyword, class
        String name1="qingjiang";
        //bool value
        boolean flag=true;
        boolean flag2=false;
    }
}

Reference type:

Class, interface, array

Extended content

public class Demo03 {
    public static void main(String[] args) {
        //Integer expansion: binary 0b decimal octal 0 hexadecimal 0x
        int i=10;
        int i1=010;//octal number system
        int i3=0x10;//hexadecimal
        System.out.println(i);
        System.out.println(i1);
        System.out.println(i3);
        System.out.println("===========================================");
        //===================================================
        //Floating point extended banking
        //BigDecimal math tool class
        //===================================================
        //The rounding error of float finite discrete is approximately close to but not equal
        //double
        //It is best to completely avoid using floating-point numbers for comparison
        //It is best to completely avoid using floating-point numbers for comparison
        //It is best to completely avoid using floating-point numbers for comparison
        float f=0.1f;
        double d=0.1;
        System.out.println(f==d);
        System.out.println(f);
        System.out.println(d);
        float d1=231231231231231f;
        float d2=d1+1;
        System.out.println(d1==d2);
        System.out.println(d1);
        System.out.println(d2);
        System.out.println("================================================");
        //====================================================================
        //Character expansion
        //===================================================================
        char a='a';
        char a1='in';
        System.out.println(a);
        System.out.println((int)a);//Forced conversion
        System.out.println(a1);
        System.out.println((int)a1);//Force conversion
        //All characters are still numbers in nature
        //Encoding Unicode table: (a 97 a 65) 2 bytes 0-65536 excel 2 16 = 65536
        //u0000 Uffff
        char a2='\u0061';
        System.out.println(a2);

        //Escape character
        //\t tab
        //\n line feed
        System.out.println("hello\nworld");

        System.out.println("=========================================================");
        String sa=new String("hello world");
        String sb=new String("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)//if(flag) has the same meaning
        {
            
        }
    }
}

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-vrhrbgrs-1645616370175) (C: \ users \ tangz \ appdata \ roaming \ typora user images \ image-20220223152126980. PNG)]

Type conversion

java is a strongly typed language, and type conversion may be used in operation

Low -----------------------------------------------------------------------------------------------------------> high

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

Convert different types into the same type during operation

public class Demo04 {
    public static void main(String[] args) {
        int i=128;
        byte b=(byte)i;//Force conversion of format (type) variable names from top to bottom
        double b2=i;//Automatic conversion, from low to high

        System.out.println(i);
        System.out.println(b);//The maximum byte is 127, so the memory overflows
        /*
        Note:
        1.bool type cannot be converted
        2.Cannot convert an object type to an unrelated type
        3.Converting high capacity to low capacity is forced conversion
        4.There may be memory overflow or accuracy problems during conversion!
         */
        System.out.println((int)-45.89f);
        char c='a';
        int d=c+1;
        System.out.println(d);
        System.out.println((char)d);
        //When the operation is relatively large, pay attention to overflow
        //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*years;//The default is int, and there is a problem before conversion

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

variable

Variable: the amount that can be changed (determined position, changed content)

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

Data type variable name = value;

Note:

Each variable has a type, which can be either a data type or a reference type

Variable name must be a legal identifier

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

public class Dome05 {
    public static void main(String[] args) {
       // int a,b,c;
       // int a=1,b=2,c=3;  // Program readability
        String name="qinjiang";
        char x='a';
        double pi=3.14;
    }
}

Variable scope

  • Class variable
  • Instance variable
  • local variable
public class Dome06 {
    //Attributes: Variables

    //Class variable static
    static double salary=2500;

    //Instance variable, subordinate to object;
    //If the instance variable is not initialized, the default value of this type is 0.0
    //Bool - > false all default values are null except the basic type
    String name;
    int age;

    //main method 
    public static void main(String[] args) {
        //Local variables: must declare and initialize values
        int i=10;
        System.out.println(i);

        //Use of instance variables
        //Variable type variable name = new Demo06();
         Dome06 demo06=new Dome06();
        System.out.println(demo06.name);

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

    }
    //Other methods
    public void add(){
        //System.out.println(i); error
    }
}

constant

Constant: a value that cannot be changed after initialization

A special variable whose value, once set, cannot be changed during program operation

final constant name = value;

final double PI=3.14;

Constant names generally use uppercase letters

public class Demo07 {

    //Modifier, not in order (static,final,public,private, etc.)
    static final double PI=3.14;
    final static double PI1=3.14;

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

[external link image transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-qr6rgzjm-1645616370176) (C: \ users \ tangz \ appdata \ roaming \ typora user images \ image-20220223165704047. PNG)]

Variable naming conventions:

All variables method. Class name: see the name to know the meaning

Class member variables: initial lowercase and hump principle: monthSalary

Local variables: initial lowercase and hump principle

Constants: uppercase letters and underscores MAX_VALUE

Class name: initial capital and hump principle Man

Method name: initial capitalization and hump principle: run()

operator

Operators supported by Java language:

Arithmetic operators: +, -, *, /,%, + +, –

Assignment operator: = assign the following number to the variable

Relational operators: >, <, > =, < =, = =,! =, instanceof

Logical operators: & & and, | or,! wrong

Bitwise operators: &, |, ^, ~, > >, <, > > >

Conditional operator:?:

Extension operators: + =, - =, * =/=

Priority: () highest

public class Demo01 {
    public static void main(String[] args) {
        //Binary operator
        //ctrl+D copies the current line to the next line
        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);//Pay attention to the scope of action
    }
}
package Operator;

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

        System.out.println(a+b+c+d);//long type
        System.out.println(b+c+d);//int type
        System.out.println((String)(c+d));//int type
        //If there is a long type, the result is long type, and others are int type

    }
}

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-65afnkaq-1645616370177) (C: \ users \ tangz \ appdata \ roaming \ typora user images \ image-20220223171349691. PNG)]

Relational operators and remainder

package Operator;

public class Demo03 {
    public static void main(String[] args) {
        //Results returned by relational operators: correct, wrong, Boolean
        int a=10;
        int b=20;
        int c=21;
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
        System.out.println(c%a);
    }
}

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-3hldi5qy-1645616370178) (C: \ users \ tangz \ appdata \ roaming \ typora user images \ image-20220223171826912. PNG)]

Self increasing and self decreasing

 public class Demo04 {
    public static void main(String[] args) {
        //++-- self increasing and self decreasing unary operator
        int a=3;
        int b=a++;
        //A + + is equal to a=a+1. This + + is assigned to b
        System.out.println(a);
        //A + + is equal to a=a+1. This + + is before the assignment to
        int c=++a;
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        //Power operation 2 ^ 3 2 * 2 * 2 = 8 power operation generally uses tool operation
        double pow=Math.pow(3,3);
        System.out.println(pow);
    }

}

Logical operator

public class Demo05 {
    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));//It's true that two are true
        System.out.println("a||b:"+(a||b));//Both are false
        System.out.println("!(a&&b):"+(!(a&&b)));//Reverse

        //Short circuit operation
       // System. out. println("a&&b:"+(a&&b));// If you find that B is false, you won't look at a
        int c=5;
        //Experimental verification shows that if the operation continues, c is 6 and short-circuit operation is 5
        boolean d=(c<4)&&(c++<4);
        System.out.println(c);
    }
}

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-kyxabren-1645616370178) (C: \ users \ tangz \ appdata \ roaming \ typora user images \ image-20220223175348221. PNG)]

Bit operation

public class Demo06 {
    public static void main(String[] args) {
        /*
        A=0011 1100
        B=0000 1101
        
        A&B  0000 1100 It is 1 only when the corresponding bits are all 1
        A/B  0011 1101 It is 0 only when the corresponding bits are 0
        A^B  0011 0001 Take 0 for the same and 1 for the different
        ~B   1111 0010 Reverse
        <<Shift left is the number multiplied by 2
         >>Shift right / 2
        
        0000 0000  0
        0000 0001  1
        0000 0010  2
        0000 0011  3
        0000 0100  4
        0000 1000  8
        0001 0000  16
         */

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

Extended assignment operator and three bit operator

public class Demo07 {
    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
        //If you add or subtract the string type inside, it will be converted into a string connection, but you need to put it in the front and put it in the back, which is equivalent to what you continue to output
        System.out.println(a+b);
        System.out.println(""+a+b);
        System.out.println(a+b+"sa");

        //Ternary operator 
        //x?y:z
        //If x is true, the result is y, otherwise the result is z
        int score=80;
        String tyre=score<60?"fail,":"pass";
    }
}

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-v4wt91oz-1645616370178) (C: \ users \ tangz \ appdata \ roaming \ typora user images \ image-20220223180241438. PNG)]

Package mechanism

The essence of a package is a folder

The syntax format of package statement is package pkg1[.pkg2.[.pkg3...]]

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

In order to use the members of a package, we need to import the package in the java program and use the "import" statement

import + package name

[the external chain image transfer fails. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-oqyy4qua-1645616370178) (C: \ users \ tangz \ appdata \ roaming \ typora user images \ image-20220223181824802. PNG)]

JavaDoc

The javadoc command is used to generate its own document API

parameter information

@Author author name

@Version version number

@since indicates that the earliest version of jdk is required

@param parameter name

@Return return value

@throws exception thrown

Keywords: Java Back-end

Added by bubblocity on Thu, 24 Feb 2022 11:58:56 +0200