Conversion between Java base type and string

Source: Internet
Author: User

In program development, we often need to convert between the basic data type and the string.

There are three ways to convert the base type to a string:

1. Use the ToString () method of the wrapper class

2. Using the ValueOf () method of the String class

3. Using an empty string with the base type, you get the string that corresponds to the base type data

int C = Ten=integer.tostring (c);      // Method One String str2 = string.valueof (c);    // Method Two String STR3 = c + "";      // Method Three

Again, there are two ways to convert a string to a basic type:

1. Call the Parsexxx static method of the wrapper class

2. The ValueOf () method that calls the wrapper class is converted to the basic type of wrapper class, and the box is automatically disassembled

String str = "8"; int d =integer.parseint (str);            // Method One int e =integer.valueof (str);            // Method Two

PS: Other basic types and string conversions are no longer listed here, and the methods are similar

Conversion between Java base type and string

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.