Conversion between Java basic type variables and packaging classes

Source: Internet
Author: User

The relationship is as follows:


What if they convert each other?
Package the basic variables into the packaging objects by passing in parameters to the corresponding package, and convert the packaging objects into the basic type variables by using the xxxValue () method provided by the packaging class. As follows:

The code is as follows: Copy code

Public class TestWrapper {
   
Public static void main (String [] args)
    {
Int I = 100;
// Convert int type to Integer type
Integer iObj = new Integer (I );
       
// Convert Integer-type objects to int-type objects
Int m = iObj. intValue ();
    }
}

The packaging class can convert between the basic type variables and strings:
All packaging classes except Character provide the parseXXX () method to convert a specific string to a basic type variable;
The String class provides the valueOf () method to convert the basic type ratio to a String.

The code is as follows: Copy code

Public static void main (String [] args)
{
String iStr = "123 ";
Int I = Integer. parseInt (iStr );
System. out. println (I );
 
String nStr = String. valueOf (100.234 );
System. out. println (nStr );
}

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.