字符串特性
查看字节码
javap
javap -c com.terwergreen.str.StringNewTest
java 源代码文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
public class StringNewTest {
public static void main(String[] args) {
String str = new String("abc");
}
}
字节码:文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
➜ javase-test git:(master) ✗ javap -c -constants com.terwergreen.str.StringNewTest
Compiled from "StringNewTest.java"
public class com.terwergreen.str.StringNewTest {
public com.terwergreen.str.StringNewTest();
Code:
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: new #16 // class java/lang/String
3: ldc #18 // String abc
5: invokespecial #20 // Method java/lang/String."<init>":(Ljava/lang/String;)V
8: return
}
idea
选中 .class
文件,View->Show bytecode
文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
字符串池
String Pool(字符串池)文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
-
String s = "aaa";
采用字面值的方式赋值- 查找 Spring Pool 中是否存在 "aaa" 这个对象,如果不存在,则在 Spring Pool 中创建一个 "aaa" 对象,然后将 Spring Pool 中的这个 "aaa" 对象的地址返回来,赋给变量 s ,这样 s 会指向 String Pool 中的这个 "aaa" 字符串对象。
- 如果存在,则不创建任何对象,直接将 Spring pool 中的这个 "aaa" 对象地址返回来,赋给 s 引用。
- String s = new String("aaa");
- 首先在 String Pool 中查找有没有 "aaa" 这个字符串对象,如果有,则不在 String Pool 中再去创建 "aaa" 这个对象了,直接在堆中(heap)创建一个 "aaa" 字符串对象,然后将堆中的这个 "aaa" 字符串对象返回来,付给 s 引用,导致 s 指向了堆中创建的这个 "aaa" 字符串对象。
- 如果没有,则首先在 String Pool 中创建一个 "aaa" 对象,然后再在堆中(heap)创建一个 "aaa" 对象,然后将堆中的这个 "aaa" 对象返回来,付给 s 引用,导致 s 指向了堆中所创建的这个 "aaa" 对象。
intern
参考文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-StringLiteral文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
参考
各个版本的 JDK 文档下载地址文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
Links to JDK documentation文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
Java SE | Download | Web | Other |
---|---|---|---|
19 (current) | Downloads page | Javadoc | Doc home |
18 | Downloads page | Javadoc | Doc home |
17 (LTS) | Downloads page | Javadoc | Doc home |
16 | no longer available | Javadoc | Doc home |
15 | no longer available | Javadoc | Doc home |
14 | no longer available | Javadoc | Doc home |
13 | no longer available | Javadoc | Doc home |
12 | no longer available | Javadoc | Doc home |
11 (LTS) | Downloads page | Javadoc | Doc home |
10 | no longer available | Javadoc | Doc home |
9 | no longer available | Javadoc | Doc home |
8 (LTS) | Downloads page | Javadoc | Platform home Doc home |
7 | no longer available | Javadoc | Doc home |
6 | no longer available | Javadoc | Doc home |
By the way, a history of Java SE versions.文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
历史版本文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
https://javadoc.allimant.org/文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
文章源自浅海拾贝-https://blog.terwergreen.com/analysis-of-the-string-class-and-its-source-code-of-java_se-bf1cv.html
- 扫码加我微信
- 验证消息请输入:来自你的博客
- 我的微信公众号
- 微信扫一扫与我交流吧
评论