In JAVA, string is a final class, and the provided string cannot be modified. The string type is often used in projects. The following introduces the more commonly used string data type conversion:
String data type conversion to long , int, double, float, boolean, char and other seven data types
* 数据类型转换 * @author Administrator * */ public class 数据类型转换 { public static void main(String[] args) { String c="123456";
//When String type data is to be converted into int, double, float, long and other data types, the data must be composed of numbers.
//When String type data consists of Chinese characters or letters and is converted into int, double, float, long and other data types, the program reports an error
//String类型数据转换成long类型时 String类型的数据必须是数字构成 long n=Long.parseLong(c); System.out.println("String类型转换成long型:"+n);
//String类型数据转换成int类型时 String类型的数据必须是数字构成 int i=Integer.parseInt(c); System.out.println("String转换成int类型:"+i);
//String类型数据转换成double类型时 String类型的数据必须是数字构成 double m=Double.parseDouble(c); System.out.println("String转换成double类型:"+m);
//String类型数据转换成float类型时 String类型的数据必须是数字构成 float M=Float.parseFloat(c); System.out.println("String类型转换成类型float类型:"+M);
Object L=c; System.out.println("String转换成Object:"+L);
String C="true"; //当String类型数据值为true/false时,直接输出true/false boolean N=Boolean.parseBoolean(+C); System.out.println("String类型转换成boolean类型:"N); //当String类型数据值为数字、字符、汉字或混合构成,则输出false boolean o=Boolean.parseBoolean(c); System.out.println("String类型转换成boolean类型:"+o);
//当String类型数据转换成char类型数据时,需要用一个char类型的数组来接受 char[] O=c.toCharArray(); System.out.print("String类型数据转换成char类型数据:"); for(int num=0;num<O.length;num++){ System.out.print(O[num]+"\t"); } System.out.println("\n");
//Convert int type to String type
int h=123456; String l=String.valueOf(h); System.out.println("int类型转换成String类型:"+l);
double a=1.1; String A=String.valueOf(a); System.out.println("double类型转String:"+A);
boolean b=false; String B=String.valueOf(b); System.out.println("boolean类型转String类型:"+b);
char d='a'; String D=String.valueOf(d); System.out.println("char类型转String类型:"+d);
char[] e={'a','b','c'}; String E=String.valueOf(e); System.out.println("char类型数组转换成String类型:"+E);
char []f={'a','b','c','d'}; String F=String.valueOf(f, 0, 3); System.out.println("char类型数组中其中几个数据转换成String类型:"+F);
float g=123; String G=String.valueOf(g); System.out.println("float类型转换成String类型:"+G);
long j=123342; String J=String.valueOf(j); System.out.println("long类型转换成String类型:"+J);
Object k=c; String K=String.valueOf(k); System.out.println("Object类型转换成String类型:"+K); System.out.println("\n");