String data type Conversions

Source: Internet
Author: User

String is the final class that provides a string that cannot be modified. Coercion type conversion, string type everywhere. Some common string data type conversions are described below.

String data types are converted to seven data types such as long, int, double, float, Boolean, char, and so on

* data type conversion
* @author Administrator
*
*/
public class data type conversion {

Public static void Main (string[] args) {
String c= "123456";
When a string type data is to be converted to a data type such as int, double, float, long, and so on, its data must consist of numbers,
When a string type data is converted to a data type int, double, float, long, etc. by Chinese characters or letters, the program error

The //string type is converted to a long type
//string type data is converted to a long type when data of type string must be a numeric component
long N=long.parselong (c);
System.out.println ("string conversion to Long type:" +n);

//string Convert to int type
//string type data is converted to int type when data of type string must be a number composition
int I=integer.parseint (c);
System.out.println ("string converted to int type:" +i);

//string convert to double type
//string type data is converted to a double type when data of type string must be a number composition
double m=double.parsedouble (c);
System.out.println ("string converted to Double type:" +m);

//string type convert to type float type
//string type data is converted to float type data must be of number composition
float m=float.parsefloat (c);
System.out.println ("String type converted to type float type:" +m);

converting a//string type to an object type does not involve a conversion directly assigning a string value to object
Object l=c;
System.out.println ("string converted to object:" +l);

The //string type is converted to a Boolean type
String c= "true";
//When the string type data value is True/false, the direct output true/false
boolean N=boolean.parseboolean (+c);
System.out.println ("string type is converted to Boolean type:" N);
output False when the string type data value is a number, character, kanji, or blend
boolean O=boolean.parseboolean (c);
System.out.println ("String type converted to Boolean type:" +o);

//string type data into char type data
//When the string type data is converted to char type data, an array of type char is required to accept
char[] O=c.tochararray ();
System.out.print ("string type data converted to char type data:");
for (int num=0;num<o.length;num++) {
System.out.print (o[num]+ "\ t");
}

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

//int, Double, Boolean, char, float, long, object type data are converted to string
//int type conversion to String type
int h=123456;
String l=string.valueof (h);
System.out.println ("int type converted to String type:" +l);

//double Type turn string
double a=1.1;
String a=string.valueof (A);
System.out.println ("Double type turns string:" +a);

//boolean type to String type
Boolean b=false;
String b=string.valueof (B);
System.out.println ("Boolean type to String type:" +b);

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

//char type array into string type
char[] e={' A ', ' B ', ' C '};
String e=string.valueof (E);
System.out.println ("char type array converted to String type:" +e);

A //char type array in which several of the data are converted to string types
Char []f={' a ', ' B ', ' C ', ' d '};
String f=string.valueof (F, 0, 3);
System.out.println ("A few of the data in the Char type array is converted to String type:" +f);

//float type conversion to String type
float g=123;
String g=string.valueof (G);
System.out.println ("float type converted to String type:" +g);

//long type conversion to String type
long j=123342;
String j=string.valueof (J);
System.out.println ("A long type is converted to String type:" +j);

//object type conversion to String type
Object k=c;
String k=string.valueof (K);
System.out.println ("Object type converted to String type:" +k);

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


}
}

String data type Conversions

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.