package test;
/**
* JDK1.8测试
* @Description:(intern之String==比较;实际中千万不要==比较,推荐equals)
* @author: HeShengjin
* @date: 2021年6月30日 下午4:05:48
* @Copyright:
*/
public class StringTest {
public static void main(String[] args) {
String s1 = "he" + "llo";//结果:常量池
String s1_1 = "hello";//结果:常量池
System.out.println(String.format("一:%s", s1 == s1_1));// true
String s1_1_1 = s1_1.intern();//结果:常量池
System.out.println(String.format("二:%s", s1 == s1_1_1));// true
String s2 = "h" + new String("ello");//结果:堆
String s2_2 = "h" + "ello";//结果:常量池
String s3 = s1.intern();//结果:常量池
String s4 = s2.intern();//结果:常量池
System.out.println(String.format("三:%s", s1 == s2_2));// true
System.out.println(String.format("四:%s", s1 == s3));// true
System.out.println(String.format("五:%s", s1 == s4));// true
System.out.println(String.format("六:%s", s3 == s4));// true
System.out.println(String.format("七:%s", s1_1 == s3));// true
System.out.println(String.format("八:%s", s1_1 == s4));// true
System.out.println(String.format("九:%s", s1_1_1 == s3));// true
System.out.println(String.format("十:%s", s1_1_1 == s4));// true
System.out.println(String.format("十一:%s", s1 == s2));// false
}
}
Java知识,String intern之==比较;实际中千万不要==比较,推荐equals
最新推荐文章于 2022-01-18 18:59:23 发布