0
点赞
收藏
分享

微信扫一扫

Java 深入学习(5) —— 字符串


String 对象不可变

String 类中每一个看起来会修改 String 值的方法,实际上都是创建了一个新的 String 对象,以包含修改后的字符串内容。

public class TestString {  
    static String upcase(String s){  
        return s.toUpperCase();  
    }  
    public static void main(String[] args) {  
        String s = "bbc";  
        String ss = upcase(s);  
        System.out.println(s);  
    }  
}

这里虽然s传参给了upcase方法,但是s并没有改变,传参的时候,s会复制一份引用,但是引用所指的对象却没有移动过。

重载“+”与 StringBuilder

重载的意思是,一个操作符在应用于特定的类时,被赋予了特殊的意义(用于String的“+”和“+=”是Java中仅有的两个重载过的操作符)。

操作符 “+” 可以用来连接 String。

StringBuilder 提供了丰富而全面的方法,包括insert() replace() substring() reverse() ,但最常用的还是 append()toString()delete()

StringBuffer    delete(int start, int end)
Removes the characters in a substring of this sequence.

StringBuffer    deleteCharAt(int index)
Removes the char at the specified position in this sequence.

String  toString()
Returns a string representing the data in this sequence.

StringBuffer

Reference

《Java编程思想》 第13章 字符串


举报

相关推荐

0 条评论