编程知识 cdmana.com

[unfinished] java basic syntax

Java Basic grammar

1. notes

The first thing to consider is , Why learning Java Learn notes from the beginning ?

The reason is simple , The notes are very interesting important , Because when developing a complex program , Writing notes can remind yourself and colleagues reading your own program , So you can reduce the cost of time . You can imagine the following , Even if it's a self written program , You can make sure you can remember a function in ten days and a half months 、 What does a variable represent ? By reading the comments next to the code, you can help yourself quickly recall .

Comments are not executed , It just serves as a reminder , It's a good habit to write notes in programs .

There are three types of annotations :

  1. Single-line comments

    Used to annotate a line , grammar :// content

  2. Multiline comment

    Used to annotate multiple lines , grammar : /* content */

  3. Documentation Comments

    Write a document for the program , grammar :/** content */

    public class Hello{
        public static void main(String[] args){
            //  This is a single line comment 		
            System.out.print("Hello World!");
    
            /** 
             * @description:  This is the document comment 
             * @Author: Zill
             */
    
            /*	 This is a multiline comment 
             *      ┌─┐       ┌─┐ + +
             *   ┌──┘ ┴───────┘ ┴──┐++
             *   │                 │
             *   │       ───       │++ + + +
             *   ███████───███████ │+
             *   │                 │+
             *   │       ─┴─       │
             *   │                 │
             *   └───┐         ┌───┘
             *       │         │
             *       │         │   + +
             *       │         │
             *       │         └──────────────┐
             *       │                        │
             *       │                        ├─┐
             *       │                        ┌─┘
             *       │                        │
             *       └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
             *         │ ─┤ ─┤       │ ─┤ ─┤
             *         └──┴──┘       └──┴──┘  + + + +
             *                 God bless 
             *                No code BUG!
             */
    
        }
    }
    
    

2. Keywords and identifiers

keyword By Java The language itself is well defined , Identifiers with special uses , Only according to Java The syntax of is required to be used . The following figure for Java All the keywords of :

abstract assert boolean break byte
case catch char class const
continue default do double else
enum extends final finally float
for goto if implements import
instanceof int interface long native
new package private protected public
return strictfp short static super
switch synchronized this throw throws
transient try void volatile while

identifier seeing the name of a thing one thinks of its function , It's used to identify classes 、 Variable 、 Methodical , The use of identifiers should follow the following standards :

1.  identifier *** should *** With A-Z(a-z)、$  and  _  Start ;
2.  After the second letter you can use  A-Z(a-z)、$ _ 、0-9 Name it ;
3.  You can't use keywords as identifiers ;
4.  Identifiers are case sensitive ;
5.  Chinese can be used as an identifier , however ** Don't suggest ** Use , Pinyin is not recommended ;

public class Demo{
    public static void main(String[] args) {
        String  Hello  = "Hello";
    }
}

data type

Java It's a door Strong type language , requirement Variable The use of the product should strictly comply with the regulations , All variables must be defined before they are used . That is to say, the type of variable should be defined first , And the general type will not change during use , Used C/C++ It's also a strong type of language , Corresponding _ Weak type language _ And JS、VB.

1 byte = 1 Byte

Java Data types are divided into two categories :1、 Basic types 2、 Reference type

Basic data type - value type - Integer type byte(1 Byte)、short(2 Bytes)、int(4 Bytes)、long(8 Bytes)

​ - Character type char(2 Bytes) One Chinese takes one char size For example, a Letter 、 Numbers 、 A character symbol is a character

​ - floating-point float(4 Bytes)、 double(8 Bytes) Zunxu IEEE754 standard

​ - Boolean type (1 Byte) Only true & false

Reference data type - class 、 Interface 、 object

public class demo01 {
    public static void main(String[] args) {
        //  Eight basic types 
        //  Integers                 The range of values that can be expressed 
        byte num1 = 1;      // -128 ~ 127
        short num2 = 2;     // -32768 ~ 32767
        int num3 = 3;       // -2147483648 ~ 2147483647 ( Probably 21 Billion )
        long num = 4L;      //  It needs to be followed by L  Expressed as Long type 

        //  Floating point numbers 
        float f1 = 1.1F;    //  It needs to be followed by F  Expressed as float type 
        double d1 = 3.1415926;  //  Double precision floating point 
        //  Character 
        char c1 = 'a';      //  Character 
        String S1 = "Hello"; // String  Not a basic type , It's a class ;
        // boolean type 
        boolean flag = true;    // Boolean type 
    }
}


Interview questions

1. Binary problem

Binary system 、 octal 、 Decimal system 、 Hexadecimal

public class demo03 {
 public static void main(String[] args) {
     //  Binary system   octal   Decimal system   Hexadecimal 
     int num1 = 0b10;        //  Binary in 0b start   On the two into one 
     int num3 = 010;         //  Octal to 0 start   Every eight enters one. 
     int num2 = 10;          //   Dot into one 
     int num4 = 0x10;        //  Hex to 0x start   Every 16 to one    0-9 a-f eg a = 10 f = 15

     System.out.println(num1);
     System.out.println(num2);
     System.out.println(num3);
     System.out.println(num4);
 }
}


Output results

2. The financial problem of floating point numbers

public class demo02 {
    public static void main(String[] args) {
        //  Observe the following , Please analyze the output why 
        float f1 = 0.1F;
        double d1 = 1.0 / 10;

        System.out.println(f1 == d1);

        System.out.println(f1);
        System.out.println(d1);

        System.out.println("=================================");
		//  Just type in a very long piece of data 
        float f2 = 2222222222222222222222F;
        double d2 = f2 + 1;

        System.out.println(f2 == d2);
    }
}


The output is :

Whether it's strange ? Two numbers that seem to be equal f1 and d1 , The judgment is false, Two seemingly unequal numbers d2 and f2, The judgment is true!!!

The reason is :

Floating point numbers can only represent Limited scope 、 Is a discrete value 、 There is rounding error Therefore, the above problems will appear .

So when dealing with banking , It's better to avoid using floating point numbers for comparison , Often by using classes BigDecimal Mathematical tools

版权声明
本文为[osc_ zhang]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/03/20210331191515280y.html

Scroll to Top