The article directories

  • The data type
    • 1. Integer type
      • 1.1 Basic Introduction
      • 1.2 Case Demonstration
      • 1.3 Types of integers
      • 1.4 Usage details of integer intdetail.java
    • 2. Floating point type
      • 2.1 Basic Introduction
      • 2.2 Case Demonstration
      • 2.3 Classification of floating point types
      • 2.4 Explanation
      • 2.5 Floating point usage details
    • 3. Char
      • 3.1 Basic Introduction
      • 3.2 Case Demonstration
      • 3.3 Character Type usage details
      • 3.4 Exploring the nature of character types
      • 3.5 ASCII Code Introduction
      • 3.6 Introduction to Unicode Encoding
      • 3.7 UTF-8 Encoding (Understanding)
    • 4. Boolean type (Boolean)

The data type

  • Each type of data has a well-defined data type, and different sizes of memory space (bytes) are allocated in memory.

  1. Java data types fall into two broad categories: basic data types, reference types
  2. There are eight basic data types: byte, short, int, long, float,double, char, Boolean
  3. Reference type [class, interface, array]

1. Integer type

1.1 Basic Introduction

  • Java’s integer types are designed to hold integer values, such as 12,30,3456, and so on

1.2 Case Demonstration

byte nl =10;/ / 1 byte
short n2 =10;/ / 2 bytes
int n3 = 10;/ / 4 bytes
long n4= 10;/ / 8 bytes
Copy the code

1.3 Types of integers

1.4 Usage details of integer intdetail.java

public class IntDetail { 

	public static void main(String[] args) {

		//Java integer constants default to int, declare long constants must be followed by 'l' or 'l'
		int n1 = 1;/ / 4 bytes
		//int n2 = 1L; // Is that right? wrong
		long n3 = 1L;/ / for}}Copy the code



2. Floating point type

2.1 Basic Introduction

  • Java floating point types can represent a decimal number, such as 123.4, 7.8, 0.12, and so on

2.2 Case Demonstration

double score = 66.6; 
Copy the code

2.3 Classification of floating point types

2.4 Explanation

  1. A simple explanation of how floating-point numbers can be stored in a machine,Floating point = sign bit + exponent bit + mantissa bit
  2. Mantissa may be lost, resulting in loss of accuracy (All decimals are approximations).

2.5 Floating point usage details

  • Note: The Java default floating point constant isdoubleType, the statementfloatType constant, must be added"F" or "f"

public class FloatDetail { 

	// Write a main method
	public static void main(String[] args) {

		//Java float constants default to double, declare float constants followed by 'f' or 'f'
		/ / float num1 = 1.1; // Is that right? error
		float num2 = 1.1 F; / / right
		double num3 = 1.1; / / for
		double num4 = 1.1 f; / / for

		// Decimal number: 5.12 512.0 f. 512 (must have a decimal point)
		double num5 = 123.; / / equivalent to 0.123
		System.out.println(num5);
		5.12e2 [5.12 * 10 ^ 2] 5.12E-2 [5.12/10 ^ 2]
		System.out.println(5.12 e2);/ / 512.0
		System.out.println(5.12 e-2);/ / 0.0512


		// In general, you should use double because it is more accurate than float.
		//[Example]double num9 = 2.1234567851; Float num10 = 2.1234567851 F;
		double num9 =  2.1234567851;
		float num10 =  2.1234567851 F;
		System.out.println(num9);/ / 2.1234567851
		System.out.println(num10);/ / 2.1234567

		// Floating point numbers use traps: 2.7 vs. 8.1/3
		// Take a look at some code
		double num11 = 2.7;
		double num12 =  8.1 / 3;
		System.out.println(num11);/ / 2.7
		System.out.println(num12);// 2.69999999999997, a fraction closer to 2.7, not 2.7
		// An important point of use is to be careful when evaluating the equivalence of decimal values
		// It should be the absolute value of the difference between two numbers
		if( num11 == num12) {
			System.out.println("Num11 == num12 equal");
		}
		// The correct way to write
		if(Math.abs(num11 - num12) < 0.000001 ) {
			System.out.println("The difference is so small, to my accuracy, that it is considered equal...");
		}
		// Through the Java API
		System.out.println(Math.abs(num11 - num12));
		// Details: if it is a decimal directly queried or directly assigned, it can be judged as equal}}Copy the code

3. Char

3.1 Basic Introduction

  • A character type can represent a single character. The character type ischar.charIs two bytes (can store Chinese characters), multiple characters we use stringsString

3.2 Case Demonstration

	char c1 = 'a';
	char c2 = '\t';
	char c3 = 'xi';
	char c4 = 97; // Note: The character type can store a number directly

	System.out.println(c1);
	System.out.println(c2);
	System.out.println(c3);
	System.out.println(c4);// when c4 is output, 97 is output => the concept of encoding
Copy the code

3.3 Character Type usage details

  • Unicode transformation: tool.chinaz.com/Tools/Unico…
  • A is 97



	// In Java, a char is essentially an integer that, when output by default, corresponds to a Unicode code
	// To print the corresponding number, the (int) character can be used
	char c1 = 97;
	System.out.println(c1); // a

	char c2 = 'a'; // Print the corresponding number of 'a'
	System.out.println((int)c2);/ / 97
	char c3 = 'xi';
	System.out.println((int)c3);/ / 20846
	char c4 = 20846;
	System.out.println(c4);/ / xi

	// The char type is operable and is equivalent to an integer because it has Unicode codes.
	
	System.out.println('a' + 10);/ / 107

	// Class quiz
	char c5 = 'b' + 1;/ / 98 + 1 = = > 99
	System.out.println((int)c5); / / 99
	System.out.println(c5); //99-> corresponding characters -> encoding table ASCII(specified)=> C
Copy the code

3.4 Exploring the nature of character types

3.5 ASCII Code Introduction

3.6 Introduction to Unicode Encoding

3.7 UTF-8 Encoding (Understanding)

4. Boolean type (Boolean)

	// show a case where the grade is passed or not
	// Define a Boolean variable
	boolean isPass = true;
	if(isPass == true) {
		System.out.println("You've passed the exam. Congratulations.");
	} else {
		System.out.println("Failed the exam, try harder next time.");
	}
Copy the code