一:“==”的不同含义
当“==”施加于原始数据类型变量时,是比较变量所保存的数据是否相等
当“==”施加于引用类型变量时,是比较这两个变量是否引用同一对象。
测试代码如下:
package test; public class qq { public static void main(String[] args) {
// TODO Auto-generated method stub
Foo obj1=new Foo();
Foo obj2=new Foo();
System.out.println(obj1==obj2); } }
class Foo{
int value=100;
}
结果是false。
分析:例子中含义是判断这两个变量是否引用同一对象,因为例子中定义的是两个不同的对象,所以返回false。
二:类的构造方法
如果类提供了一个自定义的构造方法,将导致系统不再提供默认构造方法。
源代码:
package test; public class qq { public static void main(String[] args) {
// TODO Auto-generated method stub
Foo obj1=new Foo();
} }
class Foo{
int valud;
public Foo(int initValue)
value=initValue;
}
分析:程序报错,原因因为自定义了构造方法,系统不再提供默认构造方法。
三:静态初始化块的执行顺序
源代码:
class Root
{
static{
System.out.println("Root的静态初始化块");
}
{
System.out.println("Root的普通初始化块");
}
public Root()
{
System.out.println("Root的无参数的构造器");
}
}
class Mid extends Root
{
static{
System.out.println("Mid的静态初始化块");
}
{
System.out.println("Mid的普通初始化块");
}
public Mid()
{
System.out.println("Mid的无参数的构造器");
}
public Mid(String msg)
{
//通过this调用同一类中重载的构造器
this();
System.out.println("Mid的带参数构造器,其参数值:" + msg);
}
}
class Leaf extends Mid
{
static{
System.out.println("Leaf的静态初始化块");
}
{
System.out.println("Leaf的普通初始化块");
}
public Leaf()
{
//通过super调用父类中有一个字符串参数的构造器
super("Java初始化顺序演示");
System.out.println("执行Leaf的构造器");
} } public class TestStaticInitializeBlock
{
public static void main(String[] args)
{
new Leaf(); }
}
结果截图:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQgAAACcCAIAAAAMFKJJAAAMT0lEQVR4nO2dzaHrKAyFXcxUQQfTiLbTirbTiDqYKlQMs+BPgOzk5trOs32+xXu5jv+ScEDAQV4iAGBi2eEUheE1ANdlzxIMPYDbAGEA4FCLslANg5bA+sGp9J//IsmO9wbA17DCyHpQDh9JwxWGcsjbhIaTKhOriK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Hi284AlBAAHCAMABwgDAAcIAwAHCAMAh32FsemdAOA6NGHo7x9jeuL8CwCHsqcwvAmqLS96PaCfdy8mC98jsK9Tup+WwNISUHglDDP37RrShrnw1Sny0Yuep22Yswe9mxIUkeLCzQ/lVu0uvJtTejaPjJZc8Ey2hWFrXi9Oml1UvW9yMwW6MiWjZrUC1uO0zk4XN27vNNnJKT3pGb0jkNkUxlhw3NVF7wjDSUueKBZUEdGkwuJMLH6m5l0tl97TKT0bfpCcE8T4Qhh+Z3rF7ewJY82L7lbVJNXsJKL9Ls1vtK9TutiFzIUYIwjgjVDK606vpo1dDaU8y61jvzXHdRV4Nmzu75RuH0eZRc9ZAwMuQCeMufvcbZ0iqcHt/GNhuBFZWp5nbdrDmNV+Tmm7tKT4ytFagBgPnvl+4UV3W4zq6h/WNbbobT+ndNvQeZ4RSgFYQgDwgDAAcIAwAHCAMABwgDAAcPgThAGzOvjj+K0wYFYHt8QKoz5FfvJmj57s5v0+3ax+bp5z8FQ6YQy+1UqXE2syDp5oVo/VEWiPLZP0++Y5B4+mZTvPUX7Nvty5U3XK4dkq7vPM6ufnOQdPpW8xQqgp6rsCJ7QElu6pGKxrwjjSrH52nnPwVPoWo6yHICsM5dBcqXMVfZ5ZPZ6f5xw8lbGPEWMUEaGQKmtVjUO346UwjjOrH5DnHACXSRidoZulthrrwpi7z0ea1XfOcw6ASxFGTcLeLX0Ixuxdjd5HV7WbZvXT8pyDZ/MnzHwD8McBYQDgAGEA4ABhAOAAYQDgAGEA4ABhAOAAYQDg8D/kV2h0gosnwAAAAABJRU5ErkJggg==" alt="" />
分析:
首先执行父类静态的内容,父类静态的内容执行完毕后,接着去执行子类的静态的内容,当子类的静态内容执行完毕之后,再去看父类有没有非静态代码块,如果有就执行父类的非静态代码块,父类的非静态代码块执行完毕,接着执行父类的构造方法;父类的构造方法执行完毕之后,它接着去看子类有没有非静态代码块,如果有就执行子类的非静态代码块。子类的非静态代码块执行完毕再去执行子类的构造方法。总之一句话,静态代码块内容先执行,接着执行父类非静态代码块和构造方法,然后执行子类非静态代码块和构造方法。
注意:子类的构造方法,不管这个构造方法带不带参数,默认的它都会先去寻找父类的不带参数的构造方法。如果父类没有不带参数的构造方法,那么子类必须用supper关键子来调用父类带参数的构造方法,否则编译不能通过。
四:在静态方法中访问类的实例成员
源代码:
package test; public class qq
{
int x = 3;//类的实例变量,初始化值为3
static int y = 4;//类的静态变量,初始化值为4
public static void method()//静态方法
{
System.out.println("实例变量x = " + new qq().x);//在静态方法中访问类的实例变量需首先进行类的实例化
System.out.println("静态变量y = " + y);//在静态方法中可直接访问类的静态变量
} public static void main(String[] args)
{
qq.method();
qq ex = new qq();
System.out.println("x = " + ex.x);
}
}
五:Java字段初始化的规律
源代码:
package test; public class qq { public static void main(String[] args) {
// TODO Auto-generated method stub
InitialzeBlockClass obj=new InitialzeBlockClass();
System.out.println(obj.field);
obj=new InitialzeBlockClass(300);
System.out.println(obj.field);
} }
class InitialzeBlockClass{
{
field=200;
}
public int field=100;
public InitialzeBlockClass(int value){
this.field=value;
}
public InitialzeBlockClass(){ }
}
实验结果截图:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGgAAAA3CAIAAABvppjUAAABN0lEQVR4nO3YzY2DMBQA4VeXC6IeV0MzLmb3kADGP0s0hyQr5rvFXNCIJzuOHyHx6Rf4rwwHGQ4yHNSEKzlFRCxrvxYRKZc/F++kDrcukXIpOdXh1mXveDwZLt5LP6rnEo+Y+4NHr+HizVyE23+sS0TK+fxN1ovvfOlv8FK4fTLrYW4W3/nS3+CFUY3TXD5HtV+8matw9fd0bAnDxXtpdtWTat/0ONLyAAwZDjIcZDjIcJDhIMNBp3DVQe7qyOY5brhactqCeK00Ng/X/bnyWqnWhNuGtfsH77VSY7I5bLPotdLMbFfdanitNDH/4o7NwWulgcm1Uj17HkdGPABDhoMMBxkOMhxkOMhwkOEgw0GGgwwHGQ4yHGQ4yHCQ4SDDQYaDDAcZDjIcZDjIcJDhIMNBhoMMB/0CtBRQe2cauX4AAAAASUVORK5CYII=" alt="" />
分析(Java字段初始化规律):所有类的变量都默认初始化为null,比如String a; Integer b;数字类的原始数据类型默认初始化为0,比如int a; short b; char c;boolean默认初始化为false;
类加载时直接初始化静态字段;
类加载时调用静态方法初始化静态字段;
实例化对象时,在调用构造函数之前代码块中初始化字段;
实例化对象时,在调用构造函数之时初始化字段;