Home  >  Article  >  Java  >  Detailed explanation of string data type conversion in JAVA

Detailed explanation of string data type conversion in JAVA

高洛峰
高洛峰Original
2017-01-22 10:33:181498browse

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 type Convert to long type

//String类型数据转换成long类型时 String类型的数据必须是数字构成
long n=Long.parseLong(c);
System.out.println("String类型转换成long型:"+n);

//Convert String to int type

//String类型数据转换成int类型时 String类型的数据必须是数字构成
int i=Integer.parseInt(c);
System.out.println("String转换成int类型:"+i);

//Convert String to double type

//String类型数据转换成double类型时 String类型的数据必须是数字构成
double m=Double.parseDouble(c);
System.out.println("String转换成double类型:"+m);

//Convert String type to float type

//String类型数据转换成float类型时 String类型的数据必须是数字构成
float M=Float.parseFloat(c);
System.out.println("String类型转换成类型float类型:"+M);

//Converting the String type to the Object type does not involve conversion and assigning the String value directly to the Object

Object L=c;
System.out.println("String转换成Object:"+L);

//Converting the String type to the boolean type

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 type Convert data to char type data

//当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, double, boolean, char, float, long, Object type data to String

//Convert int type to String type

int h=123456;
String l=String.valueOf(h);
System.out.println("int类型转换成String类型:"+l);

//Double type to String

double a=1.1;
String A=String.valueOf(a);
System.out.println("double类型转String:"+A);

//Boolean type to String type

boolean b=false;
String B=String.valueOf(b);
System.out.println("boolean类型转String类型:"+b);

//Char type to String type

char d=&#39;a&#39;;
String D=String.valueOf(d);
System.out.println("char类型转String类型:"+d);

//Char type array to String Type

char[] e={&#39;a&#39;,&#39;b&#39;,&#39;c&#39;};
String E=String.valueOf(e);
System.out.println("char类型数组转换成String类型:"+E);

//Convert several data in the char type array to String type

char []f={&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;};
String F=String.valueOf(f, 0, 3);
System.out.println("char类型数组中其中几个数据转换成String类型:"+F);

//Convert float type to String type

float g=123;
String G=String.valueOf(g);
System.out.println("float类型转换成String类型:"+G);

//Convert long type to String type

long j=123342;
String J=String.valueOf(j);
System.out.println("long类型转换成String类型:"+J);

//Convert Object type to String type

Object k=c;
String K=String.valueOf(k);
System.out.println("Object类型转换成String类型:"+K);
System.out.println("\n");

The above code is a detailed explanation of string data type conversion in JAVA. I hope you like it.

For more detailed explanations of string data type conversion in JAVA, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn