Basic Java lesson 1

Basics

notes

  1. Single line comment: / /

  2. Multiline comment: / **/

  3. Document notes:/**

    ​ *

    ​ */

identifier

It refers to a symbol used to identify an entity and the name used by users in programming. It is used to name variables, constants, functions, statement blocks, etc

  • All identifiers should be in letters (AZ, AZ), dollar sign ($), underscore () Any composition of numbers, and numbers cannot be the first letter
  • Keywords cannot be used as variable or method names
  • Identifiers are case sensitive
  • Chinese naming or pinyin can be used, but it is not recommended

keyword

data type

👨‍🚀 Strongly typed language: the use of variables is required to strictly comply with the regulations. All variables must be defined before they can be used

🌍 Weakly typed language: it is required that the use of variables can not comply with the regulations

  1. Primitive type:

    • Long type adds' L 'after the number, float type adds' F' after the decimal, character char contains only one word, expressed in single quotation marks, and String contains multiple words (String is not a keyword, but a class)

    • Try to avoid floating-point numbers for comparison, because float is limited, discrete, rounding error, close to but not equal to

      Bigdeciaml for Banking (mathematical tools)

  2. Reference type:

  1. \u0000~\uffff: Escape Character

    \t: Tab

    \n: Line feed

    ...

  2. case

    float a = 0.1f;
    double b = 1.0/10;
    System.out.println(a==b);  //false
    float d1 = 2333333333333333333333333f;
    float d2 = d1 + 1;
    System.out.println(d1==d2);  //true
    char c = 's';  //The character type is a single character
    

Type conversion

  1. Type conversion priority:

In the operation, different types of data are transformed into the same type first, and then the operation is carried out

  • Low to high can be assigned directly (automatic type conversion)

    Converting from high to low requires forced type conversion (there may be memory overflow or precision problems during conversion)

  • Cast: (type) variable name

  • Boolean values cannot be converted

  • Object types cannot be converted to irrelevant classes

  1. case

    int d = 128;
    byte e = (byte)d; //out of memory
    double f = d;
    
    System.out.println(d);  //128
    System.out.println(e);  //-128
    System.out.println(f);  //128.0
    
    /*
    be careful:
    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 precision problems during conversion (decimal to integer, decimal part will be lost)
    char c = 'a';
    int d = c+1;
    System.out.println(d);//98
    System.out.println((char)d);//b
    */
    
    
    System.out.println("==============practice==============");
    /*
    When operating large numbers, pay attention to the overflow problem
    JDK7 New feature, numbers can be separated by an underscore
    */
    int money = 10_0000_0000;
    int years = 20;
    int total = money*years;
    System.out.println(total);  //-1474836480, overflow
    long total2 = money*years;
    System.out.println(total2);  //-1474836480, still overflowing (money and years are int types, money*years results in int, and then convert the result to long, that is, there is a problem before conversion)
    long total3 = money*((long)years);
    System.out.println(total3);
    

variable

Naming conventions for variables

  1. All variables, methods and types: see the meaning of the name
  2. Class member variables: lowercase initial and hump principle: monthSalary (except the first word, the following words are capitalized)
  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: runRun()

basic

  1. A variable is a transformable quantity:

    Data type variable name = value;

    type varName [=value] [{,varName[=value]}], you can declare multiple variables of the same type separated by commas, but it is not recommended to specify multiple variables in one line

  2. Java is a strongly typed language. Every variable must declare its type

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

    • Scope:

      • Class variable: there must be static method outside and class inside (direct access)

        static int allClicks=0;

      • Instance variable: outside the method and inside the class, it belongs to object java1_ 1 (access instance variables through objects)

        String str="hello world";

      • Local variable: a variable in a method that must be declared and initialized

        public class Variable{

        ​ static int allClicks=0; // Class variable

        String str = "hello" / / instance variable

        ​ public void method(){

        ​ int i=0; // local variable

        ​ }

        }

  4. Variable name must be a legal identifier

  5. case

    public class Java1_1 {
    
        //Class variable static keyword
        static double salary = 2500;
    
        //Attributes: Variables
        /*
         Instance variable: subordinate object; If not initialized automatically, the default value of this type is returned
         Boolean default false
         All basic types except the basic type are null
         */
        String name;
        int age;
        
        //main method 
        public static void main(String[] args) {
            
            //Local variables: values must be declared and initialized
            int i=10;
            System.out.println(i);
            
            //Variable type variable name = new Jave1_1();—— Instance variable
            Java1_1 java1_1 = new Java1_1();
            System.out.println(java1_1.age);//Access instance variables through objects
    
            //Class variable
            System.out.println(salary);//Direct access
        }
    
        //Other methods
        public void add(){
        }
    }
    

constant

  1. Constant: the value that cannot be changed after initialization. The value that will not change is represented by the keyword final

    //Modifier, does not exist
    static final double PI = 3.14;
    
  2. The so-called constant can be understood as a special variable. After its value is set, it is not allowed to be changed during the operation of the program.

  3. Constant names are usually in uppercase characters

Keywords: Java Typora

Added by jcinsov on Sat, 19 Feb 2022 05:53:06 +0200