---
title: "java.lang.String总结"
author: "mingren135"
date: "2013-10-26T14:41:47Z"
tags: []
categories: ["移动开发"]
slug: "java-lang-string"
draft: false
lastmod: "2013-10-26T15:44:21Z"
id: 1965196
url: "https://mingren135.iteye.com/blog/1965196"
---

一、String类的基本特征

1、final的，不可被继承。public final class String

2、本质是字符数组char\[]，并且其值不可改变。private final char value\[]

3、有个特殊的创建方式，就是直接指定String x = "abc"，"abc"就表示一个字符串对象。而x是"abc"对象的地址，也叫做"abc"对象的引用。

4、可以通过“+”串联，串联后会生成新的字符串，也可以通过concat()来串联。

5、Jvm会维护一个String Pool，用来存放运行时产生的各种字符串，并且池中的字符串的内容不重复。

最特殊的地方就是有2个地方可以存储，String pool和heap，不同的生成方式存储的地方也不一样。

 

二、String的创建原理

原理1：当使用任何方式来创建一个字符串对象s时，JVM会拿着这个s的值在pool中找是否存在内容相同的字符串对象，如果不存在，则在池中创建一个字符串s，否则，不在池中添加。

原理2：只要使用new关键字来创建对象，则一定会（在堆区或栈区）创建一个新的对象。

原理3：使用直接指定或者使用纯字符串串联来创建String对象，则仅仅会检查维护String池中的字符串，池中没有就在池中创建一个，有则罢了！但绝不会在堆栈区再去创建该String对象。 

原理4：使用包含变量的表达式来创建String对象，则不仅会检查pool，还会在堆栈区创建一个对象。

\----------------------

另外

1）intern()方法是一个本地方法，定义为public native String intern();

intern()方法的价值在于让开发者能将注意力集中到String池上。当调用 intern 方法时，如果池已经包含一个等于此 String 对象的字符串（该对象由 equals(Object) 方法确定），则返回池中的字符串。否则，将此 String 对象添加到池中，并且返回此 String 对象的引用。

2）concat()：Concatenates the specified string to the end of this string. 

If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created。当参数字符串长度为0，则返回对象本身；否则，堆中创建一个新的对象

 

三、比较字符串

```
// 在池中和堆中分别创建String对象"abc",s1指向堆中对象
String s1 = new String("abc");

// s2直接指向池中对象"abc"
String s2 = "abc";

// 在堆中新创建"abc"对象，s3指向该对象
String s3 = new String("abc");

// 在池中创建对象"ab" 和 "c"，并且s4指向池中对象"abc"
String s4 = "ab" + "c";

// c指向池中对象"c"
String c = "c";

// 在堆中创建新的对象"abc"，并且s5指向该对象
String s5 = "ab" + c;

String s6 = "ab".concat("c");
String s7 = "ab".concat(c);

System.out.println("------------实串-----------");
System.out.println(s1 == s2); // false
System.out.println(s1 == s3); // false
System.out.println(s2 == s3); // false
System.out.println(s2 == s4); // true
System.out.println(s2 == s5); // false
System.out.println(s2 == s6); // false
System.out.println(s2 == s7); // false
System.out.println("-------------------------");

String b1 = new String("");
String b2 = "";
String b3 = new String("");
String b4 = "".intern();
String b5 = "" + "";
String b6 = "".concat("");
String b7 = "  ".trim();
String b8 = "  ";
String b9 = "    ".trim();
System.out.println("------------空串-----------");
System.out.println(b1 == b2); // false
System.out.println(b1 == b3); // false
System.out.println(b2 == b3); // false
System.out.println(b2 == b4); // true
System.out.println(b2 == b5); // true
System.out.println(b2 == b6); // true
System.out.println(b2 == b7); // false
System.out.println("-----a----");
System.out.println(b2.equals(b7)); // true*
System.out.println(b7 == b8); // false
System.out.println(b7 == b9); // false
System.out.println(b7.equals(b9)); // true
System.out.println(b9 == null);// false
```

 

四、字符串转码

1）转一个码，又用该码来构建一个字符串，是绝对不会出现乱码的，相当于没转。

2）转码与否，与字符串本身编码有关，字符串本身的编码与谁有关？文件编码，或者你的IDE设置的编码有关。文件已经是UTF-8了，你非要转为GBK，不乱才怪，呵呵

```
public class TestEncoding {
	public static void main(String[] args) throws UnsupportedEncodingException {
		System.out.println("转码前，输出Java系统属性如下：");
		System.out
				.println("user.country:" + System.getProperty("user.country"));
		System.out.println("user.language:"
				+ System.getProperty("user.language"));
		System.out.println("sun.jnu.encoding:"
				+ System.getProperty("sun.jnu.encoding"));
		System.out.println("file.encoding:"
				+ System.getProperty("file.encoding"));

		System.out.println("---------------");
		String s = "哈哈";
		String s1 = new String(s.getBytes(), "UTF-8");
		String s2 = new String(s.getBytes("UTF-8"), "UTF-8");
		String s3 = new String(s.getBytes("UTF-8"));
		String s4 = new String(s.getBytes("UTF-8"), "GBK");
		String s5 = new String(s.getBytes("GBK"));
		String s6 = new String(s.getBytes("GBK"), "GBK");
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s3);
		System.out.println(s4);
		System.out.println(s5);
		System.out.println(s6);
	}
}
```

 

 五、案例

通过内存监控发现，GC的动作比较频繁，偶然发现了大量如下代码：log.debug(“userId=” + user.getUserId())

原因分析：

以上代码执行时，分两步：

1\. 先执行的是括号中的字符串相”+”的动作，而每次”+”运算都会导致新字符串的生成，这样就产生了很多“中间字符串”，在极大次数被调用时，这种字符串被创建和销毁的数量非常庞大，从而造成了jvm gc频繁执行，进而影响了性能。

2\. 再执行log.debug()函数，在生产环境log level一般大于info，所以实际不会打印debug信息 综上所述，这些代码在生产环境不会产生日志，但会执行字符串”+”运算，而这些运算是无意义的，所以需要先判断日志的优先级，方式是log.isXXXEnabled() { log.XXX(……); }

Log Level的级别： Fatal-&gt;error-&gt;warn-&gt;info-&gt;debug，级别从高到低

一般我们生成环境的log level都是error，所以对于Error以上级别的日志，不用判断；对于error以下级别的都要加上判断。