Packaging classes in Java and their considerations __java

Source: Internet
Author: User

Statement: This blog for the original blog, without permission, may not be reproduced. Small partners if it is in other places to see, suggest or to csdn to see it (the original link address for http://blog.csdn.net/bettarwang/article/details/26166405), after all, look at the code and questions, discussions are more convenient.

We know that in Java, all but 8 basic data types are reference types. Using reference types to better implement object-oriented thinking, why keep 8 of basic data types?

This is actually more of a habit of taking care of programmers. In order to take care of the habit of programmers, but also fully implement the idea of object-oriented programming, Java introduced the packaging mechanism. The so-called packing class is to define the corresponding reference types for the 8 basic data types, and the corresponding relation is as follows:

Table 1 Relationship between 8 basic data types and wrapper classes
Basic data types The corresponding packing class
Byte Byte
Short Short
Int Integer
Long Long
Char Character
Float Float
Double Double
Boolean Boolean
obviously, except for int and Char, the rest of the wrapper class will capitalize the first letter of the corresponding base data type.

So why introduce the packing class. As has been said before, is to fully implement object-oriented programming ideas, specifically, unreferenced types of data in use will have a lot of constraints, such as List List=new ArrayList (), for reference types, can directly use List.add (obj); However, it is not possible to add to the base data type, so many of the methods in ArrayList (such as sorting, deleting, and so on) are not available, and the use of wrapper classes is a good way to avoid this flaw.

At the same time, automatic boxing and automatic unboxing are available from JDK 1.5, so there are currently 3 ways to initialize the wrapper class:

Method 1: Pass directly to the corresponding base data type variable or constant, as

int A1=3;integer a2=new Integer (A1);
Float f=new float (3.14f);
Boolean B=new Boolean (true);

Method 2: Passing in strings, such as

Integer a=new Integer ("3");
 Float f=new Float ("3.14");
 Boolean B=new Boolean ("true");

It is worth noting that "true" can also be used, such as Boolean B=new Boolean ("true");

Method 3: Through the automatic boxing function, such as Integer a=3; Float f=3.14f; Boolean B=true; It is noteworthy that you can initialize a float type variable with a statement such as new float ("3.14") and new float ("3.14f"), but you cannot use float f=3.14 to initialize a float type variable. Because 3.14 is a double, it can only be automatically boxed as a double type variable.

We know that when a reference type uses = = To compare, it returns true only if the two point to the same object, otherwise it returns false even if the value is equal. The wrapper class also belongs to the reference type, so the following code performs false.

Float f1=new float (3.14f);
Float f2=new float (3.14f);
System.out.println (F1==F2);
However, the output from the following code is not the same as what was discussed earlier.
Import java.util.*;

public class Testwrapperclass
{public
   static void Main (String[]args)
   {
      Integer t1=3;
      Integer t2=3;
      System.out.println (T1==T2);
      
      Integer t3=128;
      Integer t4=128;
      System.out.println (T3==T4);
      
      Boolean b1=true;
      Boolean b2=true;
      System.out.println (B1==B2);
    }
The output is as shown in the following illustration:


If you follow the previous discussion, you should output false, but here T1 and T2,b1 and B2 compare to true. It's not science.

Originally, Java in order to achieve higher execution efficiency, in some classes of the design of the introduction of caching mechanism!

This is the case for the design of integers and Boolean classes here. The partial source code for the Java.lang.Integer class is as follows:

<span style= "FONT-SIZE:18PX;" >static final integer[]cache=new integer[-( -128) +127+1];
static{for
(int i=0;i<cache.length;i++)
   cache[i]=new integer[i-128);
} </span>
Obviously, the system -128~127 the integer between the integers into an integer instance and caches it through the cache array, so as long as it is an integer variable between -128~127, the object pointing to is the cache array member, so long as there are two values identical and in -128~ 127 of the integer variable, they point to the same object, so use = = For comparison also returns true. The case of a Boolean is similar.

In fact, not just in Java, some classes in Android also use caching mechanisms, such as Android ListView is a typical example, in the inherited method GetView, Convertview is actually using the caching mechanism, Thus, the system resource expenditure is greatly reduced, and the speed of graphics rendering is accelerated. Here is not the table, will be mentioned later.

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.