The String data type is converted to seven data types, including long, int, double, float, boolean, and char. floatboolean

Source: Internet
Author: User

The String data type is converted to seven data types, including long, int, double, float, boolean, and char. floatboolean

String c = "123456 ";
// To convert String data to int, double, float, long, and other data types, the data must be composed of numbers,
// When String data is composed of Chinese characters or letters and is converted to int, double, float, long, and other data types, the program reports an error.

// Convert String type to long type
// When the String type data is converted to the long type, the String type data must be composed of numbers.
Long n = Long. parseLong (c );
System. out. println ("String type converted to long type:" + n );

// Convert String to int type
// When String type data is converted to int type, String type data must be composed of numbers.
Int I = Integer. parseInt (c );
System. out. println ("String converted to int type:" + I );

// Convert String to double type
// When String type data is converted to double type, String type data must be composed of numbers.
Double m = Double. parseDouble (c );
System. out. println ("String converted to double type:" + m );

// Convert String type to float type
// When the String type data is converted to the float type, the String type data must be composed of numbers.
Float M = Float. parseFloat (c );
System. out. println ("String type converted to float type:" + M );

// Convert the String type to the Object type. If the conversion is not involved, the String value is directly assigned to the Object.
Object L = c;
System. out. println ("String converted to Object:" + L );

// Convert String type to boolean type
String C = "true ";
// When the value of the String type is true/false, true/false is output directly.
Boolean N = Boolean. parseBoolean (C );
System. out. println ("String type converted to boolean type:" + N );
// When the String type data value is composed of numbers, characters, Chinese characters, or a combination, false is output.
Boolean o = Boolean. parseBoolean (c );
System. out. println ("String type converted to boolean type:" + o );

// Convert String type data to char type data
// When String type data is converted to char type data, an array of the char type must be used to accept
Char [] O = c. toCharArray ();
System. out. print ("converting String type data to char type data :");
For (int num = 0; num <O. length; num ++ ){
System. out. print (O [num] + "\ t ");
}

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.