Java native type and wrapper type depth analysis __java

Source: Internet
Author: User
Tags wrapper

Java native type and wrapper type depth analysis

Summary:

This paper makes a deep analysis of Java native type and wrapper type, which mainly involves the following four aspects: primitive type and wrapper type Foundation, the concept and type of literal value, the automatic transformation and compulsory transformation of basic types, and the mechanism of automatic boxing and unboxing.

Points:

Native type and wrapper type

Concept and kind of literal value

Basic types, automatic transformation and forced transformation

Automatic boxing and unpacking mechanism (autoboxing and unboxing) I. Native type and wrapper type

There are eight basic types of data in Java:

          

Special attention should be paid to the following points:

Integers that do not have the character suffix identification default to type int, and floating-point numbers with no character suffix identification default to double;

Only byte,char,short,int four basic types and their wrapper classes (which require java5.0/1.5 version support) can be used for switch statements (only enum types and int types can be applied to swith), and other types of compilation will complain;

char (two bytes) can be used in single quotes to denote a single character (can be kanji), such as: ' Good ', ' A ';

Integers can be represented by binary (beginning with 0b/0b), octal (integers beginning with 0), decimal, hexadecimal (integers beginning with 0x or 0X); two. Literal value

In Java source code, a literal is used to represent a fixed value. Numeric literals are the most common, and string literals are common as well, but special null can also be used as a literal. Literal values can be broadly divided into integer literals (int literal, long literal, and character literal), integer literal, floating-point literal (double literal and float literal), string literal, and special literal four.

(1) integer literal value

Formally, the value of an integer is categorized as an integer literal. For example: 100000L, ' B ', ' B ', 0XFF These can be called integer literal value. The following points need to be noted when using:

Integer literals can be represented in decimal, hexadecimal (beginning with 0x/0x), octal (beginning with 0), and binary (beginning with 0b/0b). Of course, the cardinality can not exceed the scope of the system, such as 09 is illegal, the base of the octal only to 7.

In general, literal values create an int type, but int literals can be assigned to byte, short, char, long, and Int. Of course, the premise is that the literal is within the target range (Java will automatically complete the conversion), and if an attempt is made to assign a literal to a certain type (such as assigning 128 to a byte type), the compiler does not, however, need to force type conversions.

byte B0 = 128;       Type mismatch:cannot convert from int to byte
byte B1 =;       OK
byte b2 = (byte) 300;//OK requires a cast, but the number indicated is the number after the interception (3,002 to 100101100, after the interception is 00101100, that is)

char c0 = 5;       OK
Char c1 = char ( -3);       The compilation passes, but the print value is. (char is unsigned)
System.out.println (C1);   。
1 2 3 4 5 6 7

If you want to create a long type that cannot be represented by an int type, you need to precede the literal value with L or L (it is usually recommended that you use an easily distinguishable L). Therefore, the integer literal value includes int literal and long literal two kinds.

In Java, character literals are enclosed in single quotes (also of integer literal values), such as ' @ ', ' 1 '. All UTF-16 character sets are included in character literals. Characters that cannot be entered directly, you can use an escape character, such as ' \ n ' for a newline character. You can also use octal or hexadecimal notation for characters, and octal uses backslashes with 3-digit representations, such as ' \141 ' for the letter A. Hexadecimal uses \u plus 4 for the number of 16, such as ' \u0061 ' for character A. That is, by using an escape character, you can represent all the characters that are or are not on the keyboard. The common escape character sequences are:
\DDD (octal), \uxxxx (hexadecimal Unicode character), \ ' (single quotation mark), \ "(double quotation mark), \ (backslash) \ r (return) \ n (line break) \f (page break) \ t (tab) \b (Back to Geff)

(2) floating-point literal value

The simple understanding of floating-point literals can be interpreted as decimals, which are divided into float literals and double literals. If you add F or F to the decimal, it means that it is a float literal, such as 11.8F. If a decimal is not followed by a f/f (such as 10.4), or after a decimal number plus a d/d, this is a double literal.

Double D1 = ten;     /OK, automatic type conversion
double d2 = 11.4;    OK
Double d3 = 1.23E3;    OK  
Double d4 = 10D;    OK  
double d5 = 0.4D;     OK 
Float f1 = ten;      OK, automatic type conversion
float F2 = 11.1F;      OK
float f3 = 11.1;      Type Mismatch:cannot convert from double to float
1 2 3 4 5 6 7 8

(3) Character and string literal value

In Java, character literals are enclosed in single quotes, such as ' @ ', ' 1 '. All UTF-16 character sets are included in character literals. Characters that cannot be entered directly, you can use an escape character, such as ' \ n ' for a newline character. You can also use octal or hexadecimal notation for characters, and octal uses backslashes with 3-digit representations, such as ' \141 ' for the letter A. Hexadecimal uses \u plus 4 for the number of 16, such as ' \u0061 ' for character A. That is, by using an escape character, you can represent all the characters that are or are not on the keyboard. The common escape character sequences are:
\DDD (octal), \uxxxx (hexadecimal Unicode character), \ ' (single quotation mark), \ "(double quotation mark), \ (backslash) \ r (return) \ n (line break) \f (page break) \ t (tab) \b (Back to Geff)

String literals use double quotes, and string literals can also contain the escape character sequences in character literal values.

(4) Special literal value

Null is a special type that can be assigned to any reference type variable, indicating that the variable does not reference anything. If a reference type variable is NULL, it means that the variable is not available.

There is also a special class literal, with type name Plus. class notation, such as String.class. First, string is an instance (object) of Class class (Java.lang.Class), and "This is a string" is an object of class string. Class literal is then used to represent an object of class classes, such as the object string that String.class uses to represent class classes. Simply put, the literal literal value (class literal) is such as String.class, Integer.class, which represents the class string, the class Integer. If the output integer.class, you will get class Java.lang.Integer. The output of List.class is interface java.util.List. In short, class literal is used to represent the type itself.

Note In particular: You can use underscores in numeric literal values (JDK7 start, you can insert one or more underscores in numeric literals, including integer and floating-point literals). However, underscores can only be used to separate numeric underscores only for separating digits, not separating characters from characters, or separating characters from numbers.

int x = 123_456_789;  //The underline is automatically removed at compile time.
float f = 1.22___33__44;   You can use the underscore
int = _123 continuously;  Error
long = 1_l;  Error
1 2 3 4 three. Automatic transition and forced transformation

1. Automatic transformation

General principle of automatic transformation: Byte,short,char (sibling)-> int-> long-> float-> double (from low precision to high precision)

(1) automatic conversion from low precision to high precision

This can be divided into the following two scenarios: type conversions from a lower-bit type to a high number of digits

    byte B = 1;
    char c = 1;
    Short s = 1;
    int i = 1;

    c = b;  Error, sibling
    C = s;  Error, sibling
    s = C;  Error, sibling
    i = C;  Ok
1 2 3 4 5 6 7 8 9

converting from integral to floating-point ———-

converting from integral to floating-point type

    int i = 1;
    long T = 1;
    float f = 1;
    Double d = 1;

    f = i;  Ok
    f = t;  Ok
    d = f;  Ok
1 2 3 4 5 6 7 8 9

(2) The effect of the operator on the basic type

The details can be divided into the following two scenarios:

1 when using the + 、-、 *,/,%, = =, >, <, etc. operators to operate on the base type, follow the following rules:

Two operands, first consider whether one is of type double. If so, the other operand and the result are converted to the double type. Then consider Float,long in turn. In addition, two operands (including Byte, short, int, char) will be converted to the INT type.

BYTE B1 = ten;  OK, check to see that 10 does not exceed the byte-type maximum
byte b2 =;   OK, check to see that 12 does not exceed the byte-type maximum

byte B = b1 + b2; The//error,byte type is automatically promoted to the int type when it is evaluated, because B1+B2 is actually an int type, But the variable on the left is of type byte. Short

s1=1//ok, which will check that 1 does not exceed the maximum value of the short type
s1=s1+1;    Error, because the s1+1 result int, but the left variable is short, error

s1++;      OK, no error, and s1=s1+1 different ... , it is found that 2 does not exceed the maximum value of the short type

s1=1+1;   Ok,1+1 is a constant that can be determined at compile time, the ' + ' operation is executed at compile time, rather than when the program executes, the effect of the statement is equal to the s1=2
1 2 3 4 5 6 7 8 9 10 11

2 when using the + =,-=, *=,/=,%=, i++, and ++i operators to perform operations on the base type, follow the following rules:

The value on the right side of the operator is first cast to the same type as the value on the left of the operator, and then the operation is performed with the same result as the value on the left of the operator. The self increasing (subtraction) operation is similar.

Short s1=1; OK, check to see that 1 does not exceed the short type of maximum short
S2;

S1+=1;    OK, correct, 1 first cast to the short type, then participate in the operation, and the result is also short type

s2 = ++s1;     OK, correct, S2 value is 2

    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: info-contact@alibabacloud.com 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.