Don't tell me you don't know. Wrapper classes in Java, wrapper type, and issues that can easily occur in automatic unpacking

Source: Internet
Author: User

Most of the time, someone will ask you, do you know what the packaging class is?

or high-end to ask you you know, wrapper type, what is it?

And then you get confused and learn Java a lot of times don't know what it is.

Actually ask your person, may just want to ask you, Java object-oriented you understand how.

In Java many times we say that everything is the object.

But int,double, there are a few exceptions to tell you that he is not the object.

But most of the time we need objects, not int, but we need objects like int.

For example, you need to use int in a list, but list can only put objects. There are a lot of similar problems.

So Java5 appeared an integer.

This is the legendary packaging class.

So what's the problem with the packaging class?

This time I met a very easy mistake.

list<integer> list = new arraylist<integer> ();

List.add (1);

List.add (2);

List.add (NULL);

for (int i:list)

{

sum + = i;

}

And then it's an error.

This loop uses an int to connect the value, then Java will take advantage of the automatic unpacking function.

Then a lot of people will ask, what is automatic unpacking.

Simply put, Intvalue () is called to convert an integer to an int value.

Then the error is very easy to appear, the empty object is not intvalue this method.

So later, either the value of the time to empty, or use an integer to connect the value, do not lazy with the automatic unpacking function.

Don't tell me you don't know. Wrapper classes in Java, wrapper type, and issues that can easily occur in automatic unpacking

Related Article

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.