String data type conversion, string Data Type

Source: Internet
Author: User

String data type conversion, string Data Type

String is a final class, providing strings that cannot be modified. Forced type conversion. The String type is everywhere. The following describes some common conversions of String data types.

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

* Data Type Conversion
* @ Author Administrator
*
*/
Public class data type conversion {

Public static void main (String [] args ){
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 ");
}

System. out. println ("\ n ");

// Convert int, double, boolean, char, float, long, and Object data to String
// Convert int type to String type
Int h = 123456;
String l = String. valueOf (h );
System. out. println ("int type converted to String type:" + l );

// Convert double type to String type
Double a = 1.1;
String A = String. valueOf ();
System. out. println ("double type to String:" + );

// Convert the boolean type to the String type
Boolean B = false;
String B = String. valueOf (B );
System. out. println ("boolean Type to String type:" + B );

// Convert the char type to the String type
Char d = 'a ';
String D = String. valueOf (d );
System. out. println ("char type to String type:" + d );

// Convert an array of the char type to the String type
Char [] e = {'A', 'B', 'C '};
String E = String. valueOf (e );
System. out. println ("char type array converted to String type:" + E );

// Convert several data in the char array to the String type.
Char [] f = {'A', 'B', 'C', 'D '};
String F = String. valueOf (f, 0, 3 );
System. out. println ("several data in the char array are converted to the String type:" + F );

// Convert the float type to the String type
Float g = 123;
String G = String. valueOf (g );
System. out. println ("float Type converted to String type:" + G );

// Convert the long type to the String type
Long j = 123342;
String J = String. valueOf (j );
System. out. println ("long TYPE converted to String type:" + J );

// Convert the Object type to the String type
Object k = c;
String K = String. valueOf (k );
System. out. println ("Object Type converted to String type:" + K );

System. out. println ("\ n ");


}
}

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.