Summary of Basic java data types and summary of Data Types

Source: Internet
Author: User
Tags float double

Summary of Basic java data types and summary of Data Types

Java data types can be divided into two categories: primitive types and reference types ). The following is the basic Java data type-related knowledge compiled by the java head. If you are interested, learn it together!

1. Data Type:

In Java source code, each variable must declare a type ). Java data types can be divided into two categories: primitive types and reference types ). Primitive types includes the boolean and numeric types (numeric types ). Numeric types can be further divided into integer types and floating-point types ). There are five types of integers: byte short int long char (char is essentially a special int ). Float and double Floating Point types are supported. Link arrangement:

2. Basic types:

Java provides language-level support for basic types, that is, it is pre-defined in Java and expressed with the corresponding reserved keywords. The basic type is a single value, not a complex object. The basic type is not object-oriented and mainly considers efficiency. However, it also provides the object version of the basic type, wrapper ). You can directly use these basic types, or use the basic types to construct arrays or other custom types. The basic type has a clear value range and mathematical behavior.

Integer 2.1

Integer types include byte short int long char, which are represented by 8, 16, 32, 64, and 16bits, respectively. In some cases, char may not be included in the integer category, but the char type is essentially a subset of the int type. The width of an integer should not be regarded as the memory space occupied by the integer, but should be understood as the behavior of a variable or expression defined as an integer. JVM can freely use the memory space they desire, any size, as long as the type of behavior complies with the specifications. Byte short int long is signed and expressed by the 2's complement (two's-complement. While char is represented by 16 bits, It is unsigned and represents the UTF-16 cube.

2.1.1 byte

Byte is represented by an 8-bit byte, which is the smallest integer. It is mainly used to save memory space. The byte type is particularly useful when you operate data streams from networks, files, or other IO types. Value Range: [-128,127]. the default value of byte is (byte) 0. If we try to assign a value out of the value range to the byte type variable, a compilation error occurs, for example, byte B = 128; this statement cannot be compiled. An interesting question, if we have a method: public void test (byte B ). The attempt to call this method is incorrect: test (0); the compiler reports an error and the type is incompatible !!! We remember that byte B = 0; this is completely correct. Why is it wrong here?

Here is a question about the literal value (literal). The literal value is the superficial value. For example, the literal value is 5, 0,-200 in the source code. If L or l is added to the front of an integer, the literal value is of the long type. For example, 1000L represents a value of the long type. If L or l is not added, int type is used. In the basic type, byte short int long can be created by adding an integer literal value without L (we call it an int literal value), for example, byte B = 100; short s = 5; for long type, if the size exceeds the range expressed by int (32 bits), it must end with L. The nominal values of an integer can be expressed in different ways: hexadecimal [0X or 0x], hexadecimal [nothing], and octal [0] hexadecimal [0B or 0b, the binary literal value is only available after JDK 7. In the value assignment operation, the int literal value can be assigned to byte short int long. the Java language automatically handles this process. If the method call time is different, when test (0) is called, the method it can match is test (int), of course it cannot match test (byte) method, it is unknown Why Java does not support method calling as it supports value assignment. Note that the automatic conversion (anto-boxing, auto-unboxing) between the package and the original type is different ). Byte d = 'a'; it is also valid. The character nominal value can be automatically converted to A 16-digit integer.
When a mathematical operation is performed on the byte type, it is automatically upgraded to the int type. If the expression contains double or float types, it is also automatically upgraded. The following code is incorrect:

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

2.1.2 short

The value range is [-2 ^ 15, 2 ^ 15-1]. Short may be the least commonly used type. You can assign values to an integer or character on the premise that the value does not exceed the value range (16 bits ). When the short type is involved in the operation, it is also upgraded to int or higher type. (The sequence is byte short int long float double ).

2.1.3 int

32 bits, [-2 ^ 31, 2 ^ 31-1]. The signed binary complement represents an integer. In common terms, the cycle is controlled. Note that byte and short will be upgraded to the int type or higher in the operation. After Java 8, you can use the int type to represent a 32-bit unsigned integer [0, 2 ^ 31-1].

2.1.4 long

64 bits, [-2 ^ 63, 2 ^ 63-1, default value: 0L]. To calculate a very large number, if int is not 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 '\ u000000' to' \ uff '. Unsigned. The default value is '\ u0000 '. Java uses the Unicode Character Set to represent characters. Unicode is a fully internationalized character set that can represent all characters in human language. Unicode requires a 16-bit Width, so the char type in Java is also represented by 16 bits. The assignment may be like this:

char ch1 = 88;char ch2 = 'A';

The ASCII character set occupies the first 127 Unicode values. The reason why char is classified as an integer is that Java provides Arithmetic Operation Support for char. For example, ch2 ++ can be used, and then ch2 is changed to Y. When char is used for addition, subtraction, multiplication, division, it is also converted to the int type and must be explicitly converted back.

2.2 floating point type

Float with single precision and double with double precision are represented by 32 and 64bits respectively, which follows the IEEE 754 standard.

2.2.1 float

The 32-bit representation corresponds to a single-precision floating point number, which is faster than double and occupies less memory. However, when the value is very large or very small, it becomes inaccurate. The float type can be used when the precision requirement is not high. An example of declaring the value assignment is as follows:

F1 = 10L; f1 = 10.0f; // f1 = 10.0; double </span>

Byte, short, int, long, and char can be assigned to the float type, and java automatically completes the conversion.

2.2.2 double

64 indicates that if f or F is not displayed after the literal value when the floating point face is assigned to a variable, the double type is used by default. All functions in java. lang. Math use the double type.

If neither double nor float can achieve the expected precision, you can use the BigDecimal class.

2.3 boolean Type

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

3. Nominal value

In Java source code, the literal value is used to represent a fixed value (fixed value ). The numeric literal value is the most common. The Literal Value of a string can be regarded as one type. Of course, special null can also be considered as the literal value. The nominal value can be divided into integer, floating point, character, string, and special.

3.1. Integer Literal Value

The literal value of an integer is classified as the literal value of an integer. For example, 10,100 000L, 'B', and 0XFF can all be called the literal value. The literal value of an integer can be expressed in decimal, 16, 8, or 2. The decimal format is very simple. In decimal Format 2, 8, and 16, add 0B (0b), 0, and 0X (0x) at the beginning. Of course, the base number cannot exceed the hexadecimal range, for example, 09 is invalid, and the base of octal is only 7. Generally, the literal value is of the int type, but the int literal value can be assigned to byte short char long int. As long as the literal value is within the target range, Java will automatically complete the conversion, if you try to assign a literal value out of the range to a certain type (for example, assign 128 to the byte type), you cannot compile it. If you want to create a long type that cannot be expressed by the int type, you must add L or l at the end of the literal value. We recommend that you use L, which is easy to distinguish. Therefore, an integer can contain two types of face values: int and long.

3.2. Floating Point Literal Value

A floating point literal can be understood as a decimal number. It can be divided into float and double. If F or f is followed by a decimal number, it indicates a float, such as 11.8F. If F (f) is not followed by a decimal number, for example, 10.4. Or add D (d) to the decimal point to indicate that this is a double literal. In addition, the floating point nominal value supports scientific and technical representation. The following are some examples:

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>

3.3 characters and string literal value

In Java, the character nominal value is enclosed in single quotes, for example, '@ 1 '. All UTF-16 character sets are included in the literal value of the character. Characters that cannot be entered directly. escape characters can be used. For example, '\ n' indicates a newline character. You can also use octal notation or hexadecimal notation, And the octal notation is represented by a backslash plus three digits. For example, '\ 141' indicates the letter. In hexadecimal notation, 4 is the hexadecimal number plus \ u. For example, '\ u0061' indicates character. That is to say, you can use escape characters to indicate all or none of the characters on the keyboard. Common escape character sequences include:

\ Ddd (octal character), \ uxxxx (hexadecimal Unicode character), \ '(single quotes), \ "(double quotation marks), \ (backslash) \ r (carriage return character) \ n (line feed) \ f (page feed) \ t (Tab) \ B (return)

Double quotation marks are used for the string literal value. The string literal value can also contain the Escape Character Sequence in the literal value. The string must be in the same row or use the + operator, Because java does not have a continuation escape sequence.

3.4 Special Literal Value

Null is a special type that can be assigned to any referenced type variable, indicating that the variable does not reference anything. If a reference type variable is null, the variable is unavailable.

There is also a special class literal, which is represented by type name plus. class, such as String. class. First, String is an instance (object) of Class (java. lang. Class), and "This is a string" is an object of the String Class. Then, class literal is used to represent an object of the Class class. For example, String. Class is used to represent the object String of the class Class. To put it simply, class literal is the literal value such as String. class and Integer. class. It represents tired String and class Integer. If Integer. class is output, you will get class java. lang. Integer. The output of List. class is interface java. util. List. In short, class literal is used to indicate the type itself!

3.5 use an underscore in the numeric literal value.

Starting with JDK 7, you can insert one or more underscores (_) in the numeric literal value (including the integer and floating-point literal values. However, underlines can only be used to separate numbers. They cannot be used to separate characters or numbers. For example, int x = 123_456_789. The underline is automatically removed during compilation. Underline can be used continuously, such as float f = 1.22 ___ 3336644. the binary or hexadecimal literal value can also be underlined. Remember that the underline can only be used between numbers. It is invalid for the first time. For example, 1. _ 23 is invalid, and _ 123 and 11000_L are invalid.

4. Conversion Between Basic Types

As we can see, it is very common to assign a value of one type to another type. In Java, the boolean Type and all other seven types cannot be converted, which is clear. For other numeric types in 7, they can all be converted, but there may be loss of precision or other changes. Conversion can be divided into automatic conversion and forced conversion. For automatic (implicit) conversion, no operation is required, while for forced type conversion, explicit conversion is required, that is, the conversion operator (type) is used ). First, sort the seven types in the following order:

byte <(short=char)< int < long < float < double

If the conversion is from small to large, it can be completed automatically, but from large to small, it must be forcibly converted. Both short and char must be forcibly converted.

4.1 automatic conversion

Widening conversion occurs during automatic conversion ). Because larger types (such as int) need to save smaller types (such as byte), the memory is always sufficient and no forced conversion is required. If the literal value is saved to byte, short, char, and long, the type conversion is also automatically performed. Note the difference. In this case, the process from int (int is not an integer with L) to byte/short/char is also automatically completed, although they are smaller than int. In automatic type conversion, except for the following situations that may cause loss of precision, other conversions cannot cause loss of precision.

》int--> float》long--> float》long--> double》float -->double without strictfp

Except for possible loss of precision, automatic conversion does not cause any running-time exceptions.

4.2 forced type conversion

If you want to convert a large string to a small string, or convert the string between short and char, you must perform a forced conversion, also known as narrowing conversion ), because the value must be explicitly smaller to adapt to the target type. Forced conversion uses the conversion operator (). Strictly speaking, converting byte to char does not belong to narrowing conversion), because the process from byte to char is actually byte --> int --> char, so widening and narrowing both have. In addition to possible loss of precision, forced conversion may also change the modulus (overall magnitude. The forced conversion format is as follows:

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

If the integer value exceeds the range expressed by byte, The result returns the remainder of the byte range. For example, if a = 256 is out of the [-128,127] range of byte, divide 257 by the byte range (256) and obtain the remainder (B = 1). Note that, when a = 200, the remainder value except 256 should be-56, rather than 200.

Truncation occurs when the floating point type is assigned to the integer type ). That is, remove the decimal part and leave only the integer part. If the integer is out of the range of the target type, the remainder of the range of the target type is also obtained.

The conversion of the seven basic types is summarized as follows:

4.3 type conversion in values and expressions:

4.3.1 Literal Value assignment

When you use the literal value to assign an integer value, you can assign the int literal value to the byte short char int as long as it does not exceed the range. The type conversion is completed automatically during this process. However, if you try to assign long literal to a byte, you must perform a forced type conversion even if it is not out of the range. For example, if byte B = 10L; is incorrect, force conversion is required.

4.3.2 automatic type escalation in expressions

In addition to value assignment, some types may also be converted during expression calculation. In expressions, the type escalation rules are as follows:

All byte, short, and char are upgraded to int.

If one operand is long, the entire expression is promoted to long. The same is true for float and double.

The above is the basic Java data type that I will introduce to you. I hope it will help you. If you have any questions, please leave a message and I will reply to you in time, or follow my public account.

I have a public account that will often share some Java-related things. If you like it, you can search for "Java headers" or "javatuanzhang.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.