There are basic types in JAVA Why do you have a wrapper type? __java

Source: Internet
Author: User
Tags wrapper

The basic data types and packing types in Java are:

Basic Type Wrapper Type
Boolean Boolean
Char Character
Int Integer
Byte Byte
Short Short
Long Long
Float Float
Double Double



Why are there two types of these?

We all know that in the Java language, new objects are stored in the heap, we use them through references in the stack, but for a series of types such as int that are often used, it's not very efficient to store them in a heap with new-especially simple small variables. So there's the basic type, like C + +, where Java uses a similar approach, which is more efficient when it is not created with the new keyword, but directly by storing the value of the variable in the stack.


Why do you have a packing type when you have a basic type?

We know that Java is a programming language of object-oriented objects, the basic type does not have the nature of the object, and in order for the base type to also have the object's characteristics, the wrapper type appears (such as we must use the wrapper type instead of the base type when using the collection type collection), which is equivalent to the base type " Wrapping up, makes it have the nature of the object, and adds attributes and methods to it, enriching the basic type of operation.

In addition, when you need to put something in the Arraylist,hashmap, the basic type like int,double is not put in, because the container is loaded object, this is the need for these basic types of wrapper class.

The two convert to each other:

1, int turn integer

int i = 0; Integer II = new Integer (i);
2, integer turn int

Integer II = new integer (0); int i = Ii.intvalue ();


the difference between the two:

1. Different ways of declaring:

The base type does not use the New keyword, and the wrapper type needs to use the New keyword to allocate storage space in the heap;

2. Different storage mode and location:

The basic type is to store the value of the variable directly on the stack, and the wrapper type is to place the object in the heap and then use it by reference;

3. Different initial values:

The initial value of the base type, such as int is 0,boolean to false, and the wrapper type's initial value is null;

4. Different ways of use:

Basic types are directly assigned directly to use, and package types are used when collections such as collection, map.


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.