局部变量(方法或语句块内定义的变量)
成员变量(方法外部,类的内部定义的变量)
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