您当前的位置: 首页 >  Java

ITKEY_

暂无认证

  • 0浏览

    0关注

    732博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java那些奇奇怪怪的面试题(持续补充中)

ITKEY_ 发布时间:2021-01-07 22:23:06 ,浏览量:0

收集一些我容易出错是的面试题,持续更新中。

String字符串常量池考点
public static void main(String args[]) {
        String strA = "yootk.com" ; // 字符串对象
        String strB = "yootk.com" ; // 直接赋值,内容相同
        System.out.println(strA == strB) ;	// 对象地址数值比较
        System.out.println(strA == "yootk.com") ; // 与匿名String对象进行地址数值比较
    }

运行结果:

true
true

"yootk.com"形式声明字符串,为了性能优化Java里会有个字符串常量池,“”相同的内容不会产生新的对象。

public static void main(String args[]) {
        String strA = "yootk.com" ; // 直接赋值
        String strB = new String("yootk.com").intern() ; // 手工入池
        System.out.println(strA == strB) ; // 地址数值比较
    }

运行结果:

true

同样是String 常量池。

public static void main(String args[]) {
        String strA = "www.yootk.com" ; // 字符串常量
        String strB = "www." + "yootk." + "com" ; // 字符串常量连接
        System.out.println(strA == strB) ;
    }

运行结果:

true

虽然是字符串拼接,但是在javac编译的时候,已经入了常量池。

public static void main(String args[]) {
        String company = "yootk" ;
        String strA = "www.yootk.com" ; // 字符串常量
        String strB = "www." + company + ".com" ; // 字符串常量连接
        System.out.println(strA == strB) ;
    }

运行结果:

false

此外拼接与上一题不同,这里里的编译阶段没有进入常量池。strB由变量拼接而成是一个全新的对象。

Integer对象==比较(小于128的数值)
public static void main(String[] args) {
        Integer numA = 127 ;
        Integer numB = 127 ;
        Integer numC = Integer.valueOf(127) ;
        Integer numD = new Integer(127) ;       //带有new肯定是一个全新的对象
        System.out.println(numA == numB) ;	// 对象比较
        System.out.println(numA == numC) ;	// 对象比较
        System.out.println(numA == numD) ;	// 对象比较
    }

运行结果:

true
true
false

Integer小于128时直接赋值比较,返回的是true。 这题目比较容易回答错误,没看到这题之前我以为全是 false.

原理我不清楚,先死记吧,难道跟 128位有关系,不可能这么巧合吧。难道小于 128的数字类似于String的常量池?

Integer对象==比较(大于等于128的数值)
 public static void main(String[] args) {
        Integer numA = 128 ;
        Integer numB = 128 ;
        System.out.println(numA == numB) ;	// 对象比较
        System.out.println(numA.equals(numB)) ;	// 对象比较
    }

运行结果:

false
true

这个给我印象中是一样的。比较好理解。

finally中的return

在这里插入图片描述

代码:

class Message{
    public static String echo(String message){
        try {
            return "【Try】"+message;
        } finally {
            return "【Finally】"+message;
        }

    }
}

public class 异常处理Finally中的return {
    public static void main(String[] args) {
        System.out.println(Message.echo("发送消息!"));
    }
}

运行结果:

【Finally】发送消息!

这个题我错了,原来是Finnally中的return最终生效了。

匿名内部类
class Book {
    public String toString(){
        return "【Book】书中自有颜如玉。";
    }
}

public class 匿名内部类 {
    public static void main(String[] args) {
        Book book1 = new Book();
        System.out.println(book1);

        Book book2 = new Book() {       //匿名子类
            public String toString() {
                return "【Book】书中自有黄金屋。";
            }
        };
        System.out.println(book2);
    }
}

运行结果:

【Book】书中自有颜如玉。
【Book】书中自有黄金屋。
关注
打赏
1665243900
查看更多评论
立即登录/注册

微信扫码登录

0.0452s