这里简单的理解一下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.

友情链接

java基础---->string字面量的使用的更多相关文章

  1. 【Java基础】字面量相加的类型转换

    Java字面量的相加类型转换 1.Java 编译期间(javac),凡是字面量和常量的运算,都会先运算出结果 2.运行期当字符串池中有 String"字面量"时,Java 会直接用 ...

  2. Java基础String的方法

    Java基础String的方法 字符串类型写法格式如下: 格式一: String 变量名称; 变量名称=赋值(自定义或传入的变量值); 格式二: String 变量名称=赋值(自定义或传入的变量值); ...

  3. Java基础 String 裸暴力算法- 五个小练习

      之间的博客,承上启下:    Java基础 String/StringBuff 常用操作方法复习/内存分析 Java数组直接选择排序.sort()排序 Java基础 String 算法 - 五个练 ...

  4. Java基础—String构造方法

    Java基础--String构造方法 public String(): 创建一个空表字符串对象,不包含任何内容 public String(char[]chs): 根据字符数组的内容,来创建字符串对象 ...

  5. Java基础-String、StringBuffer、StringBuilder

    看下面这段代码: public class Main { public static void main(String[] args) { String string = ""; ...

  6. Java中的字面量

    在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(natation).几乎所有计算机编程语言都具有对基本值的字面量表示,诸如:整数.浮点数以及字符串:而有很多也对布尔类型和 ...

  7. Java基础——String

    前言 从去年八月末开始工作一年了,有了大半年的java开发经验,自认为比在大学时期编码能力强了很多,但是基础方面概念模糊的地方感觉越来越多了 (:´д`)ゞ 所以,我准备把这些问题以及工作中遇到的问题 ...

  8. [Java基础]——String类

    此篇博客主要整理Java中的String类的使用. 一.String    1.1  String 的定义 上图是jdk中对String类的定义,得到的信息有: ①.String类声明为final的, ...

  9. String字面量

    public class assa{ static String ee = "aa";//ee指向常量池中的aa static String ff = new String(&qu ...

随机推荐

  1. C#:文件操作(待补充)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  2. NPM 模块收集

    cross-env https://www.npmjs.com/package/cross-env 在package.json设置环境变量的时候,会有兼容性问题,如: { "scripts& ...

  3. Atitit.异步编程 java .net php python js 的比较

    Atitit.异步编程 java .net php python js 的比较 1. 1.异步任务,异步模式,  APM模式,,  EAP模式, TAP 1 1.1.       APM模式: Beg ...

  4. Decoration2:引入Angularjs显示前台一条数据

    SpringMVC内置的RestFul API格式采用的是最复杂最全面的HATEOAS规范,对于简单应用来说,前台解析起来不方便,我们下面主要想办法重新定义一种简单的RestFulAPI. (1)先是 ...

  5. Away3D引擎学习笔记(三)模型拾取(翻译)

    原文详见http://away3d.com/tutorials/Introduction_to_Mouse_Picking.本文若有翻译不对的地方,敬请指出. 本教程详细介绍了Away3D 4.x中鼠 ...

  6. Secure REST API with oauth2 (翻译)

    http://blog.csdn.net/haiyan_qi/article/details/52384734 ******************************************** ...

  7. 跟着百度学习之ThinkPHP的认识/初窥

    MVC全称(Model View Controller) Model:模型(可以理解位数据库操作模型) View:视图(视图显示) Controller:(控制器) 简单的说框架就是一个类的集合.集合 ...

  8. 如果本身kali就在局域网,shell在外网,怎么反弹连接呢?

    kali虚拟机使用桥接,路由器端口映射监听的端口即可.

  9. MDI多文档窗体续

    private void 加载窗体_Click(object sender, EventArgs e) { Form2 frm = new Form2();//实例化 Form2 frm.MdiPar ...

  10. ubuntu boot空间不足的解决方法

    ubuntu boot空间不足的解决方法 2013年12月11日 11:11:39 yypony 阅读数:17000 标签: ubuntu内存boot内核更多 个人分类: linux_usageubu ...