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

106 篇文章 1 订阅

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

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】书中自有黄金屋。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值