Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2019-11-04~2019-11-09 周分享总结 #5

Open
spx0315 opened this issue Nov 17, 2019 · 0 comments
Open

2019-11-04~2019-11-09 周分享总结 #5

spx0315 opened this issue Nov 17, 2019 · 0 comments
Labels
daily share Daily Share Summary documentation Improvements or additions to documentation question Further information is requested

Comments

@spx0315
Copy link

spx0315 commented Nov 17, 2019

2019-11-04 分享人: 姚秋鸿

题目:

public class Test {
    static Test test = new Test("3");
    static{
        System.out.println("1");
    }
    {
        System.out.println("2");
    }
    Test(String s){
        System.out.println(s);
    }
    public static void staticFunction(){
        System.out.println("4");
    }
    public static void main(String[] args) {
        staticFunction();
    }
}

运行结果:

2
3
1
4

解答:

1.static代码块

static代码块会在类被加载的时候执行,且仅会被执行一次

2.执行顺序优先级:

静态块>main()>构造块>构造方法(静态块中按定义顺序,从前往后执行)

3.运行解答

先执行第一个静态块,即

static Test test = new Test("3");

生成新的对象,由于static代码块会在类被加载的时候执行,且仅会被执行一次,所以先执行构造块,即

System.out.println("2");

然后执行构造方法

Test(String s){        System.out.println(s);
}

打印出3

紧接着执行第二个static块

static{
 System.out.println("1");
}

最后执行main()方法,打印出4

2019-11-05 分享人: 袁乙文

题目:

public class DataObject implements Serializable {
    private static int i = 0;
    private String word = " ";

    public void setWord(String word) {
        this.word = word;
    }

    public static void setI(int i) {
        DataObject.i = i;
    }

    public static void main(String[] args) {
        DataObject object = new DataObject();
        object.setWord("123");
        object.setI(2);
    }
}

将此对象序列化成文件,并在另外一个JVM中读取文件,进行反序列化,请问此时读出的DataObject对象中的word值和i的值分别为?

解答:

1.答案:

i在另一个JVM中反序列化是0,不换JVM是2,word一直是123

2.知识点:

序列化是对 对象 而非 进行的。

2019-11-06 分享人: 宋丽

题目:

class Base{
    public Base(String s){
        System.out.println("B");
    }
}
public class Dervied extends Base{
    public Dervied(String s){
        System.out.println("D");
    }
    public static void main(String[] args) {
        new Dervied("C");
    }
}

输出结果是()

A.BD

B.DB

C.C

D.编译错误

解答:

1.题目答案

D.编译错误

2.知识点

缺少super关键字

2019-11-07 分享人: 杨兴旺

题目:

1.short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
2.算出2乘以8等于几最有效率的方法是什么?
3.java中会存在内存泄漏吗,举例说明。

解答:

1.答案:

(1)第一个有错,因为常量1的默认类型为int,所以第二步为short=short+int,但是等式右侧实际会默认转换为一个int类型的结果,所以错误。第二个没错,+=,-=,++,--运算符本身就包含强制转换,转换为左侧变量值类型,即等同于s1=(short)(s1+1);故无错误。

(2)使用<<运算符,<<与>>运算符均可直接移动二进制位数,最有效率,此外还可以在一定范围内数值溢出,如二分法源码。

(3) 会。java导致内存泄露的原因很明确:长生命周期的对象持有短生命周期对象的引用就很可能发生内存泄露,尽管短生命周期对象已经不再需要,但是因为长生命周期对象持有它的引用而导致不能被回收,这就是java中内存泄露的发生场景。

2019-11-08 分享人: 韩思玥

题目:

String s = new String("xyz");

创建了几个StringObject?
A.两个或一个都有可能

B.两个

C.一个

D.三个

解答:

1.答案:

A.两个或一个都有可能

2.解析:

如果在常量池中已经存在"xyz",那么不会继续创建,只创建一个new String("xyz")的对象。如果常量池中没有,则会创建两个对象,一个是对象的值"xyz",一个是new String("xyz")的对象。

2019-11-09 分享人: 何芷靓

题目

public class NULL {
    public static void haha(){
        System.out.println("haha");
    }
    public static void main(String[] args) {
        ((NULL)null).haha();
    }
}

能正常运行吗?!

public class NULL {
    public void haha(){
        System.out.println("haha");
    }
    public static void main(String[] args) {
        ((NULL)null).haha();
    }
}

如果将haha改为实例方法还可以吗?

解答

1.原题目

可以运行,输出haha

2.改为实例方法

不可以运行,报空指针异常

3.原理分析

null值可以强制转换为任何java类类型,(String)null也是合法的。但null强制转换后是无效对象,其返回值还是为null,而static方法的调用是和类名绑定的,不借助对象进行访问所以能正确输出。反过来,没有static修饰就只能用对象进行访问,所以使用null调用对象会报空指针异常。

@GeniusDSY GeniusDSY added daily share Daily Share Summary documentation Improvements or additions to documentation question Further information is requested labels Nov 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
daily share Daily Share Summary documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants