Java Automatic Unboxing Summary

Source: Internet
Author: User

There are 8 basic data types in Java, these eight basic data types have corresponding package types, the following is the corresponding relationship:

Int--integerfloat--floatdouble--doublebyte--bytelong--longchar--characterboolean--booleanshort--short

The basic data type and its corresponding package types can be converted to each other, from the basic data type to the package type is called boxing, from the package type to the basic data type called unboxing, automatic unboxing is a new feature provided by jdk5.0, it can automatically implement the type of conversion, the code is as follows:

publicclass TestDemo {    publicstaticvoidmain(String[] args) {        Integer m =10;        int i=m;    }}

The above code in jdk1.4 after the version will not error, it realizes the function of the automatic disassembly box, if it is jdk1.4, you have to write:

publicclass TestDemo {    publicstaticvoidmain(String[] args) {        new Integer(210);        int c = b.intValue();    }}

jdk1.4 the programmer to implement the type conversion, it is very cumbersome to do, increase the amount of code, so the subsequent version can automatically implement the function of the Disassembly box. Let's look at one more example:

public   Class  Testdemo {public  static  void  main  (string[] args) {do1 (10 ); } public  static   void  do1  (double  a) {System.out . println (" double "); } public  static   void  do1  (Integer b) {system.. println ( "Integer" ); }}

The above code overload the Do1 function, the parameter type is different, call the time to pass in a shaping parameter, we think about the jdk5.0 and jdk1.4 in the program will call which method, we take a look, first see 1.4,1.4 can not automatically disassembly box, it will transform the shaping into a double type, Then call the method of the double class, this is the result of jdk1.4, then 5.0, 5.0 can implement the automatic disassembly box, it is not to call the method of the parameter is integer? The answer is a mistake, and it still calls the method of the parameter double type, which is why? This is because the JDK has a feature, its backward compatibility, the new version of the JDK will be compatible with the old version of the JDK, so the execution results and 1.4, here can not be used to consider the unboxing, you have to consider a large principle.
The above is a simple summary of the disassembly box.

Java Automatic Unboxing Summary

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.