Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
toBeTopJavaer/basics/java-basic/substring.md at master · Aloner/toBeTopJavaer · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Aloner
toBeTopJavaer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
toBeTopJavaer
/
basics
/
java-basic
/
substring.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
90 lines (53 loc) · 4.37 KB
master
Breadcrumbs
toBeTopJavaer
/
basics
/
java-basic
/
substring.md
Copy path
Top
File metadata and controls
Code
Blame
90 lines (53 loc) · 4.37 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
String是Java中一个比较基础的类
,
每一个开发人员都会经常接触到
。
而且
,
String也是面试中经常会考的知识点
。
String有很多方法
,
有些方法比较常用
,
有些方法不太常用
。
今天要介绍的subString就是一个比较常用的方法
,
而且围绕subString也有很多面试题
。
`
substring
(
int
beginIndex
,
int
endIndex
)`
方法在不同版本的JDK中的实现是不同的
。
了解他们的区别可以帮助你更好的使用他
。
为简单起见
,
后文中用
`
substring
()`
代表
`
substring
(
int
beginIndex
,
int
endIndex
)`
方法
。
##
substring
()
的作用
`
substring
(
int
beginIndex
,
int
endIndex
)`
方法截取字符串并返回其
[
beginIndex
,
endIndex
-
1
]
范围内的内容
。
String
x
=
"abcdef"
;
x
=
x
.
substring
(
1
,
3
);
System
.
out
.
println
(
x
);
输出内容
:
bc
##
调用substring
()
时发生了什么
?
你可能知道
,
因为x是不可变的
,
当使用
`
x
.
substring
(
1
,
3
)`
对x赋值的时候
,
它会指向一个全新的字符串
:
![
string
-
immutability1
][
1
]
然而
,
这个图不是完全正确的表示堆中发生的事情
。
因为在jdk6
和
jdk7中调用substring时发生的事情并不一样
。
##
JDK
6
中的substring
String是通过字符数组实现的
。
在jdk
6
中
,
String类包含三个成员变量
:`char
value
[]`, `int
offset
`,`int
count
`。
他们分别用来存储真正的字符数组
,
数组的第一个位置索引以及字符串中包含的字符个数
。
当调用substring方法的时候
,
会创建一个新的string对象
,
但是这个string的值仍然指向堆中的同一个字符数组
。
这两个对象中只有count和offset
的值是不同的
。
![
string
-
substring
-
jdk6
][
2
]
下面是证明上说观点的Java源码中的关键代码
:
//JDK 6
String
(
int
offset
,
int
count
,
char
value
[]) {
this
.
value
=
value
;
this
.
offset
=
offset
;
this
.
count
=
count
;
}
public
String
substring
(
int
beginIndex
,
int
endIndex
) {
//check boundary
return
new
String
(
offset
+
beginIndex
,
endIndex
-
beginIndex
,
value
);
}
##
JDK
6
中的substring导致的问题
如果你有一个很长很长的字符串
,
但是当你使用substring进行切割的时候你只需要很短的一段
。
这可能导致性能问题
,
因为你需要的只是一小段字符序列
,
但是你却引用了整个字符串
(
因为这个非常长的字符数组一直在被引用
,
所以无法被回收
,
就可能导致内存泄露
)。
在JDK
6
中
,
一般用以下方式来解决该问题
,
原理其实就是生成一个新的字符串并引用他
。
x
=
x
.
substring
(
x
,
y
) +
""
关于JDK
6
中subString的使用不当会导致内存系列已经被官方记录在Java
Bug
Database中
:
<
img
src
=
"http://www.hollischuang.com/wp-content/uploads/2016/03/leak.png"
alt
=
"leak"
width
=
"1089"
height
=
"744"
class
=
"aligncenter size-full wp-image-2660"
/>
>
内存泄露
:
在计算机科学中
,
内存泄漏指由于疏忽或错误造成程序未能释放已经不再使用的内存
。
内存泄漏并非指内存在物理上的消失
,
而是应用程序分配某段内存后
,
由于设计错误
,
导致在释放该段内存之前就失去了对该段内存的控制
,
从而造成了内存的浪费
。
##
JDK
7
中的substring
上面提到的问题
,
在jdk
7
中得到解决
。
在jdk
7
中
,
substring方法会在堆内存中创建一个新的数组
。
![
string
-
substring
-
jdk7
][
3
]
Java源码中关于这部分的主要代码如下
:
//JDK 7
public
String
(char
value
[], int
offset
, int
count
) {
//check boundary
this
.
value
=
Arrays
.
copyOfRange
(
value
,
offset
,
offset
+
count
);
}
public
String
substring
(
int
beginIndex
,
int
endIndex
) {
//check boundary
int
subLen
=
endIndex
-
beginIndex
;
return
new
String
(
value
,
beginIndex
,
subLen
);
}
以上是JDK
7
中的subString方法
,
其使用
`
new
String
`
创建了一个新字符串
,
避免对老字符串的引用
。
从而解决了内存泄露问题
。
所以
,
如果你的生产环境中使用的JDK版本小于1
.7
,
当你使用String的subString方法时一定要注意
,
避免内存泄露
。
[
1
]:
http
:
//www.programcreek.com/wp-content/uploads/2013/09/string-immutability1-650x303.jpeg
[
2
]:
http
:
//www.programcreek.com/wp-content/uploads/2013/09/string-substring-jdk6-650x389.jpeg
[
3
]:
http
:
//www.programcreek.com/wp-content/uploads/2013/09/string-substring-jdk71-650x389.jpeg
You can’t perform that action at this time.