java基础---->string字面量的使用
这里简单的理解一下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字面量的使用的更多相关文章
- 【Java基础】字面量相加的类型转换
Java字面量的相加类型转换 1.Java 编译期间(javac),凡是字面量和常量的运算,都会先运算出结果 2.运行期当字符串池中有 String"字面量"时,Java 会直接用 ...
- Java基础String的方法
Java基础String的方法 字符串类型写法格式如下: 格式一: String 变量名称; 变量名称=赋值(自定义或传入的变量值); 格式二: String 变量名称=赋值(自定义或传入的变量值); ...
- Java基础 String 裸暴力算法- 五个小练习
之间的博客,承上启下: Java基础 String/StringBuff 常用操作方法复习/内存分析 Java数组直接选择排序.sort()排序 Java基础 String 算法 - 五个练 ...
- Java基础—String构造方法
Java基础--String构造方法 public String(): 创建一个空表字符串对象,不包含任何内容 public String(char[]chs): 根据字符数组的内容,来创建字符串对象 ...
- Java基础-String、StringBuffer、StringBuilder
看下面这段代码: public class Main { public static void main(String[] args) { String string = ""; ...
- Java中的字面量
在计算机科学中,字面量(literal)是用于表达源代码中一个固定值的表示法(natation).几乎所有计算机编程语言都具有对基本值的字面量表示,诸如:整数.浮点数以及字符串:而有很多也对布尔类型和 ...
- Java基础——String
前言 从去年八月末开始工作一年了,有了大半年的java开发经验,自认为比在大学时期编码能力强了很多,但是基础方面概念模糊的地方感觉越来越多了 (:´д`)ゞ 所以,我准备把这些问题以及工作中遇到的问题 ...
- [Java基础]——String类
此篇博客主要整理Java中的String类的使用. 一.String 1.1 String 的定义 上图是jdk中对String类的定义,得到的信息有: ①.String类声明为final的, ...
- String字面量
public class assa{ static String ee = "aa";//ee指向常量池中的aa static String ff = new String(&qu ...
随机推荐
- 非变动性算法源代码分析与使用示例( for_each、min_element 、find_if、search 等)
非变动性算法代码分析与示例: 一.for_each C++ Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // TEMPLATE FUNCTION for_eac ...
- 迅为4412开发板Linux驱动教程——总线_设备_驱动注冊流程具体解释
视频下载地址: 驱动注冊:http://pan.baidu.com/s/1i34HcDB 设备注冊:http://pan.baidu.com/s/1kTlGkcR 总线_设备_驱动注冊流程具体解释 • ...
- 怎样给ExecutorService异步计算设置超时
ExecutorService接口使用submit方法会返回一个Future<V>对象.Future表示异步计算的结果.它提供了检查计算是否完毕的方法,以等待计算的完毕,并获取计算的结果. ...
- C# “预先生成事件命令行”和“后期生成事件命令行”
概述 Visual studio 项目允许在项目属性生成事件一栏中指定预先生成和后期生成事件来实现项目生成与部署的自动化. 实例1: 我自己写了一个调试工具,该工具处于一边开发一边使用过程中.实际工作 ...
- js--在页面元素上(移动到或获取焦点)、鼠标离开(或失去焦点)
1.onfocus() 和 onblur() 是一对相反的事件 但是他们只支持一部分标签 W3C关于onblur的描述:http://www.w3school.com.cn/jsref/event_o ...
- hibernate中一对一映射
一.hibernate中一对一映射有两种 1 主键方式,一张表的主键是通过另一张表的主键生成的 2 外键方式,一张表添加外键引用另一张表的主键,并添加唯一unique约束 二.下面进行简单例子,用户和 ...
- django之整体复习
1. 配置文件: media: avatar = models.FileField(upload_to='avatar') sessiongs MEDIA_ROOT=os.path.join(BASE ...
- [Jobdu] 题目1544:数字序列区间最小值
题目描述: 给定一个数字序列,查询任意给定区间内数字的最小值. 输入: 输入包含多组测试用例,每组测试用例的开头为一个整数n(1<=n<=100000),代表数字序列的长度.接下去一行给出 ...
- 分享一个mysql 复杂查询的例子
发布:脚本学堂/MySQL 编辑:thebaby 2013-08-23 09:37:37 [大 中 小] 有关mysql复杂查询的一个例子,正在学习mysql的朋友,可以作为一个参考. 在my ...
- FreeRtos——空闲任务与空闲任务钩子函数
以下基础知识转载自正点原子PDF资料. 前面例子 中创建的任务大部份时间都处于阻塞态.这种状态下所有的任务都不可运行,所以也不能被调度器选中.但处理器总是需要代码来执行——所以至少要有一个任务处于运行 ...