Java Source code Interpretation (a) 8 basic types corresponding to the package type

Source: Internet
Author: User
Tags throw exception

Say the Origin code in fact the first to see should be our parent class object, where it does not describe the people each read it.

One or eight basic types

Next, let's introduce our eight basic types (as everyone knows): Char, Byte, short, int, long, float, double, Boolean. There are not too many things to describe here, just to say something special to pay attention to (if I'm missing, please add):

1, Byte, short, char cannot perform operator operations that contain variables (normal operators), all require a strong turn, because there are scope constraints. However , you can do the + = and self-increment operation.
2, Auto-transformation level: Byte,short,char (sibling), int------float-------double (from low precision to high precision), sibling cannot be automatically transformed, need to cast two or eight types of packages

Let's say something in common:

    • Equals method:
only the same type can be compared, and then the value is compared. The source code takes short for example, the other several are the same.
     Public Boolean equals (Object obj) {        ifinstanceof short ) {            return value = = (short) obj. Shortvalue ();        }         return false ;    }
therefore, regardless of the value of the different types of equality is false;
    @Test    publicvoid  testequals () {        = 1;         = 1;         = 1L;        System.out.println (Num1.equals (num2)); // false        System.out.println (Num2.equals (num3)); // false    }
    • When you assign a value directly, the ValueOf method is called. So be aware of its source code.
different points: 1, Character
    • Note The code in valueof, which caches the ASCII code 0~127 characters. All other instances are created.

2, Byte
    • The range is -128~127, so exceeding this range will require casting. If you create an instance using byte (String s), an exception will be thrown over the range: NumberFormatException
    • Note The code in valueof, which caches the -128~127, so the direct assignment is not to create a new instance.
@Test Public voidTestbyte () {//the range of byte is -128~127, and for byte if assignment is not in range eclipse will require a forced transition to byte. //Package Type byte new byte ("128") when an instance is created with a string, the value above the range will error numberformatexception.         byteB1 = 12; Byte B2= 12;//(byte) 129;//over range requires strong turnByte B3 = 12;//(byte) 129; //byte b4 = new Byte ("n");//Throw ExceptionByte B4 =NewByte ("12");//Throw ExceptionSystem.out.println (B1 = = b2);//trueSystem.out.println (B2 = = B3);//trueSystem.out.println (B2 = = b4);//false}
3. Short
    • Just like the 1th byte, only the range (-32768 ~ 32767) is different.
    • Note the valueof method, which caches the value of -128~127 and creates a new instance of this range ( -128~127).
    • Based on the second case, it is not easy to iterate through the copy operation, otherwise the range will be more than one instance, affecting memory.
@Test Public voidTestshort () {//1, Short range: -32768 ~ 32767Short S =NewShort ("32767");//over range will be error numberformatexceptions =NewShort (( Short) 327671);//Automatic conversion over this range//2, packing and unpacking automatic transformation         ShortS1 = 12; Short S2=NewShort (S1);//Manual BoxingSystem.out.println ("S1 = = S2:" + (S1 = = s2));//Automatic unpacking True//3. The ValueOf method caches the scope of the -128~127, and it is necessary to create this instance in addition to this range. Short S3 = 12; Short S4= 12; Short S5= 128; Short S6= 128; System.out.println ("S3 = = S4:" + (s3 = = S4));//trueSystem.out.println ("S5 = = S6:" + (S5 = = S6));//false//4, because of the above characteristics, so this type of packaging can not be assigned in the loop traversal. Otherwise, if the value exceeds this range, new objects will be created, and many objects will be created if many. Wasted space. }
4, Integer
    • The value to be paid attention to range (-2147483648 ~ 2147483647), does not require a strong turn.
    • Note that the valueof method, like the short method, caches the value of -128~127, and a new instance is created beyond that range ( -128~127).
  • based on the second case, it is not easy to iterate through the copy operation, otherwise the range will be more than one instance, affecting memory.
    @Test Public voidTestinteger () {//1, Integer range: -2147483648 ~ 2147483647//the back is the same as short//2, packing and unpacking automatic transformation        intS1 = 12; Integer S2=NewInteger (S1);//Manual BoxingSystem.out.println ("S1 = = S2:" + (S1 = = s2));//Automatic unpacking True//3. The ValueOf method caches the scope of the -128~127, and it is necessary to create this instance in addition to this range. Integer S3 = 12; Integer S4= 12; Integer S5= 128; Integer S6= 128; System.out.println ("S3 = = S4:" + (s3 = = S4));//trueSystem.out.println ("S5 = = S6:" + (S5 = = S6));//false//4, because of the above characteristics, so this type of packaging can not be assigned in the loop traversal. Otherwise, if the value exceeds this range, new objects will be created, and many objects will be created if many. Wasted space. }
5, Long
  • The value to be paid attention to range (-9223372036854775808 ~ 9223372036854775807), does not require a strong turn.
  • Note that the valueof method, like the short method, caches the value of -128~127, and a new instance is created beyond that range ( -128~127).
  • based on the second case, it is not easy to iterate through the copy operation, otherwise the range will be more than one instance, affecting memory.
    @Test Public voidTestlong () {//the scope is not considered. //Similarly, the ValueOf method caches the scope of the -128~127, and it is necessary to create this instance in addition to this range. Long s3 = 12L; Long S4= 12L; Long S5= 128L; Long S6= 128L; System.out.println ("S3 = = S4:" + (s3 = = S4));//trueSystem.out.println ("S5 = = S6:" + (S5 = = S6));//false//because of this feature, this wrapper type cannot be assigned in a loop traversal. Otherwise, if the value exceeds this range, new objects will be created, and many objects will be created if many. Wasted space. } 
6, Float
    • The value to be paid attention to range ( -1.4e-45~ 3.4028235E38), does not require a strong turn.
    • Note the valueof method, which is different from the previous short, Integer, and long, creates the instance directly.  
    • So the two variable = = operation of the equal value is false.
    • Do not add and subtract operations. Accuracy (9-bit) problem.
@Test Public voidtestfloat () {//There's nothing special to pay attention to.        intF = 1; Float F1=1F; Float F2=NewFloat (f); System.out.println (f= = F1);//trueSystem.out.println (f1 = = F2);//false//be careful not to use this type for addition and subtraction operations, the accuracy problem will be affected. System.out.println (f1-0.1f*9);//0.099999964}
7, Double
    • Pay attention to the range of values, do not require a strong turn.
    • Note the ValueOf method, which, like float, creates the instance directly.
so the two variable = = operation of the equal value is false.
    • Do not add and subtract operations. Accuracy (17-bit) problem.
@Test Public voidtestdouble () {//be careful not to use this type for addition and subtraction operations, the accuracy problem will be affected. System.out. println (1.0-0.1*9);//0.09999999999999998//valueofDouble I1 = 100.0; Double I2= 100.0; Double i3= 200.0; Double I4= 200.0; System.out.println (I1==I2);//falseSystem.out.println (I3==I4);//false}
8, Boolean
    • ValueOf method

Two methods are cached, so if the assignment is the same, the = = operation is true.
    @Test    publicvoid  testequals () {        = 1;         = 1;         = 1L;        System.out.println (Num1.equals (num2)); // false        System.out.println (Num2.equals (num3)); // false    }
Third, case analysis

Guess what the results of each of the following outputs are:

@Test Public voidtest1 () {Integer a= 1; Integer b= 2; Integer C= 3; Integer D= 3; Integer e= 321; Integer F= 321; Long g= 3L; Long h= 2L; System.out.println (c==d); System.out.println (e==f); System.out.println (c= = (A +b)); System.out.println (C.equals (a+b)); System.out.println (g= = (A +b)); System.out.println (G.equals (a+b)); System.out.println (G.equals (a+h)); }

This will involve unpacking and packing (self-understanding) issues. For individual parsing, the following:

c== (a+b): A+b will be disassembled into int and added, so C will also automatically split the box comparison.

g== (A+B): Similarly, a+b will be disassembled into int and added, G will be disassembled into a long type. So the basic type comparison is just a comparison of its values.

G.equals (a+b): First unpacking the box a+b or integer, this will not be automatically transformed. The long equals judgment is not the same type directly returning false

G.equals (a+h): Ibid, first unboxing a+h unboxing (here will automatically upward transformation) is a long, so the same type of comparison values are equal, here returns True.

The final result is as follows:

true false true true true false true

Java Source code Interpretation (a) 8 basic types corresponding to the package type

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.