Java type conversion

Inconsistent data type conversion occurs at the assigned numeric types and data types of the variable are received, which need to convert from one data type to another data type. Data type to be converted into an implicit (automatic type conversions) and explicit conversions (casts) two.

 

Casts, implicit conversion (automatic conversion)

char C = 'A' ;
 int num1 = C; // automatic type conversions (small to large) 
int num2 = 65 ; 
char C1 = ( char ) num2; // casts a float F1 = 1.5F ; int num3 = ( int ) (C1 + F1); System.out.println (num3);

 

Integer to String, Integer String turn

// int to String
int num1=100;
1、String s1=num1+""; //*
2、String s2=num2.toString();//*
String s3=Integer.toString(num1);//*
3、String s4=String.valueOf(num1);
 
 
 // turn string integer
String s1="123";
int num1=Integer.parseInt(s1);//*
int num2=Integer.valueOf(s1);
int num3=Integer.valueOf(s1).intValue();

 

String turn char array, char array to a String

// String char transfer array 
String S3 = "ABC" ; char C2 = s3.toCharArray () [0]; // turn char, Note: [0] is to be displayed, the subscript char C2_1 s3.charAt = (0 ); // switch char, Note: [0] is specified index to display char [] C3 = s3.toCharArray ();

// char array to a String
char c1='A';
String s1=c1+"";
String s2=Character.toString(c1);

 

Conversion of knowledge:

1, the default java integer type int type; Double default decimal type;

2, char can be used as a special type of integer;

3, int can not be converted to boolean;

4, the decimal type to Integer type, decimals may be discarded, all precision loss occurs, it is necessary to cast;

5, boolean type can not be converted into any other type of data;

 

 

Guess you like

Origin www.cnblogs.com/bushui/p/11484750.html