当前位置:网站首页>String问题
String问题
2022-07-24 05:32:00 【脱发程序员】
多数情况下,基本类型直接代表了最底层的语言实现。所有基本类型的值都是不可改变的。
而string是C++、java、VB等编程语言中的字符串,字符串是一个特殊的对象,属于引用类型。
但是他又和普通的引用类型有所不同。
由于string对象的不可变,所以可以共享。对String类的任何改变,都是返回一个新的String类对象。
一般而言表现如下:
- int等原始值类型,直接复制一份,传到函数里修改,原值不变。
- 引用类型如int[] a,传到函数里修改,原值会修改。
- string类型,虽然是引用类型,但情况和int一样,会被复制,传到函数里修改,原值不变。
- 如果使用==号判断,则是直接判断两个字符串是否相等,而不是使用地址。
https://wenku.baidu.com/view/8a0d6acde309581b6bd97f19227916888486b920.html
单纯的C++里:
C++里String的情况可以看作原始值类型。且不论怎么定义,其表现都和原始值类型一样。
(1)string s1; —> // 默认初始化,s1是一个空字符串
(2)string s2 = s1; —> // s2是s1的副本,注意s2只是与s1的值相同,并不指向同一段地址
(3)string s3 = “hiya”; —> // s3是该字符串字面值的副本
(4)string s4(10, ‘c’); —> // s4的内容是: “cccccccccc”
string a("foo");
string b = "foo";
cout<< (a==b) << endl; // true
string a1 = a;
a = "bar";
cout << a1 << endl; // foo
上述例子可以看到,修改了a的值后,a1并没有变化,这不就是原始值类型的表现嘛。
https://blog.csdn.net/weixin_59649214/article/details/122741032
因此在函数里把String作为参数,修改该值,不会影响原来的值,所以如果要影响原址,要在参数前面加&。
比如
// ①
void test1(string s){
s="bar";
}
int main(){
string s{
"foo"};
test(s);
cout<<s<<endl;// foo
}
// ②
void test(string &s){
s="bar";
}
int main(){
string s{
"foo"};
test(s);
cout<<s<<endl;// bar
}
不单纯的Java:
正常情况下表现是一个原始值类型。
String str1 = "Foo";
String str2 = "Foo";
System.out.println(str1 == str2); // true
String str1_1 = str1;
str1 = "Bar";
System.out.println(str1_1); // Foo
而我们可以通过特殊的方式改变一下他,让他变成真正的引用值类型。
可以看到下面的例子,直接用==判断已经不行了,因为它们指向了不同的地址。
String str1 = new String("Foo");
String str2 = new String("Foo");
System.out.println(str1 == str2); // false
String str1_1 = str1;
str1 = "Bar";
System.out.println(str1_1); // Foo
解释看https://blog.csdn.net/weixin_53236070/article/details/123594927
不单纯的JavaScript
JavaScript里String是原始值类型,也可以是引用类型。
let s1 = "Foo";
let s2 = "Foo";
console.log(s1==s2); //true
下面是引用类型的例子。
let str1 = new String("Foo");
let str2 = new String("Foo");
console.log(str1 == str2); // false
let str1_1 = str1;
str1 = "Bar";
console.log(str1_1); // [String: 'Foo']
边栏推荐
- Special effects - Cherry Blossom falling background effects
- Login page + summary
- 【LVGL】组件的样式的设置、更改、删除API函数
- MySQL Index & execution plan
- Use of awk
- Kubernetes rapid installation
- Special effects - cobweb background effects
- STM32 MP3 music player based on FatFs r0.14b & SD card (also a simple application of FatFs)
- (静态,动态,文件)三个版本的通讯录
- adb交互-干掉难看的默认shell界面
猜你喜欢
![[lvgl (2)]](/img/f8/d04183cf74896295382765a9dfd88d.png)
[lvgl (2)]

It's not too much to fight a landlord in idea!

SSH Remote Access and control

【LVGL(重要)】样式属性API函数及其参数

Directory and file management
![[lvgl (1)] a brief introduction to lvgl](/img/2e/2e155f1d3669c27ad1b090ca954224.png)
[lvgl (1)] a brief introduction to lvgl

STM32 MP3 music player based on FatFs r0.14b & SD card (also a simple application of FatFs)

Transition effect

Process and planned task management
![[audio decoding chip] Application of vs1503 audio decoding chip](/img/ee/0d5f95fba647592cc95f1e9f410bc9.png)
[audio decoding chip] Application of vs1503 audio decoding chip
随机推荐
Special effects - Cherry Blossom falling background effects
JSONObject按照key的A——Z顺序排序
The character that appears the most times in the JS output string
我有 7种 实现web实时消息推送的方案,7种!
Common commands and package management of go language
Special effects - click the mouse, and a random color of love will appear
kubernetes 的Deployment(部署),Service概念,动态扩缩容
Crud of MySQL
Secondary processing of template data
These 10 kinds of timed tasks have been learned, and you can float directly
[small object velocimeter] only principle, no code
【波形/信号发生器】基于 STC1524K32S4 for C on Keil
类的加载器 和 双亲委派机制详解
【LVGL(2)】LVGL入门,在CodeBlock上进行模拟以及移植STM32
创建WPF项目
Restful API introduction
Special effects - click with the mouse and the fireworks will burst
目录和文件管理
[audio decoding chip] Application of vs1503 audio decoding chip
NFS共享服务及实验