Home > Java > javaTutorial > body text

Detailed explanation of Java basic data types graphic code (power node arrangement)

黄舟
Release: 2017-04-01 10:27:06
Original
2074 people have browsed it

JavaData types (type) can be divided into two major categories: primitive types (primitive types) and Reference types (reference types). The following is a power node to organize knowledge about Java's basic data types. Friends who are interested can learn together

1. Data type:

In Java source code, each variable must declare a type (type). Java data types (types) can be divided into two major categories: primitive types and reference types. Primitive types include boolean types and numeric types (numeric types). Numeric types are further divided into integer types and floating-point types. There are 5 types of integers: byte short int long char (char is essentially a special type of int). Floating point typesThere are float and double. The relationship is organized as follows:

2. Basic type:

Java is basic Types provide language-level support, that is, they are already predefined in Java and are represented by corresponding reserved keywords. The basic type is a single value, not a complex object. The basic type is not object-oriented. It is mainly out of efficiency considerations, but it also provides object versions of basic types, that is, basic Type wrapper. You can use these basic types directly, or you can use basic types to construct arrays or other custom types. Primitive types have clear value ranges and mathematicalbehavior.

2.1 Integer type

The integer type includes byte short int long char, which are represented by 8, 16, 32, 64, and 16 bits respectively. In some places, char may not be included in the integer category, but essentially the char type is a subset of int. The width of an integer should not be regarded as the size of the memory space occupied by the integer, but should be understood as the behavior of a variable or expression defined as an integer. JVMs are free to use any amount of memory they wish, as long as the type behaves according to the specification. byte short int long are all signed, represented by 2's complement (two's-complement). Char is represented by 16 bits, which is unsigned and represents the UTF-16 encoding set.

2.1.1 byte

byte is represented by 1 byte of 8 bits and is the smallest integer type. Mainly used to save key memory space. The byte type is particularly useful when operating on data streams from the network, files, or other IO. The value range is: [-128, 127]. The default value of byte is (byte)0. If we try to assign a value outside the value range to a byte type variable, a compilation error will occur, such as byte b = 128; This statement cannot be compiled. An interesting question, if we have a method: public void test(byte b). It is wrong to try to call this method like this: test(0); The compiler will report an error, type incompatibility! ! ! We remember byte b =0; this is no problem at all, why does it go wrong here?

This involves a problem called literal value. Literal value is the value on the surface. For example, integer literal value in the source code is such as 5, 0, -200. If L or l is added to the end of the integer, the literal value is of type long. For example: 1000L represents a value of type long. If L or l is not added, it is of type int. Among the basic types, byte short int long can be created by integer literals without adding L (let’s call them int literals), for example, byte b = 100; short s = 5; for the long type, if the size exceeds The range that int can represent (32 bits) must be expressed with an L ending. Integer literals can be represented in different ways: hexadecimal [0X or 0x], decimal [nothing], octal [0] binary [0B or 0b], etc. Binary literals are only available after JDK 7. function. In the assignment operation, int literal value can be assigned to byte short int long, and the Java language will automatically handle this process. If the method calls are different, when test(0) is called, the method it can match is test(int), and of course it cannot match the test(byte) method. As for why Java does not support method calls like it supports assignment operations, it has no choice. Know. Note the difference between wrappers and automatic conversion of primitive types (anto-boxing, auto-unboxing). byte d = 'A'; is also legal, character literals can be automatically converted into 16-bit integers.
When performing mathematical operations on the byte type, it will be automatically promoted to the int type. If there are types such as double or float in the expression, it will also be automatically promoted. So the following code is wrong:

byte s2 = 'a'; 
 byte sum = s1 + s2;//should cast by (byte)</span></span></span>
Copy after login

2.1.2 short

is represented by 16, and the value range is: [- 2^15, 2^15 - 1]. short is probably the least commonly used type. Values ​​can be assigned through integer literals or character literals, provided they do not exceed the range (16 bits). When the short type participates in operations, it is also promoted to int or a higher type. (The order is byte short int long float double).

2.1.3 int

32 bits, [- 2^31, 2^31 - 1 ]. Integer represented by signed two's complement . Commonly used expressions controlLoop, note that byte and short will be promoted to int type or higher during operations. After Java 8, you can use the int type to represent unsigned 32-bit integers [0, 2^31 - 1].

2.1.4 long

64 bits, [- 2^63, 2^63 - 1, the default value is 0L]. When you need to calculate very large numbers, if int is not large enough to accommodate the size, you can use the long type. If long is not enough, you can use the BigInteger class.

2.1.5 char

16 bits, [0, 65535], [0, 2^16 -1], from '\ u0000' to '\uffff'. Unsigned, default value is '\u0000'. Java uses the Unicode character set to represent characters. Unicode is a completely international character set that can represent characters in all human languages. Unicode requires 16 bits wide, so the char type in Java is also represented by 16 bits. The assignment might look like this:

char ch1 = 88;
char ch2 = &#39;A&#39;;
Copy after login

The ASCII character set occupies the first 127 values ​​of Unicode. The reason why char is classified as an integer type is because Java provides arithmetic operation support for char, for example, ch2++; then ch2 becomes Y. When char performs addition, subtraction, multiplication and division operations, it is also converted to int type and must be converted back explicitly.

2.2 Floating point type

Includes single-precision float and double-precision double, expressed in 32 and 64 bits respectively, following the IEEE 754 specification.

2.2.1 float

uses 32 bits to represent single-precision floating point numbers. It runs faster than double and occupies less memory. However, when the value is very It becomes inaccurate when it is large or very small. When the precision requirements are not high, you can use the float type. Declaration assignment example:

f1 = 10L; 
 f1 = 10.0f; 
 //f1 = 10.0;默认为double</span></span></span>
Copy after login

You can assign byte, short, int, long, and char to the float type, and java automatically completes the conversion.

2.2.2 double

64 means that when assigning a floating point value to a variable, if f or F is not displayed after the literal value, then The default is double type. The functions in java.lang.Math all adopt double type.

If double and float cannot achieve the desired precision, you can use the BigDecimal class.

2.3 boolean type

The boolean type has only two values, true and false, and the default is false. boolean has nothing to do with whether it is 0 or not, but can be converted according to the desired logic. The boolean type is needed in many places.

3. Literal value

In Java source code, literal value is used to represent a fixed value. Numeric literal values ​​are the most common. String literal values ​​can be regarded as one type. Of course, the special null can also be regarded as a literal value. Literal values ​​can be roughly divided into integer literals, floating-point literals, character and string literals, and special literals.

3.1. 整型字面值

从形式上看是整数的字面值归类为整型字面值。例如: 10, 100000L, 'B'、0XFF这些都可以称为字面值。整型字面值可以用十进制、16、8、2进制来表示。十进制很简单,2、8、16进制的表示分别在最前面加上0B(0b)、0、0X(0x)即可,当然基数不能超出进制的范围,比如09是不合法的,八进制的基数只能到7。一般情况下,字面值创建的是int类型,但是int字面值可以赋值给byte short char long int,只要字面值在目标范围以内,Java会自动完成转换,如果试图将超出范围的字面值赋给某一类型(比如把128赋给byte类型),编译通不过。而如果想创建一个int类型无法表示的long类型,则需要在字面值最后面加上L或者l。通常建议使用容易区分的L。所以整型字面值包括int字面值和long字面值两种。

3.2. 浮点字面值

浮点字面值简单的理解可以理解为小数。分为float字面值和double字面值,如果在小数后面加上F或者f,则表示这是个float字面值,如11.8F。如果小数后面不加F(f),如10.4。或者小数后面加上D(d),则表示这是个double字面值。另外,浮点字面值支持科学技术法表示。下面是一些例子:

double d2 = 11.4; 
 double d3 = 1.23E3; 
 double d4 = 10D; 
 double d5 = 0.4D; 
 float f1 = 10; 
 float f2 = 11.1F; 
 float f3 = 1.23e-4F; 
 float f4 = 1.23E0F;</span>
Copy after login

3.3 字符及字符串字面值

Java中字符字面值用单引号括起来,如‘@'‘1'。所有的UTF-16字符集都包含在字符字面值中。不能直接输入的字符,可以使用转义字符,如‘\n'为换行字符。也可以使用八进制或者十六进制表示字符,八进制使用反斜杠加3位数字表示,例如'\141'表示字母a。十六进制使用\u加上4为十六进制的数表示,如'\u0061'表示字符a。也就是说,通过使用转义字符,可以表示键盘上的有的或者没有的所有字符。常见的转义字符序列有:

\ddd(八进制) 、 \uxxxx(十六进制Unicode字符)、\'(单引号)、\"(双引号)、\\ (反斜杠)\r(回车符) \n(换行符) \f(换页符) \t(制表符) \b(回格符)

字符串字面值则使用双引号,字符串字面值中同样可以包含字符字面值中的转义字符序列。字符串必须位于同一行或者使用+运算符,因为java没有续行转义序列。

3.4 特殊字面值

null是一种特殊的类型(type),可以将它赋给任何引用类型变量,表示这个变量不引用任何东西。如果一个引用类型变量为null,表示这个变量不可用。

还有一种特殊的class literal,用type name加上.class表示,例如String.class。首先,String是类Class(java.lang.Class)的一个实例(对象),而"This is a string"是类String的一个对象。然后,class literal用于表示类Class的一个对象,比如String.class用于表示类Class的对象String。简单地说,类子面子(class literal)就是诸如String.class 、Integer.class这样的字面值,它所表示的就是累String、类Integer。如果输出Integer.class,你会得到class java.lang.Integer。List.class的输出为interface java.util.List。总之,class literal用于表示类型本身!

3.5 在数值型字面值中使用下划线。

JDK7开始,可以在数值型字面值(包括整型字面值和浮点字面值)插入一个或者多个下划线。但是下划线只能用于分隔数字,不能分隔字符与字符,也不能分隔字符与数字。例如 int x = 123_456_789.在编译的时候,下划线会自动去掉。可以连续使用下划线,比如float f = 1.22_3344.二进制或者十六进制的字面值也可以使用下划线,记住一点,下划线只能用于数字与数字之间,初次以外都是非法的。例如1._23是非法的,_123、11000_L都是非法的。

4. 基本类型之间的转换

我们看到,将一种类型的值赋给另一种类型是很常见的。在Java中,boolean类型与所有其他7种类型都不能进行转换,这一点很明确。对于其他7中数值类型,它们之间都可以进行转换,但是可能会存在精度损失或者其他一些变化。转换分为自动转换和强制转换。对于自动转换(隐式),无需任何操作,而强制类型转换需要显式转换,即使用转换操作符(type)。首先将7种类型按下面顺序排列一下:

byte <(short=char)< int < long < float < double
Copy after login

如果从小转换到大,可以自动完成,而从大到小,必须强制转换。short和char两种相同类型也必须强制转换。

4.1 自动转换

自动转换时发生扩宽(widening conversion)。因为较大的类型(如int)要保存较小的类型(如byte),内存总是足够的,不需要强制转换。如果将字面值保存到byte、short、char、long的时候,也会自动进行类型转换。注意区别,此时从int(没有带L的整型字面值为int)到byte/short/char也是自动完成的,虽然它们都比int小。在自动类型转化中,除了以下几种情况可能会导致精度损失以外,其他的转换都不能出现精度损失。

》int--> float
》long--> float
》long--> double
》float -->double without strictfp
Copy after login

除了可能的精度损失外,自动转换不会出现任何运行时(run-time)异常。

4.2 强制类型转换

如果要把大的转成小的,或者在short与char之间进行转换,就必须强制转换,也被称作缩小转换(narrowing conversion),因为必须显式地使数值更小以适应目标类型。强制转换采用转换操作符()。严格地说,将byte转为char不属于narrowing conversion),因为从byte到char的过程其实是byte-->int-->char,所以widening和narrowing都有。强制转换除了可能的精度损失外,还可能使模(overall magnitude)发生变化。强制转换格式如下:

 byte b;  
 b = (byte)a;//1</span>
Copy after login

如果整数的值超出了byte所能表示的范围,结果将对byte类型的范围取余数。例如a=256超出了byte的[-128,127]的范围,所以将257除以byte的范围(256)取余数得到b=1;需要注意的是,当a=200时,此时除了256取余数应该为-56,而不是200.

将浮点类型赋给整数类型的时候,会发生截尾(truncation)。也就是把小数的部分去掉,只留下整数部分。此时如果整数超出目标类型范围,一样将对目标类型的范围取余数。

7种基本类型转换总结如下图: 

4.3 赋值及表达式中的类型转换:

4.3.1 字面值赋值

在使用字面值对整数赋值的过程中,可以将int literal赋值给byte short char int,只要不超出范围。这个过程中的类型转换时自动完成的,但是如果你试图将long literal赋给byte,即使没有超出范围,也必须进行强制类型转换。例如 byte b = 10L;是错的,要进行强制转换。

4.3.2 表达式中的自动类型提升

除了赋值以外,表达式计算过程中也可能发生一些类型转换。在表达式中,类型提升规则如下:

》所有byte/short/char都被提升为int。

》如果有一个操作数为long,整个表达式提升为long。float和double情况也一样。

The above is the detailed content of Detailed explanation of Java basic data types graphic code (power node arrangement). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template