java basic -03 basic data types

There are 8 basic data types in Java to store numeric values, characters and boolean values, namely byte , short , int , long , float , double , char , boolean .

Let's introduce each basic data type (the concept really doesn't want to learn TAT, the review now is the punishment for being lazy before~~)

The first is the integer types (byte, short, int, long) and floating-point types (float, double) that store values

1. Integer type

The integer type is used to store integer values, that is, the value without a decimal point. It can be positive or negative. Integer data can be divided into four types: byte, short, int, and long according to the size of the memory occupied:

1)byte

The memory space occupied is 8 bits (8 bits equals 1 byte), so the value range of this type is -2^7~2^7-1, that is, the value range is -128~127.

(Note 1: Regarding why it is the 7th power of 2 instead of 3 or other 7th powers, it is because 1 bit (bite) has only the binary representation of 1 and 0, which can be understood as a door that is either open or closed , There are only two cases. Therefore, the memory occupied by 8 bits means that each of the 8 non-opening and closing doors, the following types are the same)

(Note 2: Regarding why it is 2 to the 7th power instead of 2 to the 8th power, it is because in the signed case, the highest bit is the sign bit. Therefore, there are only 7 bits used for actual counting. The following types are the same )

2)short

The memory space occupied is 16 bits or two bytes, so the value range of this type is -2^15~2^15-1, that is, the value range is -32768~32767.

3)int

The memory space occupied is 32 bits or 4 bytes, so the value range of this type is -2^31~2^31-1, that is, the value range is -2147483648~2147483647

4)long

The memory space occupied is 64 bits or 8 bytes, so the value range of this type is -2^63~2^63-1, that is, the value range is -9223372036854775808~9223372036854775807

Therefore, when assigning values, you need to pay attention to the value range of the variable. If the value exceeds the corresponding range, an error will occur.

public class Number {

	public static void main(String[] args) {
		byte inbyte=127;//赋值byte取值范围内正常
//		byte outbyte=128;//超出byte取值范围报错
		
		short inshort=32767;//赋值short取值范围内正常
//		short outshort=32768;//超出short取值范围报错
		
		int inint=2147483647;//赋值int取值范围内正常
//		int outint=2147483648;//超出int取值范围报错
		
		/*
		 * 对于long整型,若赋的值大于int型的最大值或小于int型最小值,则需要在数字后加L或l
		 * */
		long inlong=9223372036854775807l;//赋值long取值范围内正常
//		long outlong=9223372036854775808l;//超出long取值范围报错

	}

}

 

2. Floating point type 

What is floating point? Checked Baidu (Baidu Dafahao). The explanation is that relative to fixed-point numbers, floating-point numbers use exponents to make the position of the decimal point float up and down as needed, so that a larger range of real numbers can be flexibly expressed.

Here I simply understand it as a number with a decimal part.

Floating-point types can also be divided into two types: single-precision floating-point type (float) and double-precision floating-point type (double)

1)float

The memory space occupied is 32 bits or 4 bytes, and the range of single-precision floating-point numbers that can be represented is 1.4E-45~3.4028235E38

2)double

The memory space occupied is 64 bits or 8 bytes, and the range of multi-precision floating point numbers that can be represented is 4.9E-324~1.7976931348623157E308

Since decimals are regarded as multi-precision floating-point numbers double by default, when using double-type decimals, they can be represented by the suffix d or D, or without the suffix, but when using float-type decimals, you must add f Or F. Otherwise, when the variable is defined, the decimal will be considered as a double type and an error will be reported.

 

3. Character type

The character type (char) is used to store a single character and occupies 16 bits of memory, that is, 2 bytes. It should be noted that when defining a character, it should be expressed with single quotation marks. If double quotation marks are used, the definition is a string.

public class Char {

	public static void main(String[] args) {
		char a='s';//单引号表示定义字符,可正常定义
//		char a="t";//双引号表示定义字符串,这里报错了
		String r="u";//字符串要用String正常定义

	}

}

4. Boolean type

Boolean (boolean) type is also called logical type, only true and false, usually used as a judgment condition

For example, the following example

boolean t=true;
boolean f=false;

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/dream_18/article/details/115053547