java 基础知识3-变量初始化

来源:互联网 发布:进口数据查询 编辑:程序博客网 时间:2024/06/04 18:44

class Test{Test(){ System.out.println("This is the default call class");}Test(int x){System.out.println("This is the static call class test" + x);}}class Vary{Vary(){System.out.println("This is the Vary's construct function");}int i;short sh;long lo;float j;double db;byte by;char ch;boolean bo;int ii = 1;short ssh = 2;long llo = 3;float jj = 4;double ddb = 5;byte bby = 6;char cch  = '7';boolean bbo = true;Test test1;Test test2 = new Test();void printDefault(){System.out.println("This is the Default print");System.out.println(i);//0 int System.out.println(sh);//0 shortSystem.out.println(lo);//0 longSystem.out.println(j);//0.0 floatSystem.out.println(db);//0.0 doubleSystem.out.println(by);//0 byteSystem.out.println(ch); //|_| (空白) charSystem.out.println(bo);//false boolean}void printDefine(){System.out.println("This is the Define print");System.out.println(ii);//1 intSystem.out.println(ssh);//2 shortSystem.out.println(llo);//3 longSystem.out.println(jj);//4.0 floatSystem.out.println(ddb);//5.0 doubleSystem.out.println(bby);//6 byteSystem.out.println(cch);//7 charSystem.out.println(bbo);//true boolean}void printClass(){System.out.println("This is the Class print");System.out.println(test1); //nullSystem.out.println(test2); //test@280bca}//*****************************************************************************************void print(){System.out.println("what is wrong!");}int funInt(int xp){return xp;}//如果只有”语句2“注释,那么语句1会报错:(xx2 cannot be resolved to a variable)//如果语句1,2都不注释,那么语句1会报错:(Cannot reference a field before it is defined)//说明不能用一个未定义或者说还没初始化的变量去初始化另外一个变量//int xx1 = funInt(xx2); //语句1//int xx2 = funInt(xx1); //语句2//****************************************************************************************//为了支持前向引用,java会先把所有的变量都加载到符号表中,符号表中的所有变量采用默认值,//加载完后再按照变量的顺序进行初始化。因此xx3先初始化,它会调用函数funcReturn(),返回值//是没有被初始化的n,结果就是0了。int funReturn(){return xx4;}int xx3 = funReturn();// xx3 = 0,not 1int xx4 = 1;  // xx4 = 1//***************************************************************************************static Test test3 = new Test(3); //call this first}public class Varibles {public static void main(String[] args) {Vary varies = new Vary();varies.printClass();varies.printDefine();varies.printDefault();System.out.println("xx3 = " + varies.xx3);System.out.println("xx4 = " + varies.xx4);}static Test test4 = new Test(4);static Test test5 = new Test(5);}





输出结果:
This is the static call class test4
This is the static call class test5
This is the static call class test3
This is the default call class
This is the Vary's construct function
This is the Class print
null
Test@39fc0f04
This is the Define print
1
2
3
4.0
5.0
6
7
true
This is the Default print
0
0
0
0.0
0.0
0
从上面可以看出:
1:java在类中要进行对变量(字段)的初始化,这和C++不同,C++中类中是不能初始化变量的。
2:static的变量初始化要在非static变量之前
从程序中可以可以看出,类中成员test1,test2,test3的定义顺序就是按照1,2,3的顺序来的,但是显示的调用构造函数,却是test3的在前面,test2的在后面,test1的没有。
从程序中也可以看出test3是static类型 的变量,所以static类型的变量初始化要在非static之前,即便非static变量在static变量之前定义。
3:变量的初始化是按照变量的定义顺序来的(但static一定要在非static之前初始化)
从这部分程序可以看得出来:

static Test test4 = new Test(4);
static Test test5 = new Test(5);
显示结果是:
This is the static call class test4
This is the static call class test5
4:变量没有显式的初始化,会有默认的初始化
从函数printDefault可以看出,以及test2变量初始化时调用的默认构造函数也可以看出来:
5:变量的初始化可以调用函数,包括类对象的构造函数的调用
从test1,test2,test3,test4,test5这些对象,以及变量xx3,xx4可以看出
6:对于类对象,如果不new的话,那仅仅是产生一个空的指针
这点我们从test1 和test2的对比可以看出,调用printClass函数,可以看到test1对象输出的是null,test2对象输出的是Test@39fc0f04。再次从test1没有调用构造函数也可以看出,所以如果要用类对象的话,一定记住不要忘记 new 。
7:java中支持变量的前向引用
也就是说在变量没有定义之前,我们再函数体中就可以引用这些变量去做该做的事情。这个从变量funRetun可以看的出来,在定义函数funReturn时,变量xx4却在它后面定义。
8:不能用未定义(未初始化)的变量去初始化另外的变量
//如果只有”语句2“注释,那么语句1会报错:(xx2 cannot be resolved to a variable)
//如果语句1,2都不注释,那么语句1会报错:(Cannot reference a field before it is defined)
//说明不能用一个未定义或者说还没初始化的变量去初始化另外一个变量
// int xx1 = funInt(xx2); //语句1
// int xx2 = funInt(xx1); //语句2
上面的这两个变量定义,如果把语句2注释,语句1报错出现xx2 cannot be resolved to a variable(xx2 不能被解析为一个变量),也就是根本没有xx2这个东西。但是我们把语句2注释去掉,语句1依然报错出现Cannot reference a field before it is defined(不能引用一个字段在它被定义之前),这说明虽然xx1初始化语句在xx2之前,但是此时对xx1进行初始化时,存在xx2这个变量,只是xx2没有被初始化罢了,这跟(xx2不能被解析为一个变量)是完全不同的错误。
9:使用函数去初始化变量时,可以在函数中使用欲初始化变量后面定义的变量
int funReturn()
{
return xx4;
}
int xx3 = funReturn();// xx3 = 0,not 1
int xx4 = 1;      // xx4 = 1


在初始化xx3时调用了函数funReturn,但是此时函数返回xx4去初始化xx3,虽然xx4没有定义,但是依然没有出错,原因是什么呢。原来java为了支持所谓的前向引用,在初始化字段之前会把所有的字段都加载到符号表中,然后再对变量进行初始化,那么这样的话在调用函数funReturn时候xx4并没有初始化,所以xx4就是默认值0了,那么xx3得到的值就是0了。






原创粉丝点击