"Java" basic data types and encapsulation classes, string conversions __java basic data types and their encapsulation class strings

Source: Internet
Author: User
one, the conversion between the basic data type and its corresponding encapsulation class basic data types are converted to encapsulated classes

1 • Direct Assignment (automatic packaging)

Integer i=3;

2 • Using the enclosing parameter constructor of the encapsulated class (instantiated)

Integer i=new integer (3);

3 • Use the valueof () method provided by the encapsulation class (static method)

Integer i=integer.valueof (3);


encapsulate class to basic data type

1. Direct assignment (Automatic unpack)

Integer i=new integer (2);

int j=i;

2 • Use the value () method provided by the encapsulation class

Integer i=new integer (3);

int J=i.intvalue ();

float F=i.floatvalue ();

ii. conversion between basic data types and strings Convert base data type to String

1 • Using the valueof () method of the String class (static method)

String s=string.valueof (123);

2 • Use the ToString () method of the encapsulated class (static method)

String s=integer.tostring (3);

3 • Use string connector "+"

String s= "" +123;


string conversion to basic data type

1. Use the static method of the encapsulation class

int I=integer.parseint ("12");

third, the encapsulation class and the string between the conversion string conversion to encapsulated class

1 • Using the enclosing parameter constructor of the encapsulated class (instantiated)

Integer i=new integer ("123");

2 • Using the valueof () method of the Encapsulation class

Integer i=integer.valueof ("123");


encapsulate class to String

1 • Use string connector "+"

Integer i=new integer (123);

String s= "" +i;

2 • Using the ToString () method of the encapsulated class

Integer i=new integer (123);

String s=i.tostring ();  

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.