当前位置:网站首页>String class understanding - final is immutable
String class understanding - final is immutable
2022-04-23 05:23:00 【Cx_ Xuan】
String Class understanding ----- immutable (final)
String cx ="abc";
System.out.println(cx);
cx = "nice";
System.out.println(cx);
Output results :
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
Here we can see that in fact String Class is the encapsulation of a character array . But this value He is also a quote , It points to a data object .
String cx ="cx";
System.out.println(cx);
cx = cx.replace("c", "C");
System.out.println(cx);
Output results :
String cx ="cx";
System.out.println(cx);
cx.replace("c", "C");
System.out.println(cx);
Output results ;
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = count;
int i = -1;
char[] val = value; /* avoid getfield opcode */
int off = offset; /* avoid getfield opcode */
while (++i < len) {
if (val[off + i] == oldChar) {
break;
}
}
if (i < len) {
char buf[] = new char[len];
for (int j = 0 ; j < i ; j++) {
buf[j] = val[off+j];
}
while (i < len) {
char c = val[off + i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
<span style="color:#ff0000;">return new String(0, len, buf);</span>
}
}
return this;
}
After reading it, everyone has an epiphany .
版权声明
本文为[Cx_ Xuan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220545385472.html
边栏推荐
- 4 most common automated test challenges and Countermeasures
- Basic knowledge of redis
- MFC implementation resources are implemented separately by DLL
- Blender程序化地形制作
- Swing display time (click once to display once)
- 相机成像+单应性变换+相机标定+立体校正
- Requirements for SQL server to retrieve SQL and user information
- 项目经理值得一试的思维方式:项目成功方程式
- Mairadb数据库基本操作之数据管理
- How to set the initial value of El input number to null
猜你喜欢
SQL Server检索SQL和用户信息的需求
Uniapp wechat sharing
Requirements for SQL server to retrieve SQL and user information
es6数组的使用
Graphics. Fromimage reports an error "graphics object cannot be created from an image that has an indexed pixel..."
Qingdao agile tour, coming!
JS Array常见方法
2021-10-08
Kanban Quick Start Guide
Domain driven model DDD (III) -- using saga to manage transactions
随机推荐
JSP -- Introduction to JSP
2021 年 25 大 DevOps 工具(下)
好的测试数据管理,到底要怎么做?
Solution of how to log in with mobile phone verification code in wireless network
WTL 自绘控件库 (CQsCheckComboxBox)
Laravel routing job
Source code analysis of how to use jump table in redis
Pandas to_ SQL function pit avoidance guide "with correct code to run"
工具在数字化转型中扮演了什么样的角色?
The prefix of static of egg can be modified, including boots
CORS and proxy (づ  ̄ 3  ̄) in egg ~ the process of stepping on the pit and filling the pit ~ tot~
2021-10-25
学习笔记:Unity CustomSRP-12-HDR
日志简介和构建web应用
What are the reasons for the failure of digital transformation?
The 2021 IT industry project management survey report was released!
Basic knowledge of vegetable chicken database
Implementation of resnet-34 CNN with kears
Master-slave replication of MariaDB database
Laravel implements the Holy Grail model with template inheritance