String data type conversion in JAVA

  • 2020-04-01 04:04:40
  • OfStack

In JAVA, string is the final class, which cannot be modified. The type of string is often used in projects. The following is a common string data type conversion:

String data types are converted to seven data types: long, int, double, float, Boolean, char, etc


* Data type conversion
* @author Administrator
*
*/
public class Data type conversion {
public static void main(String[] args) {
String c="123456";

// when String data is to be converted to int, double, float, long, etc., the data must be composed of Numbers.
// when String data is converted to int, double, float, long, etc

//String to long


//String data must be numeric
when it is converted to long long n=Long.parseLong(c);
System.out.println("String Type conversion to long Type: "+n);

//String to int


//String data must be numeric
when converted to int int i=Integer.parseInt(c);
System.out.println("String Converted to int Type: "+i);

//String to double


//String data must be numeric
when converted to double double m=Double.parseDouble(c);
System.out.println("String Converted to double Type: "+m);

//String type converts to type float


//String data must be numeric
when converted to float float M=Float.parseFloat(c);
System.out.println("String Type to type float Type: "+M);

//String to Object does not involve converting the value of String directly to Object


Object L=c;
System.out.println("String Converted to Object : "+L);

//String type converted to Boolean type


String C="true";
//When the String type data value is true/false, the output is true/false
boolean N=Boolean.parseBoolean(+C);
System.out.println("String Type conversion to boolean Type: "N);
//When the String type data value is a number, character, Chinese character, or a mixture, the output is false
boolean o=Boolean.parseBoolean(c);
System.out.println("String Type conversion to boolean Type: "+o);

//String data converted to char data


//When String data is converted to char data, a char array is needed to accept
char[] O=c.toCharArray();
System.out.print("String Type data is 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 to String
//int is converted to String


int h=123456;
String l=String.valueOf(h);
System.out.println("int Type conversion to String Type: "+l);

//double to String


double a=1.1;
String A=String.valueOf(a);
System.out.println("double Turn type String : "+A);

// Boolean type to String type


boolean b=false;
String B=String.valueOf(b);
System.out.println("boolean Turn type String Type: "+b);

//char type to String type


char d='a';
String D=String.valueOf(d);
System.out.println("char Turn type String Type: "+d);

//char array converts to String


char[] e={'a','b','c'};
String E=String.valueOf(e);
System.out.println("char Type array to String Type: "+E);

//char array, several of which are converted to String


char []f={'a','b','c','d'};
String F=String.valueOf(f, 0, 3);
System.out.println("char Several of the data in the type array are converted to String Type: "+F);

//float converts to String


float g=123;
String G=String.valueOf(g);
System.out.println("float Type conversion to String Type: "+G);

//long to String


long j=123342;
String J=String.valueOf(j);
System.out.println("long Type conversion to String Type: "+J);

//Object is converted to String


Object k=c;
String K=String.valueOf(k);
System.out.println("Object Type conversion to String Type: "+K);
System.out.println("n");

The above code is the JAVA string data type conversion in detail, hope you like.


Related articles: