Java string data type conversion detailed _java

Source: Internet
Author: User
Tags array to string

In Java, string is the final class, which provides strings that cannot be modified, and string types are used frequently in projects, and the following are some common string data type conversions:

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

Copy Code code as follows:

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

//When the string type data is to be converted to a data type int, double, float, long, and so on, its data must consist of numbers.
The program complains when string data is converted into int, double, float, long, and so on, when it is composed of Chinese characters or letters.

String type is converted to a long type

Copy Code code as follows:

String type data must be a numeric composition when the string data is converted to a long type
Long N=long.parselong (c);
System.out.println ("string type is converted to long:" +n);

String converted to int type

Copy Code code as follows:

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

String to double type

Copy Code code as follows:

String data must be a numeric composition when the string type data is converted to a double type
Double m=double.parsedouble (c);
System.out.println ("string converts to Double type:" +m);

String type to type float type

Copy Code code as follows:

String data must be a numeric composition when the string type data is converted to a float type
Float m=float.parsefloat (c);
System.out.println ("String type converted to type float type:" +m);

String type to object type does not involve conversion directly assigns string value to Object

Copy Code code as follows:

Object L=c;
System.out.println ("string converts to object:" +l);

String is converted to a Boolean type

Copy Code code as follows:

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 converts to Boolean type:" N);
Output False when string data values are numeric, character, Chinese, or mixed
Boolean O=boolean.parseboolean (c);
System.out.println ("Convert String to Boolean type:" +o);

String type data converted to char type data

Copy Code code as follows:

When a string type data is converted to a char type data, an array of type char is required to accept the
Char[] O=c.tochararray ();
System.out.print ("type string 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 converted to string
convert//int type to String type

Copy Code code as follows:

int h=123456;
String l=string.valueof (h);
SYSTEM.OUT.PRINTLN ("int type is converted to String type:" +l);

Double type string

Copy Code code as follows:

Double a=1.1;
String a=string.valueof (A);
System.out.println ("Double type string:" +a);

Boolean type of String

Copy Code code as follows:

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

Char type go to String type

Copy Code code as follows:

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

Converts a char type array to a string type

Copy Code code as follows:

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

Converts several of the data in a char type array to a string type

Copy Code code as follows:

char []f={' a ', ' B ', ' C ', ' d '};
String f=string.valueof (F, 0, 3);
System.out.println (converts several of the data in the char type array to a String type: +f);

Float type converted to string type

Copy Code code as follows:

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

The long type is converted to a string type

Copy Code code as follows:

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

Object type converted to String type

Copy Code code as follows:

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

The above code is the Java string data type conversion detailed explanation, I hope you like.

Related Article

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.