- public static void main(String[] args)
- {
- char ch ='a';
- int n = (int)ch;
- System.out.println(n);
- }
复制代码
先将a赋值给一个单字节变量 char ch = 'a';
然后,将ch这个变量转换成整型在赋值给一个变量 int n = (int)ch;
输出变量n System.out.println(n);其它的:
- class Change{
- public static void main(String[] args){
- short shortvar=0;
- int intvar=0;
- String stringvar;
- float floatvar=9.99f;
- double doublevar=99999999.99;
- char charvar='9';
- String intstring="10";
- String floatstring="10.1f";
- String longstring="99999999";
- String doubleString="99999999.9";
- String s1="0";
- String s2="abc";
- String s3="true";
- //1.short-->int
- intvar=shortvar;
- System.out.println(intvar);
- //2.int-->short
- shortvar= (short) intvar ;
- System.out.println(shortvar);
- //3.int->String
- intvar=1;
- stringvar=String.valueOf (intvar);
- System.out.println(stringvar);
- //4.float->String
- stringvar=String.valueOf (floatvar);
- System.out.println(stringvar);
- //5. double->String
- stringvar=String.valueOf (doublevar);
- System.out.println(stringvar);
- //6. char->String
- stringvar=String.valueOf (charvar);
- System.out.println(stringvar);
- //7 String->int、float、long、double
- int i=Integer.parseInt (intstring);
- float f= Float.parseFloat (floatstring);
- long lo=Long. parseLong (longstring);
- double d=Double. parseDouble(doubleString);
- System.out.println(i+"/n"+f+"/n"+lo+"/n"+d);
- //8 String->byte、short
- byte b=Byte.parseByte(s1);
- short sh=Short.parseShort(s1);
- System.out.println(b+"/n"+sh) ;
- //9 String->char
- char a=s2.charAt(0);
- System.out.println(a);
- //10 String-->boolean
- boolean flag=Boolean.getBoolean(s3);
- System.out.println(s3);
- }
- }
复制代码
|