Java basic theory + code practice

notes:

  • Comments will not be executed. They are for the people who write our code

  • There are three types of annotations in java:

    • Single line note//

    • Multiline comment/**/

    • /**
       * Documentation Comments 
       */
      

Identifier:

Definition: all components of java need names. Class names, variable names and method names are called identifiers. The following basically covers most identifiers in java.


797)(C:\Users\L\AppData\Roaming\Typora\typora-user-images\image-20201110180539920.png)]

Identifier notes:

  • All identifiers should start with letters, dollar characters, and underscores
  • The first letter can be followed by a letter, a dollar sign, an underscore or a number
  • Keywords cannot be used as variable or method names
  • Identifiers are case sensitive
  • It can be named in Chinese, but it is generally not recommended to use it in this way, and Pinyin is not recommended. It is very low.

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.

The data types of java are divided into two categories:

  • primitive type

    • value type

      • Integer type:

        • Byte: occupies a byte range
        • short: in the range of two bytes
        • int: in the range of four bytes
        • long: in the range of eight bytes
      • Floating point type:

        • float: in the range of four bytes

        • double: in the range of eight bytes

      • Character type:

        • char: 2 bytes
    • boolenan type: occupy one bit, and its value is only true and false

  • reference type

    • class
    • Interface
    • array

What are bytes:

  • Bit: it is the smallest unit of data storage in the computer. 11001100 is an eight bit binary number.
  • Byte: it is the basic unit of data processing in the computer. It is customarily expressed in capital B
  • 1B (byte) = 8bit (bit)
  • Characters: alphanumeric characters and symbols used in computers
  • 1 bit represents one bit,
  • 1byte means a byte 1B=8b
  • 1024B=1KB
  • 1024KB=1M
  • 1024M=1G
  • 1024G=1TB

Data type:

 //Integer expansion: binary 0b decimal octal 0 hexadecimal

         int i=10;
         int i2=010;
         int i3=0x10;

        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println("=========================Floating point number==========================");
        //Floating point expansion? What do you think of banking
        // Using the digital tool BigDecimal
        // 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=1.0/10;

        System.out.println(f==d); //false
        // The word length represented by float floating-point number is limited. Discrete floating-point number will round off the error, and the result is about the same number

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

        System.out.println(d1==d2);  //true is a large number, and the following will be discarded, so it will be equal
        System.out.println("=========================Character extension==========================");
        //Character extension
        //All characters are still numbers in nature
        // Encoding Unicode Table 2 byte 0 - 65536 excel 2 16th power = 65536
        char c1='a';
        char c2='in';

        System.out.println(c1);

        System.out.println((int)c1);  //Cast type

        System.out.println(c2);

        System.out.println((int)c2);  //Cast type

       //U0000 UFFFF interval range

        char c3='\u0061';
        System.out.println("Unicode Representation of code:"+c3);  //a

        //Escape
        // /n newline / t space
        System.out.println("Hello\nWorld");
        System.out.println("============================================");
        String s=new String("Hello World");
        String s1=new String("Hello World");
        System.out.println(s==s1);
        //false


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

        //Boolean extension
        boolean flag=true;
        if (flag){}  //Veteran flag defaults to true
        if (flag=true){}   //Novice
        //The code should be concise and easy to read

Type conversion:

  • In the operation, different types of data are first transformed into the same type, and then the operation is carried out
  • byte,short,char->int->long->float->double
    • From low to high
 //Cast (type) variable name high -- low
         int i=128;
         byte b=(byte) i; //out of memory

        //Automatic conversion low high
        int i1=128;
        double b1=(double) i;

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

        System.out.println(i1);
        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 memory overflow or accuracy problems during conversion!
        * */


        System.out.println((int)23.7);  //23
        System.out.println((int)-45.7f); //45

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


        //When operating large numbers, pay attention to the overflow problem
        //jdk7 new feature, numbers can be separated by underscores, and underscores will not be output
         int money=10_0000_0000;
         System.out.println(money);
         int years=20;
         long total=money*years;  //-1474836480 overflowed money during calculation. The default is int. there was a problem before conversion
         System.out.println(total);

         long tota2=money*((long)years);
         System.out.println(tota2);

Variable:

  1. A variable is a variable!
  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.

Naming convention of variables:

  • Class names of all variable methods: see the meaning of names
  • Class member variables: first letter lowercase and hump principle: monthSalary (except the first word, the following words are capitalized)
  • Local variables: initial lowercase and hump principle
  • Constants: uppercase letters and underscores: Max_VAlLUE
  • Class name: initial capitalization and hump principle: Man, GoodMan
  • Method name: initial lowercase and hump principle: run(),runRun()
public class Demo6 {

      //Class variable static
      static double salary=2500;


       //Attributes: Variables
       //Instance variable: subordinate object: if it is not initialized by itself, the default value of this variable will be output
       //Boolean: false by default
       //The default is null except for the basic type
       String name="11";
       int age;
    public static void main(String[] args) {
        //variable
         //int a=1,b=2,c=3;
         String name="qj";
         char x='x';
         double opi=3.14;

        //Local variables must be declared and initialized
        System.out.println(name);
        //Variable type variable name = new Demo6();
        Demo6 demo6 = new Demo6();
        System.out.println(demo6.age);
        System.out.println(demo6.name);

        System.out.println(salary);

    }
     public void add(){
        
        
     }
}

Scope:

  • Each variable has a type, which can be either a basic 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.

Constant:

  • Constant: the value cannot be changed after initialization! Unchanged value

  • 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.

    final Variable name=Value;
    final double PI=3.14
    
  • Constant names generally use uppercase characters.

Tags: Java

Posted by rlalande on Sun, 08 May 2022 06:19:27 +0300