day1.4--java basic syntax

Keywords: Java Big Data Eclipse

Comments, identifiers, keywords

  • Note: self help writing is a very good habit
  1. Single-Line Comments
  2. multiline comment
  3. Document comments are not executed

New Project create a New Project

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-uWnXzV4b-1632566320691)(C:\Users320\Desktop\java learning \ pic \ screenshot 3.png)]

New Module creates a new template

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-wW6k1kRs-1632566320698)(C:\Users320\Desktop\java learning \ pic \ screenshot 4.png)]

Project Structure (set environment variables)

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-mgSQAZ5q-1632566320700)(C:\Users320\Desktop\java learning \ pic \ screenshot 2.png)]

Modify annotation color

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-0PhFA26L-1632566320702)(C:\Users320\Desktop\java learning \ pic \ screenshot 5.png)]

[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-6v1JO2AH-1632566320703)(C:\Users320\Desktop\java learning \ pic \ screenshot 6.png)]

//Interesting code comments

public class HelloWorld {
    public static void main(String[] args) {
        //Output a Hello world!
        //Single-Line Comments 
        System.out.println("Hello World!");

        /*
        multiline comment 
        You can annotate multiline text
         */
        
        
        //JavaDoc: document comments  
        /** 
        emmmm
        emmmmmmmmm
        emmmmmmmmmmmmmmmmmmm
        */


    }
}

identifier

  • keyword

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

    Attention

    • All identifiers should start with a letter (A-Z or A-Z), dollar sign ($), or underscore ()
    • The first letter can be followed by any combination of letters (A-Z or A-Z), dollar sign ($), underscore () or numbers
    • Keywords cannot be used as variable names
    • Identifiers are case sensitive

data type

  • Strongly typed language

    • It is required that the use of variables should strictly comply with the regulations, and all variables must be defined before they can be used
  • Weakly typed language js

  • java data types are divided into two categories

    • primitive type

      1. value type

        Integer type: byte 1

        ​ short 2

        ​ int 4

        ​ long 8

        Floating point type: float: 4 bytes

        double: 8 bytes

        Character type: char: 2 bytes

    • reference type

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 uppercase B

  • 1B (Byte sub section) = 8bit (bit)

  • Characters: letters, numbers and symbols used in computers

    • 1 bit means 1 bit

    • 1Byte represents a byte 1B=8b

    • 1024B=1KB

    • 1024M=1GB

    • 1GB=1TB

      public class Demo03 {
          public static void main(String[] args) {
              //Integer expansion: binary 0b decimal octal 0 hexadecimal 0x
              int i = 10 ;
              int i2 =010; //Octal 0
              int i3 =0x10; //Hex 0x
      
              System.out.println(i);
              System.out.println(i2);
              System.out.println(i3);
      
              System.out.println("=======================================================");
      //==============================================================================================
      
              //How to express floating-point expansion of banking business? money
              //BigDecimal math tool class
              //float finite discrete rounding error
              //double
      
              // It is best to avoid using floating-point numbers for comparison
              // It is best to avoid using floating-point numbers for comparison
              // It is best to avoid using floating-point numbers for comparison
      
              float f = 0.1f;    //0.1
              double d = 1.0/10; //0.1
      
              System.out.println(f==d);
              System.out.println(f);
              System.out.println(d);
      
              float d1 = 2232323232323232322f;
              float d2 = d1 + 1;
              System.out.println(d1==d2);
      
              System.out.println("=======================================================");
      
              //============================================================================
              //Character expansion
              //=============================================================================
              char c1= 'a';
              char c2 ='in';
              System.out.println(c1);
              System.out.println((int)c1);  //Force conversion
      
              System.out.println(c2);
              //All characters are still numbers in nature
              //Encoding unicode table: 97 = a 2 bytes 65535 excel 2 ^ 16 = 65536
      
              //u0000 uFFFF
              char c3 = '\u0061';
              System.out.println(c3); //a
              //Escape character
              //\Tabbed \ nline break
              // ......
              System.out.println("Hello \t world!");
              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
      		System.out.println("=======================================================");
              //Boolean extension
              boolean flag = true;
              if (flag=true){}
              if (flag){}
              //less is more code should be concise and easy to read
      
          }
      }
      
      

      Type conversion

      • Because java is a strongly typed language, type conversion is required for some operations

      Low -------------------------------------------------------------------------------------------- high

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

      • In the operation, different types of data are first converted to the same type, and then the operation is carried out
      • Cast type
      • Automatic type conversion
public class Demo05 {
    public static void main(String[] args) {
        int i = 128;
        double b = i ;    //-128 ~ 127 memory overflow cast (type) variable name
        System.out.println(i);
        System.out.println(b);
        /*
        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("==========================");
        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 Demo06 {
    public static void main(String[] args){
        //When operating a large number, pay attention to the overflow day
        //jdk7 new feature, numbers can be separated by underscores
        int money = 10_0000_0000;
        System.out.println(money);

        int years = 20;
        int total1 = money*years;  //-1474836480, overflow during calculation
        long total2 = money*years; //The default is int. there is a problem before the conversion l
        long total3 = money*(long)years; //First convert a number to long
        System.out.println(total1);
        System.out.println(total2);
        System.out.println(total3);
        //long try to use capital L
    }
}

variable

  • What is a variable: it is a variable

  • java is a strongly typed language. Every variable must declare its type

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

    type varName [=value][{					}]
    
    • matters needing attention:
      • 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

Variable scope

  • Class variable

  • Instance variable

  • local variable

    public class Variable{
        static int allClicks=0;     //Class variable
        String str = "hello world"; //Instance variable
        public void method (){
            int i = 0 ;             //local variable
        }
    }
    
    public class Demo08 {
        //Class variable static
        static double salary = 2500;
        
        //Attributes; variable
        //Instance variable: subordinate to object; If you do not initialize yourself, the default value of this type is 0.0
        //Boolean: false by default
        //The default values are null except for the basic type
        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 Demo08();
            Demo08 demo08 = new Demo08();
            System.out.println(demo08.name);
            System.out.println(demo08.age);
        }
    }
    

constant

  • Constant: the value cannot be changed after initialization; Value that will not change;

  • The so-called constant can be understood as a special variable, and its value cannot be changed during program operation after it is set;

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

Naming conventions for variables

  • All variables, methods and class names: see the meaning of the name
  • Class member variables: initial message amount and hump principle: monthSalary except the first word, the following words are capitalized lastName
  • Local variables: initial lowercase and hump principle
  • Constants: uppercase letters and underscores: MAX_VALUE
  • Class name: initial capitalization and hump principle; Man,GoodMan
  • Method name: initial lowercase and hump principle: run(),runRun()

Basic operator

  • java supports the following operators: (priority ())

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

    • Assignment operator:=

    • Relational operators >, <, > =, < =, = == instanceof

    • **Logical operators: & &, |, |**

    • Bitwise operators: &, |, ^, ~, > >, <, > > (understand)

    • Conditional operator?:

    • Extended assignment operator: + =, - =, * =/=

package operater;

public class Demo01 {
    public static void main(String[] args) {
        //Binary operator
        int a = 10 ;
        int b = 15 ;
        int c = 20 ;
        int d = 25 ;
        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/(double)b); //Just cast one
    }
}

package operater;

public class Demo02 {
    public static void main(String[] args) {
        long a = 12131223131344L;
        int b = 123;
        short c = 10 ;
        byte d =8;
        System.out.println(a+b+c+d); //long
        System.out.println(a+b+c);  //int
        System.out.println(a+b);  //Int cast: conversion


    }
}

package operater;

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(c%a); //Surplus
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);

    }
}

package operater;

public class Demo04 {
    public static void main(String[] args) {
        //++-- self increasing and self decreasing unary operator
        int a = 3;
        int b = a++;  //After a++ a=a+1 executes this line of code, assign a value to b first, and then increase it automatically
        int c = ++a;  //++After a = a + 1 executes this line of code, it will increase automatically first, and then assign a value to b
        System.out.println(a);
        System.out.println(a);
        System.out.println(b);
        System.out.println(b);
        System.out.println(c);
        System.out.println(c);

        //Power operation 2 ^ 3 2 * 2 * 2 = 8 many operations we will use some tool classes to operate
        double pow = Math.pow(3,2); // 3*3
        System.out.println(pow);
        }
}

package operater;

public class Demo05 {
    public static void main(String[] args) {
        // And (and) or (0r) 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: if one of the two variables is true, 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);
        System.out.println(d);
        System.out.println(c);
        

    }
}

package operater;

public class Demo06 {
    public static void main(String[] args) {
        /*
        A= 0011 1100
        b =0000 1101
------------------------------------------------------
        A&B = 0000 1100    //If there is a 0, it is 0
        A|B = 0011 1101    //If there is a 1, it is 1
        A^B = 0011 0001    //The same is 0, the difference is 1
        ~B = 1111 0010     //Reverse

        2*8=16     2*2*2*2=
        Extremely efficient
        <<  Move left * 2 > > move right / 2
        0000 0000    0
        0000 0001    1
        0000 0010    2
        0000 0100    4
        0000 1000    8
        0001 0000    16

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

    }
}

package operater;

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+
        System.out.println(a+b);
        System.out.println(""+a+b);    //String before full splicing
        System.out.println(a+b+"");    //After the string is, the operation will still be performed before it
    }
}

package operater;

public class Demo08 {
    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";  //Must master
        System.out.println(type);
    }
}

Package mechanism

  • In order to better organize classes, Java provides a package mechanism to distinguish the namespace of class names

  • The syntax format of the package statement is: package1.package2.package3;

  • Generally, the company domain name is inverted as the package name: www.baidu.com.baidu.www

  • In order to use the members of a package, we need to explicitly import the package in the Java program. This can be done using the "import" statement

    import package1.package2.package3.*;
    

    JavaDoc

    • The javadoc command is used to generate its own api documentation
    • parameter information
      • @Author author name
      • @Version version number
      • @since you need the earliest jdk version
      • @param parameter name
      • @Return return value
      • @throws exception thrown
package com.zzx.base;

/**
 * @author Kuangshen
 * @version 1.0
 * @since 1.8           //Can write the author and version number
 */
public class Doc {
        String name; //Class variable

    /**
     * @author Kuangshen
     * @param name
     * @return
     * @throws Exception
     */
        public String test(String name) throws Exception
        {
            return name;
        }
}
//The annotation added to the method is the annotation on the method
//Adding to a class is the annotation of the class
//From the command line javadoc -encoding UTF-8 -charset UTF-8 Doc.java
//Homework: learn to find javadoc documents produced by IDEA and program for Baidu
//All the basic knowledge will be used almost every day

Return return value
-@ throws exception thrown

package com.zzx.base;

/**
 * @author Kuangshen
 * @version 1.0
 * @since 1.8           //Can write the author and version number
 */
public class Doc {
        String name; //Class variable

    /**
     * @author Kuangshen
     * @param name
     * @return
     * @throws Exception
     */
        public String test(String name) throws Exception
        {
            return name;
        }
}
//The annotation added to the method is the annotation on the method
//Adding to a class is the annotation of the class
//From the command line javadoc -encoding UTF-8 -charset UTF-8 Doc.java
//Homework: learn to find javadoc documents produced by IDEA and program for Baidu
//All the basic knowledge will be used almost every day

Posted by c815902 on Sat, 25 Sep 2021 03:02:19 -0700