这里简单的理解一下java中关于string字面量的知识,关于字节码可以使用java自带的javap工具查看。

string字面量

一、直接贴出测试的代码

A string literal always refers to the same instance of class String.

package com.linux.huhx.stringTest;
/**
* Created by huhx on 2017-05-12.
*/
class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((com.linux.huhx.Other.hello == hello) + " "); // 这个和上面的Other在不同的包内。内容一样
System.out.print((hello == ("Hel" + "lo")) + " ");
System.out.print((hello == ("Hel" + lo)) + " "); // false
System.out.print((hello == ("Hel" + lo).intern()) + " ");
System.out.println(hello == new String("hello")); // false
}
} class Other {
static String hello = "Hello";
} // true true true true false true false

二、关于String的intern方法官方文档的解释

// 一个字符串池,最初是空的,是由类字符串私有维护的。
、A pool of strings, initially empty, is maintained privately by the class String. // 如果常量池中已经有了这个字符串,那么直接返回常量池中它的引用,如果没有,那就将它的引用保存一份到字符串常量池,然后直接返回这个引用。
、When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned. 、It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

友情链接

最新文章

  1. redis数据结构详解之Hash(四)
  2. ManagementClass类解析和C#如何获取硬件的相关信息
  3. 深入理解Java虚拟机(一)、Java内存区域与内存溢出异常
  4. 【读书笔记】读《JavaScript模式》 - 对象创建模式
  5. 没有必要去指定SqlSessionFactory或SqlSessionTemplate
  6. Java基础——集合框架
  7. 浅析CSS负边距
  8. hdu1054(二分图匹配)
  9. VC6项目移植到VS2008的若干问题——好的代码,从我做起,从如今做起。
  10. [转]如何正确清理C盘
  11. css2--垂直对齐
  12. SDP(3):ScalikeJDBC- JDBC-Engine:Fetching
  13. [leetcode](4.21)2. 按字典序排列最小的等效字符串
  14. spring boot+spring data jpa+gradle+mysql配置问题
  15. 使用C语言开发PHP扩展(转)
  16. Go随机数
  17. Cash Machine POJ - 1276 多重背包二进制优化
  18. 2分钟理解文件IO -我对文件IO的理解与实验对比
  19. Python 类的祖宗--metaclass
  20. 破解Unity5.3.4f1

热门文章

  1. SQL SERVER 2005允许自定义聚合函数
  2. 在文件夹右键菜单里添加“DOS 到这里”这个菜单项
  3. C# ManualResetEvent
  4. vim手记
  5. C++ 简单的日志类
  6. jvm垃圾收集器之Throughput GC
  7. Linksys WRT54G2 V1刷ddwrt注意事项
  8. 一款基于css3和jquery实现的动画弹出层
  9. Web app root system property already set to different value 错误原因及解决
  10. 矩阵hash + KMP - UVA 12886 The Big Painting