Java Fundamentals point 1: Basic type wrapper class

Source: Internet
Author: User

Introduction to basic types of packaging classes

In general, we often use meta-types in our programs, such as

int data = 1;

float data = 2.1F;

However, in some scenarios it is not possible to use meta-types directly, such as if we want to create an int type of ArrayList, you cannot write directly:

Arraylist<int>

Because the type requirement in the ArrayList "<>" symbol is a class, it cannot be a meta-type.

So Java in order to solve this problem, put forward the packaging class solution. For each meta-type, Java introduces a wrapper class (wrapper class) to hold the value of the meta-type. Now Java is able to automatically package the meta-types in your program. This means that where you want to use the wrapper class, even if your code is written in meta-type, Java will help you wrap it automatically. In addition, if you are using a wrapper class where you need to use the meta type, Java will automatically unpack it.

is a wrapper class that corresponds to several types of numeric elements:

As we can see, the following six types of numeric wrapper classes are inherited from a parent class, number.

Common methods of packing class

The wrapper class contains many commonly used methods, which are grouped into two categories. One is the method inherited from number, and the other is the unique method of this class.

The method that inherits from number

1. Remove the package class corresponding to the value of the series method, the return of the method is the wrapper class object corresponding to the truth of the meta-type.

2. Two comparison functions of the same kind

3. Determine if the values of two objects of the same class are equal

The wrapper class added itself

Each class defines itself as a unique method for some special purposes, and we use an integer class example, and the other class method is similar to the integer, and is not described.

Character Packing class

There is also a type in the basic type, Java also provides it with wrapper class, that is char, wrapper class character.

There are several more commonly used methods in the character class, as follows:

  

  

Java Fundamentals point 1: Basic type wrapper class

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.