Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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 }}
Uh oh!
There was an error while loading.
Please reload this page
.
JavaDevTeam
/
notes
Public
Notifications
You must be signed in to change notification settings
Fork
64
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
notes
/
JAVASE
/
StringBuffer.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
42 lines (42 loc) · 1.67 KB
master
Breadcrumbs
notes
/
JAVASE
/
StringBuffer.java
Copy path
Top
File metadata and controls
Code
Blame
42 lines (42 loc) · 1.67 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
关于JAVA的
StringBuffer
适合多线程
————
线程同步
,
安全
StringBuffer
是字符串缓冲区
,
一个容器
。
而且长度是可变化的
。
1
,
该容器的长度是可变的
。
2
,
可以操作多个数据类型
。
3
,
最终会通过toString方法变成字符串
。
————
当数据类型不确定
,
当个数不确定
、
最后都要变成字符串使用的
。
就可以考虑使用缓冲区
。
【
作为数据的存储
,
一般是为了在最后用toString变成字符串使用
】
StringBuffer
sb
=
new
StringBuffer
();
①
存储
sb
.
append
(
x
); ————
含大部分重载函数
|--
将指定的数据
,
x
,
添加到已有数据的结尾处
。
返回的还是本类对象
(
StringBuffer
);
sb
.
insert
(
1
,
x
); ————
含大部分重载函数
|--
将指定的数据x
,
插入到sb的1角标位置
。
其余的往后延伸
。
还是返回该类对象
(
StringBuffer
);
如果x角标不存在
。
运行的时候会出现角标越界
。
②
删除
sb
.
delete
(
x
,
y
);
|--
删除缓冲区中x
-
y位置的数据
!
包含x不包含y
。
返回的还是该类对象
(
StringBuffer
);
sb
.
deleteCharAt
(
x
);
|--
删缓冲区中指定位置
。
x处的数据
。
返回的还是该类对象
(
StringBuffer
);
③
获取
sb
.
charAt
(
x
);
|--
通过角标获取字符串
。
sb
.
indexOf
(
x
)
|--
通过字符串获取位置
。
sb
.
substring
(
x
,
y
);
|--
从x开始y结束
。
不包含y
。
返回的是一个String
(
String
);
sb
.
getChars
(
x
,
y
,
chs
,
z
);
|--
把sb容器里面x到y的数据
。
存放到chs容器里面
,
从z位置开始存放
。
将缓冲区中的指定数据
(
包含头部包含尾
)。
存储到指定数组中
。
④
修改
sb
.
replace
(
x
,
y
,
str
);
|--
把容器里面从x开始到y结束
(
不包含y
)
的内容替换成str
(
StringBuffer
);
sb
.
setCharAt
(
x
,
str
);
|--
把容器里面x位置的数据替换成str
。
没有返回值
。
运行完就完事儿
(
void
);
⑤
反转
sb
.
reverse
();
|--
反转整个容器
。
返回的还是该类对象
(
StringBuffer
);
-----------------------------------------------------------------------------------------------
关于JAVA的
StringBuilder
适合单线程
,
如果非要应用于多线程
。
需要自己加锁
synchronized
————
线程不同步
,
线程不一定安全
。
在JDK1
.5
版本
(
较新
)
之后才出现了
StringBuilder
。
You can’t perform that action at this time.