[Super invincible and detailed Han Shunping's java notes] From beginner to proficient - data types, character encoding table

Table of contents

1. Data type

2. Integer type

1.Basic introduction

 2. Details of the use of integers

3. Variables in Java programs are declared as int type. Long is used unless it is not large enough to represent a large number.

4.bit: The smallest storage unit in a computer

byte: the basic storage unit in a computer

 3. Floating point type

1.Basic introduction

2. Classification of floating point types

3. Usage details of floating point types

4. Floating-point constants have two representation forms

5. Normally, the double type should be used because it is more accurate than the float type.

6. Traps in using floating point numbers: 2.7 and 8.1 / 3 comparison

4. Character type (char)

1. Basic introduction

2. Code Char01.java

3 Character type usage details

 4.Character encoding table editing

4.1 ASCII encoding

 4.2 Unicode encoding

 4.3 UTF-8 encoding

 5. Boolean type: boolean


1. Data type

Each kind of data has a clear data type defined and different sizes of memory space ( bytes ) allocated in the memory .

 

 The basic data types include 8 numerical types [byte, short, int, long, float, double] char, boolean

2. Integer type

1.Basic introduction

Java 's integer type is used to store integer values, such as 12, 30, 3456 , etc.

 2. Details of the use of integers

1. Each integer type in Java has a fixed range and field length, which is not affected by the specific os [operating system] to ensure the portability of Java programs.

2. Java's integer constants (specific values) default to int type. When declaring a long constant, you must add 'l' or 'L' after it.

(The compiler first reads 1L and finds that there are 8 bytes. It puts it into int and finds that it cannot fit, so it reports an error.)

 Code:

public class IntDetail {
    //编写一个 main 方法
    public static void main(String[] args) {
    //Java 的整型常量(具体值)默认为 int 型,声明 long 型常量须后加‘l’或‘L’
        int n1 = 1;//4 个字节
        //int n2 = 1L;//对不对?不对
        long n3 = 1L;//对
    }
}

3. Variables in Java programs are declared as int type. Long is used unless it is not large enough to represent a large number.

4.bit: The smallest storage unit in a computer

byte: the basic storage unit in a computer

1byte = 8 bit

 

 3. Floating point type

1.Basic introduction

Java 's floating point type can represent a decimal, such as 123.4 , 7.8 , 0.12, etc.

2. Classification of floating point types

  • Floating point number = sign bit + exponent bit + mantissa bit
  • The mantissa part may be lost, resulting in loss of accuracy ( decimal values ​​are approximate ) .

3. Usage details of floating point types

1. Each Java floating point type has a fixed range and field length, which is not affected by the specific OS [operating system linux, mac,] to ensure the portability of Java programs

2. Java's floating-point constants (specific values) default to double type. When declaring a float-type constant, you must add 'f' or 'F' after it.

(float=1.1 is an error. Floating-point values ​​default to double type, so 1.1 is double type, which is 8 bytes. If you put it into float and find that it cannot fit, an error will be reported)

public class FloatDetail {
//编写一个 main 方法
public static void main(String[] args) {
//Java 的浮点型常量(具体值)默认为 double 型,声明 float 型常量,须后加‘f’或‘F' //float num1 = 1.1; //对不对?错误
float num2 = 1.1F; //对的
double num3 = 1.1; //对
double num4 = 1.1f; //对

4. Floating-point constants have two representation forms

Decimal number format: such as: 5.12 512.0f        . 512         (must have decimal point)

Scientific notation form:   5.12e2 [5.12*10 to the power 2 = 512.0]               

                                5.12E-2 [5.12/10 to the 2nd power or 5.12*10 to the 2nd power = 0.0512]

public class FloatDetail {
    //编写一个 main 方法
    public static void main(String[] args) {
    //十进制数形式:如:5.12 512.0f .512 (必须有小数点)
        double num5 = .123; //等价 0.123
        System.out.println(num5);
        //科学计数法形式:如:5.12e2 [5.12 * 10 的 2 次方 ] 5.12E-2 []
        System.out.println(5.12e2);//512.0
        System.out.println(5.12E-2);//0.0512
    }
}

5. Normally, the double type should be used because it is more precise than the float type.

double num9 = 2.1234567851;

float num10 = 2.1234567851F;

public class FloatDetail {
    //编写一个 main 方法
    public static void main(String[] args) {
    //通常情况下,应该使用 double 型,因为它比 float 型更精确。
    //[举例说明]double num9 = 2.1234567851;float num10 = 2.1234567851F;
        double num9 = 2.1234567851;
        float num10 = 2.1234567851F;
            System.out.println(num9);
            System.out.println(num10);
    }
}

6. Traps in using floating point numbers: 2.7 and 8.1 / 3 comparison

public class FloatDetail {
    //编写一个 main 方法
    public static void main(String[] args) {double num11 = 2.7;
        double num12 = 8.1 / 3; //2.7
            System.out.println(num11);//2.7
            System.out.println(num12);//接近 2.7 的一个小数,而不是 2.7
    }
}
When we judge the equality of an operation result that is a decimal, we use the absolute value of the difference between the two numbers to judge within a certain precision range.
public class FloatDetail {
    //编写一个 main 方法
    public static void main(String[] args) {double num11 = 2.7;
        double num12 = 8.1 / 3; //2.7
        System.out.println(num11);//2.7
        System.out.println(num12);//接近 2.7 的一个小数,而不是 2.7
//得到一个重要的使用点: 当我们对运算结果是小数的进行相等判断是,要小心
//应该是以两个数的差值的绝对值,在某个精度范围类判断
    if( num11 == num12) {
        System.out.println("num11 == num12 相等");
}
//不输出这句话
//正确的写法 , ctrl + / 注释快捷键, 再次输入就取消注释
    if(Math.abs(num11 - num12) < 0.000001 ) {
        System.out.println("差值非常小,到我的规定精度,认为相等...");
        System.out.println(Math.abs(num11 - num12));
//细节:计算num11-num12的绝对值的差值,如果是直接查询得的的小数或者直接赋值,是可以判断相等
//通过Java API来看
        }
}

4. Character type (char)

1. Basic introduction

The character type can represent a single character . The character type is char . char is two bytes ( can store Chinese characters ) . For multiple characters, we use the string String

2. Code Char01.java

public class Char01{
	public static void main(String []args){
		char c1 = 'a';
		char c2 = 97;
		char c3 = 'a';
		char c7 = 'a' + 1;

		char c4 = '韩';
		char c6 = 38889;



		System.out.println(c1);//a
		System.out.println((int)c1);//97
		System.out.println(c1+1);//98
		System.out.println((char)(c1+1));//b
		System.out.println(c2);//a

		System.out.println(c7);//b
		System.out.println((int)c7);//98

		System.out.println(c4);//韩
		System.out.println((int)c4);//38889
		System.out.println(c6);//韩

	}
}

run:

3 character type usage details

1. A character constant is a single character enclosed in single quotes (' ').

For example: char c1 = 'a'; char c2 = 'Chen'; char c3 = '9';

char c3 = '99'; error report

Character constants enclosed in single quotes can only contain one character, and the char type can only store one character. Therefore, char c1='9' works fine because it contains only one character '9'. And char c1='99' will cause a compiler error because it contains two characters and cannot be stored in a char type variable.

 2. Use the escape character '\' to convert characters into special character constants

For example: char c3 = '\n'; // '\n' represents a newline character

3. In Java , the essence of char is an integer. In the default output, it is the character corresponding to the unicode code;

To output the corresponding number, you can use (int) characters

URL: Unicode encoding conversion-Webmaster Tools

 4. The char type can be operated on, which is equivalent to an integer, because it all corresponds to Unicode code .

unicode URL: The most complete unicode encoding_unicode code_LemonWatermelon's blog-CSDN blog

ascii website: ascii code comparison table - the most complete ASCII encoding table

 4.Character encoding table

4.1 ASCII encoding

 4.2 Unicode encoding

 4.3 UTF-8 encoding

 5. Boolean type: boolean

Code:

public class Boolean01 {
//编写一个 main 方法
public static void main(String[] args) {
//演示判断成绩是否通过的案例
//定义一个布尔变量
boolean isPass = true;//
if(isPass == true) {
System.out.println("考试通过,恭喜");
} else {
System.out.println("考试没有通过,下次努力");
}
}
}

Guess you like

Origin blog.csdn.net/qq_45206556/article/details/131720252