Object Wrapper and Auto Boxing

Source: Internet
Author: User

Objective

As mentioned earlier, except int,float these basic data types, all other data types are classes in Java.

So, what if I want these basic types to be class types, too?

Then use the object wrapper.

The role of the wrapper

1. It can provide many types of transformation-related approaches.

2. The members of the generic data can only be objects.

Automatic Boxing

When you use an underlying type, but the actual type requirement is its wrapper, the compiler does not give an error and will help you automate the transformation.

Such as:

ArrayList <Integer> array = new ArrayList <> ();

Array.add (3);

Before the Add method is executed, the base type 3 is converted to the integer wrapper type.

code example

1  Packagetest;2 3  Public classJava7learn {4     5      Public Static voidMain (string[] args) {6         7         /*8 * Integer conversion to String type9          */Ten         inti = 999; One         //defines an integer object and initializes it with an I value AInteger tempi =i; -         //converting an Integer object to a String type - tempi.tostring (); the          -         //of course, you can also use the following static method to convert directly -         //integer.tostring (i); -          +         /* - * String type converted to integral type +          */ AString s = "1"; at         //converting a string type to an integral type - Integer.parseint (s); -     } -}

Summary

It's a real hassle to recall the process of implementing object transformations (involving string types) in C + + before.

Java provides encapsulation of the underlying data, which naturally makes it more powerful and easier to use.

Object Wrapper and Auto Boxing

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.