string类面试题

1,String类可以被继承吗

String类定义如下

 

public final class String{...}

String类是一个final类,final类不能被继承。报错提示:The type StringDemo cannot subclass the final class String。

2,string的值为什么不能被修改

String类属性定义如下

 

private final char value[];

对象值用final字符数组保存,一旦初始化引用地址不可修改,同时String类未提供修改value值的方法,并可保证线程安全。

String类作为java中最常用的类,底层用的也是char,由此可见java中所有的操作最终还是由8种基本数据类型来完成。

3,为什么常用string作为map中的key

 

/** Cache the hash code for the string */
   private int hash; // Default to 0
   public int hashCode() {
       int h = hash;
       if (h == 0 && value.length > 0) {
           char val[] = value;

           for (int i = 0; i < value.length; i++) {
               h = 31 * h + val[i];
           }
           hash = h;
       }
       return h;
   }

string实例缓存了自己的hashcode,而在map操作中计算key的索引时会使用key的hashcode。

 

4,字符串倒序输出

String youfengxin = "youfengxin.com";
System.out.println(new StringBuilder(youfengxin).reverse().toString());

输出

moc.nixgnefuoy

5,string与char互转

String youfengxin = "youfengxin.com";
//string to char[]
char[] chars = youfengxin.toCharArray();
//char[] to string
System.out.println(new String(chars));

6,==和equals

public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

(==)是判断两个对象的引用地址refrence是否相等。对象的访问定位

equals先判断==,在判断类型,在比较每个字符,只有==true或者类型相同且每个字符都相等才返回true。

 

7,intern方法有什么用

 * When the intern method is invoked, if the pool already contains a
     * string equal to this {@code String} object as determined by
     * the {@link #equals(Object)} method, then the string from the pool is
     * returned. Otherwise, this {@code String} object is added to the
     * pool and a reference to this {@code String} object is returned.
     */
    public native String intern()

当intern()方法被调用,如果字符串池中含有一个字符串和当前调用方法的字符串eqauls相等,那么就会返回池中的字符串。如果池中没有的话,则首先将当前字符串加入到池中,然后返回引用。

 

8,String语法糖

String youfengxin = "youfengxin";
String com = ".com";
String youfengxin_com1 = youfengxin+com;
String youfengxin_com2 = "youfengxin.com";
String youfengxin_com3 = "youfengxin"+".com";
System.out.println(youfengxin_com1 == youfengxin_com2);
System.out.println(youfengxin_com1 == youfengxin_com3);
System.out.println(youfengxin_com2 == youfengxin_com3);

结果

false
false
true

但如果把youfengxin和com变量都改成常量final

final String youfengxin = "youfengxin";
final String com = ".com";
String youfengxin_com1 = youfengxin+com;
String youfengxin_com2 = "youfengxin.com";
String youfengxin_com3 = "youfengxin"+".com";
System.out.println(youfengxin_com1 == youfengxin_com2);
System.out.println(youfengxin_com1 == youfengxin_com3);
System.out.println(youfengxin_com2 == youfengxin_com3);

结果

true
true
true

常量拼接(字面量和final修饰的变量),虚拟机会直接优化成youfengxin_com2格式

变量拼接,编译后使用StringBuilder.append()拼接生成一个新的字符串

更多java类库文章阅读

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值