java learning (1.java Foundation)

Posted by robdavies on Wed, 17 Nov 2021 07:03:02 +0100

java Foundation

Type conversion

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

Force conversion

  • Form: (variable type) variable name
  • Attention
    1. Boolean values cannot be converted
    2. Object types cannot be converted to irrelevant types
    3. Force conversion when converting high capacity to low capacity
    4. There may be memory overflow or accuracy problems during conversion!

Automatic conversion

variable

  • Variable: variable quantity (variable must declare type)

  • Variables include: variable name, variable type, and scope
    type varName [=value] [{,varName[=value]}]

  • Variable scope

    1. Class variable
    2. Instance variable
    3. local variable
public class Demo4 {
    //Class variable static
    static double salary =2500;
    
    //Attributes: Variables

    //Instance variable: subordinate to object; You do not need to initialize, but use it as its default value:
    //Value type: 0 / 0.0
    //boolen: false
    //All are null except the basic type
    String name; //null
    int age;  //0
    
    //main method 
    public static void main(String[] args) {
    
        //Local variables: must be declared and initialized
        int i=10;
        
        //Variable type variable name
        Demo4 demo4 = new Demo4();
        System.out.println(demo4.name);
        System.out.println(demo4.age);
        
        //Class variable static can be used directly
        System.out.println(salary);
    }
}

constant

    //Modifier, no order exists
    static final double PI=3.14;  //Or final static double pi = 3.14;

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

Naming conventions

  • All variables, methods and class names: see the meaning of the name
  • Class member variable: initial lowercase + hump principle
  • Local variable: initial lowercase + hump principle
  • Class name: initial size + hump principle
  • Method name: initial lowercase + hump principle
  • Constants: uppercase letters and underscores MAX_VALUE

operator

The java language supports the following operators

  • Arithmetic operator

  • Relational operator

  • Bitwise Operators

  • Logical operator

  • Assignment Operators

  • Other Operators
    Conditional operator (?:)
    Conditional operators are also called ternary operators. The operator has three operands and needs to judge the value of the Boolean expression. The main purpose of this operator is to determine which value should be assigned to the variable.
    variable x = (expression) ? value if true : value if false

  • instanceof operator
    If the object referred to by the variable on the left side of the operator is an object of the class or interface on the right side of the operator, the result is true.
    ( Object reference variable ) instanceof (class/interface type)

String name = "James";
boolean result = name instanceof String; 
// Because name is a String type, it returns true

Operator priority

       //Binary operator
        //ctrl+D: assign the current row to the next row
        int a=10;
        int b=20;
        int c=30;
        int d=40;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a+b);
        System.out.println(a/(double)b); //0.5

        //Binary operation for different types
        System.out.println("================"); //0.5
        long a1=124L;
        int b1=123;
        short c1=10;
        byte d1=8;
        double e1=8.0;

        System.out.println(a1+b1+c1+d1+e1); //Double has a higher type than int, and the result is the highest type
        System.out.println(a1+b1+c1+d1); //Long has a higher type than int, and the result is a higher type
        System.out.println(b1+c1+d1); //Int
        System.out.println(c1+d1); //Int

        //Unary operator
        int x=a++; //First assign a value to x in auto increment
        int y=++a; //Assign a value to y by self increment first

        //Power operation 2 ^ 3 2 * 2 * 2 = 8 many operations are performed using tool classes
        System.out.println("================");
        double pow = Math.pow(2,3);
        System.out.println(pow);

        //Logical operation
        //->Short circuit operation
        System.out.println("================");
        int q=5;
        boolean w=(q<4)&&(q++<4); //If Q < 4 is judged as false, w=false is known, and Q + + < 4 is not judged
        System.out.println(q);
        System.out.println(w);

        //Bit operation
        System.out.println("================");
        /**
         2*8=16  2*2*2*2
         High efficiency!!
         << *2
         >> /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);
        System.out.println(8<<1);

        //String connector
        System.out.println("================");
        a=10;
        b=5;
        a+=b; //a=a+b
        System.out.println(a);
        a-=b; //a=a-b
        System.out.println(a);
        //String connector +, string
        System.out.println(""+a+b); //'10' + '5' first String type is changed to String
        System.out.println(a+b+""); //After 10 + 5, String is calculated first, and the operation result is converted to String;

Package mechanism

  • Function: used to distinguish the namespace of class names (generally using the inversion of company domain names as package names)
    package pkg1[.pkg2[.pkg3...]]

  • To use package members, you need to import the package, and use the "import" statement
    import package1[.package2...].(classname|*);

javaDoc

  • Function: javaDoc command is used to generate its own API documents
    Link: JDK help documentation

  • parameter information
    @Author: author name
    @Version: version number
    @since: the earliest JDK version required for naming
    @Description: description
    @param: parameter name
    @Return: return value
    @date: date
    @throws: exception thrown

  • Generation method
    1. Use the command line to generate
    a. Open the file where the class is located and execute cmd
    b. Input: javadoc -encoding UTF-8 -charset UTF-8 Doc.java
    2. Generate using IDEA
    a. Tools->Genrate JavaDoc...
    b.-encoding utf-8 -charset utf-8

Topics: Java Back-end