882
技術社區[雲棲]
馬士兵J2SE-第二章-J2SE基礎語法-標識符、關鍵字、數據類型轉換
局部變量(方法或語句塊內定義的變量)
成員變量(方法外部,類的內部定義的變量)
public class test{
static int j=6;
public static void main(String[] args){
int i=0;
System.out.println("i= "+i);
System.out.println("j= "+j);
}
}
輸出結果:
i= 0
j= 6
字符型
public class test{
public static void main(String[] args){
boolean b=false;
int x,y=9;
char c1,c2;
c1='\u534e';
c2='c';
//x=12;
System.out.println("b= "+b);
System.out.println("x= "+x);
System.out.println("y= "+y);
System.out.println("c1= "+c1);
System.out.println("c2= "+c2);
}
}
輸出結果:
D:\>javac test.java
test.java:11: 可能尚未初始化變量 x
System.out.println("x= "+x);
^
1 錯誤
y才是9,x未初始化。
修改後,解除注釋以後的結果是:
b= false
x= 12
y= 9
c1= 華
c2= c
JAVA八種數據類型轉化,溢出,所占存儲空間,表數範圍
public class test{
public static void main(String[] args){
int i1=123;
System.out.println("i1= "+i1);
int i2=456;
System.out.println("i2= "+i2);
double d1=(i1+i2)*1.2;//int->double
System.out.println("d1= "+d1);
float f1=(float)((i1+i2)*1.2);//int->float,需要加強製轉換符
System.out.println("f1= "+f1);
byte b1=67;
System.out.println("b1= "+b1);
byte b2=89;
System.out.println("b2= "+b2);
byte b3=(byte)(b1+b2);//byte->int,需要加強製轉換符,越界了
System.out.println("b3= "+b3);
double d2=1e200;
System.out.println("d2= "+d2);
float f2=(float)d2;//double->float,溢出
System.out.println("f2= "+f2);
float f3=1.23f;//必須加f
System.out.println("f3= "+f3);
long l1=123;
System.out.println("l1= "+l1);
long l2=3000000000L;//必須加L
System.out.println("l2= "+l2);
float f=l1+l2+f3;//係統將轉化為float運算
System.out.println("f= "+f);
long l=(long)f;//強製轉換會舍去小數部分(不是四舍五入)
System.out.println("l= "+l);
}
}
輸出:
i1= 123
i2= 456
d1= 694.8
f1= 694.8
b1= 67
b2= 89
b3= -100
d2= 1.0E200
f2= Infinity
f3= 1.23
l1= 123
l2= 3000000000
f= 3.0E9
l= 3000000000
分析:
byte b1=67;
System.out.println("b1= "+b1);
byte b2=89;
System.out.println("b2= "+b2);
byte b3=(byte)(b1+b2);//byte->int,需要加強製轉換符,越界了
System.out.println("b3= "+b3);
byte占存儲空間 1字節,表數範圍-128~127
156在內存中是:1001 1100(最高一位是符號位)
補碼計算,按位取反再加1得:1110 0100
因其符號位是1,所以是負數:-100
類型 占用存儲空間 表數範圍
byte 1字節 -128~127
short 2字節 -2(15)~2(15)-1
int 4字節 -2(31)~2(31)-1
long 8字節 -2(63)~2(63)-1
public class test{
public static void main(String[] args){
int i=1,j=12;
System.out.println("i= "+i);
System.out.println("j= "+j);
float f1=(float)0.1;//0.1f 這兩者是有區別的,(float)0.1是把八個字節的double強轉為四個字節的float,而0.1f就是四個字節的float。
float f2=0.1f;
System.out.println("f1= "+f1);
System.out.println("f2= "+f2);
float f3=123;
System.out.println("f3= "+f3);
long l1=12345678,l2=88888888L;
System.out.println("l1= "+l1);
System.out.println("l2= "+l2);
double d1=2e20,d2=124;
System.out.println("d1= "+d1);
System.out.println("d2= "+d2);
i=j+10;
System.out.println("i= "+i);
i=i/10;
System.out.println("i= "+i);
i=(int)(i*0.1);
System.out.println("i= "+i);
char c1='a',c2=125;
System.out.println("c1= "+c1);
System.out.println("c2= "+c2);
char c=(char)(c1+c2-1);
System.out.println("c= "+c);
byte b1=1,b2=2,b3=127;
System.out.println("b1= "+b1);
System.out.println("b2= "+b2);
System.out.println("b3= "+b3);
byte b=(byte)(b1-b2);
System.out.println("b= "+b);
float f4=f1+f3;
System.out.println("f4= "+f4);
float f5=(float)(f1+f3*0.1);
System.out.println("f5= "+f5);
double d=d1*i+j;
System.out.println("d= "+d);
float f=(float)(d1*5+d2);
System.out.println("f= "+f);
}
}
輸出:
i= 1
j= 12
f1= 0.1
f2= 0.1
f3= 123.0
l1= 12345678
l2= 88888888
d1= 2.0E20
d2= 124.0
i= 22
i= 2
i= 0
c1= a
c2= }
c= ?
b1= 1
b2= 2
b3= 127
b= -1
f4= 123.1
f5= 12.4
d= 12.0
f= 1.0E21
public class test{
public static void main(String[] args)
{
int i=1,j=2;
float f1=(float)0.1; float f2=123;
long l1=12345678,l2=888888888888l;
double d1=2e20,d2=124;
byte b1=1,b2=2,b3=(byte)129;
j=j+10;
i=i/10;
i=(int)(i*0.1);
char c1='a',c2=125;
byte b=(byte)(b1-b2);
char c=(char)(c1+c2-1);
float f3=f1+f2;
float f4=(float)(f1+f2*0.1);
double d=d1*i+j;
float f=(float)(d1*5+d2);
System.out.println(" i="+i+" j="+j+" f1="+f1+" f2="+f2+" l1="+l1+" l2="+l2+" d1="+d1+" d2="+d2+" b1="+b1+" b2="+b2+" b3="+b3
+" c1="+c1+" c2="+c2+" b="+b+" c="+c+" f3="+f3+" f4="+f4+" d="+d+"\n"+" f="+f);
}
}
//byte占存儲空間 1字節,表數範圍-128~127
//129在內存中是:1000 0001(最高一位是符號位)
//補碼計算,按位取反再加1得:1111 1111
//因其符號位是1,所以是負數:-127
輸出結果:
i=0 j=12 f1=0.1 f2=123.0 l1=12345678 l2=888888888888 d1=2.0E20 d2=124.0 b1=1 b2=2 b3=-127 c1=a c2=} b=-1 c=? f3=123.1 f4=12.4 d=12.0
f=1.0E21
最後更新:2017-04-02 06:52:05