Java basic syntax

Java basic syntax

notes

  • We usually write code. When the amount of code is relatively small, we can still understand what we write, but when the project structure is complex, we need to use comments.

  • Comments are not executed. They are for our code writers. Writing comments is a very good habit

  • There are three types of annotations in Java:

    • Single-Line Comments

    • multiline comment

    • Documentation Comments

 
 1 public class HelloWorld {
 2      public static void main(String[] args) {
 3          //Single-Line Comments 
 4          //Output a Hello,World
 5          
 6          System.out.println("Hello,World!");
 7          
 8          //multiline comment    /*  notes  */
 9          
10          /*
11          many
12          that 's ok
13          notes
14          Buddhism
15           */
16          
17          //JavaDoc:Documentation Comments    /**   */
18          
19          /**
20           * @Description HelloWorld
21           * @Author JSS
22           */
23      }
24  }

 

identifier

keyword

Identifier considerations

  • All identifiers should start with letters (A-Z or a-z), dollar sign ($), or underscore ()

  • The first character can be followed by any combination of letters (A-Z or a-z), dollar sign ($), underscore () or numbers

  • Keywords cannot be used as variable or method names.

  • Identifiers are case sensitive

  • Examples of legal identifiers: age, $salary_ value, 1 _value

  • Examples of illegal identifiers: 123abc, - salary. #abc

  • 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

 
 1 public class Demo01 {
 2      public static void main(String[] args) {
 3  4          String Glory of Kings = "Hundred star king";
 5          //String Glory of Kings = "Stubborn bronze";
 6          System.out.println(Glory of Kings);
 7  8          /*
 9          String Ahello = "JSS";
10          String hello = "JSS";
11          String $hello = "JSS";
12          String _hello = "JSS";
13          */
14          //Illegal identifier
15          
16          //Case sensitive
17          //String Man = "JSS";
18          //String man = "JSS";
19      }
20  }

 

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

Java data types fall into two categories

  • Primitive type

    • value type

      • Integer type

        • byte   1B   -128~127

        • short   2B   -32768~32767

        • int   4B

        • long   8B

      • Floating point type

        • float  4B

        • double  8B

      • Character type Char   2B

    • boolean type 1 bit 0 or 1

      • true

      • 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: letters, numbers, words and symbols used in computers

1bit represents 1 bit, 1Byte represents a byte, 1B=8b. 1024B=1KB , 1024KB= 1 M 1024M=1G.

variable

  • What is the variable: it is the 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.

    1 type varName [=value] [{, varName[=value]}] ;
    2 //Data type variable name =value;You can declare multiple variables of the same type separated by commas.
     
  • 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 declarations are - complete statements, so each declaration must end with a semicolon

 
 1 public class Demo03 {
 2      public static void main(String[] args) {
 3          //Integer extension: binary 0 b    Hex 0 x    Octal 0
 4  5          int i = 10;
 6          int i2 = 010;    //Octal 0
 7          int i3 = 0x10;   //Hex 0 x  0~9  A~F
 8  9          System.out.println(i);
10          System.out.println(i2);
11          System.out.println(i3);
12          System.out.println("==================================================");
13 14          //====================================================
15          //Floating point extensions? How to express banking business? money
16          //BigDecimal    Mathematical tools
17          //====================================================
18          //float   The finite discrete rounding error is approximately close to but not equal to
19          //double
20          //It is best to use floating point numbers for comparison
21          float f = 0.1f;  //0.1
22          double d = 1.0/10;  //0.1
23          System.out.println(f==d);
24          System.out.println(f);
25          System.out.println(d);
26 27          float d1 = 21212121221212f;
28          float d2 = d1 + 1;
29 30          System.out.println(d1 == d2);//true
31 32          //====================================================
33          //Character extension?
34          //====================================================
35          char c1 = 'a';
36          char c2 = 'in';
37 38 39          System.out.println(c1);
40          System.out.println((int)c1);  //Force conversion
41          System.out.println(c2);
42          System.out.println((int)c2);   //Force conversion
43 44          //All characters are still numbers in nature
45          //code  Unicode  2 65536 bytes table: (97) = a  65 = A)Excel 2  16 65536
46 47          //U0000  UFFFF
48 49          char c3 = '\u0061';
50          System.out.println(c3);  //a
51 52          //Escape character
53          //  \t   Tab
54          //  \n   Line feed
55          // ....
56 57          System.out.println("Hello\nWorld");
58 59          //
60          System.out.println("============================");
61          String sa = new String("Hello World");
62          String sb = new String("Hello World");
63          System.out.println(sa==sb);
64 65          String sc = "Hello World";
66          String sd = "Hello World";
67          System.out.println(sc==sd);
68          //Object from memory
69 70          //Boolean extension
71          boolean flag = true;
72 73          if (flag==true){ }  //Novice
74          if (flag){ }  //an old hand
75 76          //less is More!  The code should be concise and easy to read
77 78      }
79 80  }
81  

 


    

Posted on Fri, 05 Nov 2021 15:51:03 -0400 by goltoof