final和String的关系

我们在编写代码时,经常用到final关键字,这是java的保留字。

  1. 修饰类时,表明这个类不能被继承。
    String a="hello2";
    String b="hello"+2;
    System.out.println("a==c-->  "+(a==b));

    String c="hello";
    final String d="hello";

    String e=c+2;
    String f=d+2;
    System.out.println("a==e-->  "+(a==e));   
    System.out.println("a==f-->  "+(a==f));

    out:
        a==c-->  true
        a==e-->  false
        a==f-->  true

你们发现什么规律了?

猜你喜欢

转载自blog.csdn.net/lvoelife/article/details/81165730
今日推荐