部门内部JAVA培训几点不清晰地方

2013-10-28

1.重新考虑一下JAVA中的基本数据类型和引用类型,JAVA中没有指针,只有引用;参考一下自己之前写的指针和引用的区别。

2.关于JAVA构造函数的问题

(1)父类如果构造函数没有形参,有时也叫入参,那么子类可以不写构造函数

public class Children extends Parent{
    public static void main(String[] args){
    Children children = new Children();
    System.out.println("Hello world!");
}
}

class Parent{
    public Parent(){    
        System.out.println("Parent!");
    };
}

输出:

Parent!

Hello world!

(2)父类如果构造函数有形参,子类必须有构造函数

public class Children extends Parent{
    public static void main(String[] args){
    Children children = new Children();
    System.out.println("Hello world!");
}
}

class Parent{
    public Parent(String a){    
        System.out.println("Parent!");
    };
}

输出:编译错误

 3.String和StringBuffer之间的关系

(1)String和StringBuffer之间的区别大概就是String没有缓冲区,不能动态增长,如果做了append的操作就会在内存中重新分配一块空间,再往底层说实际上是调用的StringBuilder分配内存空间,性能方面的测试参照园内博友一篇文章

http://www.cnblogs.com/fancydeepin/archive/2013/04/23/min-snail-speak_String-StringBuffer-StringBuilder.html

有关于性能方面的测试,非常不错~ 

贴一段代码进行佐证

public class DifferStrAndStrBuf{
    public static void main(String[] argc){
        String a = "String";
        System.out.println("String orignal address:" + a.hashCode());
        a += "add";
        System.out.println("String change address:" + a.hashCode());
        StringBuffer b = new StringBuffer("StringBuffer");
        System.out.println("StringBuffer orignal address:" + b.hashCode());
        b.append("plus");
        System.out.println("StringBuffer change address:" + b.hashCode());
    }
}

说明:由于JVM的原因,无法向C/C++一样获取到地址,用hashCode代替,关于hashCode参照园内博友一篇文章

http://www.cnblogs.com/hxsyl/p/3203470.html

输出:

String orignal address:-1808118735

String change address:1814688464

StringBuffer orignal address:1701381926

StringBuffer change address:1701381926

4.关于线程安全的问题

参照下面园内博友一篇文章

http://www.cnblogs.com/kunpengit/archive/2011/11/18/2254280.html

5.关于强引用和弱引用问题(回头写)

题外话:居然不知道接口不能实例化,对自己无语。。。

原文地址:https://www.cnblogs.com/ChengDongSheng/p/3392469.html