The difference between basic data types and wrapper classes in Java

Source: Internet
Author: User

There are two main types of data in Java:

1. Basic data type: long,int,byte,float,double, these are just basic data types, not objects

2. Object type: Long,integer,byte,float,double and all other Java objects

For Interger and Int,integer is an object of int encapsulation, the simplest version of the package can be similar to the following encapsulation

Class Integer {

private int i;

public int intvalue () {

return i;

}

}

Because int is a data type and integer is an object, at initialization time, the int type initializes with a value of 0, and the integer initialization type is null

Object types are useful in some situations, such as Map,set containers, where item needs to be object, so it is impossible to add the basic data type directly, this time, you can only add integer and not add int

So when choosing between these two types, if we just need to define a number of type int to perform some subtraction operations or pass as parameters, then you can use int directly

But if you need to do something like an object, use an integer.

The difference between basic data types and wrapper classes in Java

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.