Java-String

1.

Person p1 = new Person();
p1.name = “Tom";//被放入常量池中

Person p2 = new Person();
p2.name = “Tom";

System.out.println(p1.name.equals( p2.name)); //true
System.out.println(p1.name == p2.name); //true
System.out.println(p1.name == "Tom"); //true

2.

String s1 = "javaEE";
String s2 = "javaEE";
String s3 = new String("javaEE");
String s4 = new String("javaEE");

System.out.println(s1 == s2);//true
System.out.println(s1 == s3);//false
System.out.println(s1 == s4);//false
System.out.println(s3 == s4);//false

3.

String s1 = "hello";
String s2 = "world";
String s3 = "hello" + "world";
String s4 = s1 + "world";
String s5 = s1 + s2;
String s6 = (s1 + s2).intern();

System.out.println(s3 == s4);//false
System.out.println(s3 == s5);//false
System.out.println(s4 == s5);//false
System.out.println(s3 == s6);//true

结论:

(1)常量+常量:结果是常量池。且常量池中不会存在相同内容的常量。

(2)常量与变量 或 变量与变量:结果在堆中

(3)拼接后调用intern方法:返回值在常量池中

4.

    final String s1 = "hello";
    final String s2 = "world";
    String s3 = "helloworld";
    
    String s4 = s1 + "world";
    String s5 = s1 + s2;
    String s6 = "hello" + "world";
        
    System.out.println(s3 == s4);//true
    System.out.println(s3 == s5);//true
    System.out.println(s3 == s6);//true

5.

public class TestString {
    public static void main(String[] args) {
        String str = "hello";
        String str2 = "world";
        String str3 ="helloworld";
        
        String str4 = "hello".concat("world");
        String str5 = "hello"+"world";
        
        System.out.println(str3 == str4);//false
        System.out.println(str3 == str5);//true
    }
}

concat方法拼接,哪怕是两个常量对象拼接,结果也是在堆

6.

public class StringTest {

    String str = new String("good");
    char[] ch = { 't', 'e', 's', 't' };

    public void change(String str, char ch[]) {
        str = "test ok";
        ch[0] = 'b';
    }
    public static void main(String[] args) {
        StringTest ex = new StringTest();
        ex.change(ex.str, ex.ch);
        System.out.print(ex.str + " and ");//good and
        System.out.println(ex.ch);//best
    }
}

最终输出为good and best

为何传递引用数据类型str,而str的值没有变更呢?

Java字符串的参数传递为何没改变原本的值_java中string作为参数传入函数中修改值,原来的值不变-优快云博客

7.

@Test
public void test01() throws Exception {
    String str = "中国";
    System.out.println(str.getBytes("ISO8859-1").length);// 2
    // ISO8859-1把所有的字符都当做一个byte处理,处理不了多个字节
    System.out.println(str.getBytes("GBK").length);// 4 每一个中文都是对应2个字节
    System.out.println(str.getBytes("UTF-8").length);// 6 常规的中文都是3个字节

    /*
     * 不乱码:(1)保证编码与解码的字符集名称一样(2)不缺字节
     */
    System.out.println(new String(str.getBytes("ISO8859-1"), "ISO8859-1"));// 乱码
    System.out.println(new String(str.getBytes("GBK"), "GBK"));// 中国
    System.out.println(new String(str.getBytes("UTF-8"), "UTF-8"));// 中国
}

高频面试题:

  • 区分String、StringBuffer、StringBuilder

    • String:不可变的字符序列; 底层使用char[]数组存储(JDK8.0中)

    • StringBuffer:可变的字符序列;线程安全(方法有synchronized修饰),效率低;底层使用char[]数组存储 (JDK8.0中)

    • StringBuilder:可变的字符序列; jdk1.5引入,线程不安全的,效率高;底层使用char[]数组存储(JDK8.0中)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值