Java 中数据类型之间的转换

1. short-->int
exp: short shortvar=0;
int intvar=0; 
shortvar= (short) intvar

2. int-->short
exp: short shortvar=0;
int intvar=0;
intvar=shortvar;

3. int->String
exp: int intvar=1;
String stringvar;
Stringvar=string.valueOf (intvar);

4. float->String
exp: float floatvar=9.99f;
String stringvar;
Stringvar=String.valueOf (floatvar);

5. double->String
exp double doublevar=99999999.99;
String stringvar;
Stringvar=String.valueOf (doublevar);

6. char->String
exp char charvar=’a’;
String stringvar;
Stringvar=String.valueOf (charvar);

7. String->int、float、long、double
Exp String intstring=”10”;
String floatstring=”10.1f”;
String longstring=”99999999”;
String doubleString=”99999999.9”;

Int I=Integer.parseInt (intstring);
Float f= Integer.parseInt (floatstring);
Long lo=long. parseInt (longstring);
Double d=double. parseInt (doublestring);

8 String->byte、short
Exp String s=”0”;
Byte b=Integer.intValue(s);
Short sh=Integer.intValue(s);

9 String->char
Exp String s=”abc”;
Char a=s.charAt(0);
返回字符a

10 String-->boolean
String s=”true”;
boolean flag=Boolean.valueOf (“s”);




你可能感兴趣的:(java,String,float,byte)