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");