Java基础复习:String类注意点

来源:互联网 发布:莫奈评价知乎 编辑:程序博客网 时间:2024/06/17 01:59
/* * String是不可变的类,一旦创建,包含在对象中的字符序列是不可变的 * 一个String对象的内容不能变,一旦内容变量该对象就变成一个新的String对象了 * String常量和基本类型常量都存放在JVM的一块独立的常量池中 * String使用private final char value[]来实现存储 */public class StingDemo {public static void main(String[] args) {String s1 = "hello";String s2 = "hello";//s1,s2指向的的是常量池中的同一块区域System.out.println(s1==s2);//true//equals()方法比较的是内容System.out.println(s1.equals(s2));//trueSystem.out.println("===================");String s3 = new String("well");String s4 = new String("well");//s3,s4指向堆中的不同地址,但其内容都定位于常量池中的同一块区域System.out.println(s3==s4);//falseSystem.out.println(s3.equals(s4));//true}}

原创粉丝点击