Java data type division (numerical type, boolean type, character type)

1. Java data type classification

Data types in Java are generally divided into two categories:

Basic data type : some specific digital units described, such as 1, 1.1, etc.;

(1) Numerical type:

》》Integer: byte, short, int, long; default value→0

》》Floating point type: float, double; default value→0.0

(2) Boolean: boolean; default value → false

(3) Character type: char; default value→'\u0000'

" Reference data type : involves the use of memory;

Array, class, interface; default value→null

Therefore, different data types store different ranges of data, so the choice of data type is involved here. Here are some principles of misuse:

》The first choice for describing numbers is int (integer) and double (floating point);

》If you want to perform data transmission or text encoding conversion, use byte type (binary processing operation);

》When dealing with Chinese, it is most convenient to use char to complete

》Long can be used to describe the file size and the primary key column of the description table (automatic growth);

 2. Integer data type

int occupies 32 bits, 4 bytes, common definitions such as: int x=10;//A constant 10 is assigned to the x integer variable

We know that any data type has a range of data that can be saved (under normal use, there will rarely be data beyond this range), so what if it is really exceeded?

Here we write the following code

public class Hello{
	public static void main(String args[]){
	int max=Integer.MAX_VALUE;//获取int型变量的最大值
	int min=Integer.MIN_VALUE;//获取int型变量的最小值
	System.out.println(max);
	System.out.println(min);
	System.out.println(max+1);
	System.out.println(max-1);
}
}

operation result:

Here, the maximum value +1 becomes the minimum value, and the maximum value -1 is normal.

Students who have studied the principles of computer composition should all know why this is.

At this time, when our number is processing, if it exceeds the maximum or minimum range, there will be a loop problem (the maximum value + 1 becomes the minimum value). This problem is called data overflow in Java .

How to solve this problem? The reason why data overflow occurs is because the data range of the data type we choose is too small, so there are generally two ways to deal with it:

"Use a data type with a larger data range. For example, in the above question, we can change the data type of the variable max to a long type, so that we can get the correct result;

》Use forced conversion to convert a small range of data types to a large range of data types.

When performing shaping, there is also a byte type that needs attention. Its data range is -128~127, and the data value is very small.

Normally, in a Java program, the number 20 should be of type int, but when 20 is assigned to a variable of type byte, there is no forced type conversion because it is of type int. This is because Java has done special for our byte Processing, that is, if the variable does not exceed the value range of byte, it can automatically become byte.

3. Floating point data

Floating-point data describes decimals, and any decimal constant in Java has a corresponding type of double, so it is recommended to use the double type when describing decimals. 

If we define a float x=10.2; then

Therefore, it is recommended that everyone use the double type;

If we have the following code

public class Hello{
	public static void main(String args[]){
	float x=10.2F;//将double类型转换为float类型
	System.out.println(x*10.2);
}
}

The result we get is

The real result should be 104.04, so there will be some errors when using float.

4. Character type

The character type is defined using char. In Java, using '' is defined as a character.

Define a character variable as follows:

public class Hello{
	public static void main(String args[]){
	char c='a';//定义一个字符变量
	System.out.println(c);
}
}

Note: In any language, characters can be converted to int. At this time, the content described in the character can be obtained through int to obtain the system code corresponding to its content.

For example, the following code:

public class Hello{
	public static void main(String args[]){
	char c='A';//定义一个字符变量
	int num=c;//将字符A对应的编码给了num
	System.out.println(c);
	System.out.println(num);
}
}

The execution results are as follows

So we know:

The code of'A' is 65, the code of'Z' is 90, the code of'a' is 97, and the code of'z' is 122.

So we can convert uppercase letters to lowercase letters, the code is as follows:

public class Hello{
	public static void main(String args[]){
	char c='A';//定义一个字符变量
	int num=c;//将字符A对应的编码给了num
	System.out.println(c);
	System.out.println((char)(num+32));//大写字母的编码+32即为小写字母的编码
}
}

At the same time, the char type in Java can also save Chinese data, and has its corresponding encoding, as follows:

public class Hello{
	public static void main(String args[]){
	char c='你';//定义一个字符变量
	int num=c;
	System.out.println(c);
	System.out.println(num);
}
}

The reason why you can use char to save Chinese data in Java is because Java uses unicode, a hexadecimal code, which is characterized by including arbitrary text content.

5. Boolean type

Boolean is the name of a shift mathematician. Boolean describes a kind of logical processing result. In Java, boolean is used to define Boolean variables. The only values ​​of Boolean type are true and false.

For example, define Boolean data:

public class Hello{
	public static void main(String args[]){
	boolean c=true;
	System.out.println(c);
	}
}

Note: Because some programming languages ​​do not have a Boolean type, they will use 0 to represent false, or use non-zero to represent true. Such logic does not exist in Java.

6.String type

The so-called string data type is not provided in any programming language, but from the actual use point of view, the corresponding description of the string is also provided. In Java, String is used as the definition of a string.

Because the String class is special, it can be used to directly assign a value to the definition of the string like other ordinary variables, and requires the use of double quotation marks to describe the string.

For example, define a string variable of type String:

public class Hello{
	public static void main(String args[]){
	String c="Hello World";//使用""描述
	System.out.println(c);
	}
}

When using string variables, we can use '+' to connect two strings, as follows to connect strings A and B:

public class Hello{
	public static void main(String args[]){
	String A="Hello";//字符串A
	String B="LIMING";//字符串B
	System.out.println(A+B);
	}
}

We can see that a HelloLIMING is output, which is the connection of two strings.

We know that '+' is also an addition operator, so what happens when the string type and the number type are mixed together? Let's look at the following example:

public class Hello{
	public static void main(String args[]){
	double x=10.5;
	int y=79;
	String z="结果:";
	System.out.println(z+x+y);
	}
}

We can see that the result of the operation is: Result: 10.579

Why is this so?

As we know, in Java, data types with a small data range and data types with a large data range are automatically converted to data types with a large data range. Then when the int type and double and String types are operated, all data types will be Converted to the String data type, so the result is the result of concatenating the three strings.

To prevent this general situation from happening, we can add parentheses to increase the priority to get the correct result:

public class Hello{
	public static void main(String args[]){
	double x=10.5;
	int y=79;
	String z="结果:";
	System.out.println(z+(x+y));
	}
}

At this point, the result: 89.5 is the data we want.

Of course, when describing a string, some escape characters can also be used for processing:

For example: TAB (\t), "(\"),'(\'), line feed (\n), \(\\) to describe.

public class Hello{
	public static void main(String args[]){
	String z="你真\n可爱\\我好\"喜欢你";
	System.out.println(z);
	}
}

We can see that the escape character is displayed.

Guess you like

Origin blog.csdn.net/qq_25036849/article/details/108741692