简体   繁体   English

Java final String,C ++等价物。 我的理解是否正确?

[英]Java final String, C++ equivalent. Is my understanding correct?

So I stumbled upon the following piece of Java code : 所以我偶然发现了以下Java代码:

final String osName = System.getProperty("os.name");

I have read in several other StackOverFlow questions that the String class is actually immutable. 我在其他几个StackOverFlow问题中读过String类实际上是不可变的。 So I asked myself the following thing, why is it the String declared final? 所以我问自己以下的事情,为什么String声明为final?

I am not very good with C++, but in order to understand what happens behind the scenes(in Java) I decided to see what would be the equivalent in C++. 我对C ++不是很了解,但是为了理解幕后发生的事情(在Java中),我决定看看C ++中的等价物是什么。

If my understanding is correct making the String final in Java is equivalent in making a pointer in C++ being const, that is what the pointer variable points to cannot be changed. 如果我的理解是正确的,那么使Java中的String final与使C ++中的指针成为const是等价的,那就是指针变量所指向的是无法改变的。 The limitation in Java is that you can change only the constness of the pointer to that String, but the chuck of memory the pointer points to is immutable. Java中的限制是你只能改变指向该String的指针的常量,但指针指向的内存量是不可变的。

So my question is am I correct or I am missing something else? 所以我的问题是我是正确的还是我错过了其他的东西?

When we say that Java String(s) are immutable, it is because a String object cannot be modified once created. 当我们说Java String是不可变的时,是因为String对象一旦创建就无法修改。 Instead, you must create (and assign a reference to) a new String when you want to modify a String reference. 相反,当您要修改String引用时, 必须创建(并分配对新String的引用)。 When you mark a String variable as final you are additionally making the reference immutable. 当您将String变量标记为final您还要使引用不可变。

You are correct in your assumption. 你的假设是正确的。 String is immutable but without the final declaration the pointer can't be changed. 字符串是不可变的,但没有最终声明,指针不能更改。 So if you want to "change" the String variable you can do so but it will not actually change the object it will create a new object and point the pointer to the new object. 因此,如果您想“更改”String变量,您可以这样做,但它实际上不会更改对象,它将创建一个新对象并将指针指向新对象。 Adding final makes it so you cannot do this it won't let you change the pointer reference and the String object that it points to is immutable so you cannot change a final String. 添加final会使它无法执行此操作,它不会让您更改指针引用,并且它指向的String对象是不可变的,因此您无法更改最终的String。

Java differs in a number of ways. Java在很多方面都有所不同。

In Java you can never alter a String 's contents after creation - it is immutable . 在Java中,你永远不能在创建后改变String的内容 - 它是不可变的 Also the Java reference is essentially equivalent to a C++ pointer. Java引用本质上也等同于C ++指针。 But in C++ you can change the contents of a std::string unless it is declared const . 但是在C ++中你可以改变std::string的内容,除非它被声明为const

Putting that together: 把它放在一起:

String s = "stuff"; // Java
const std::string* s = new std::string("stuff"); // C++

Now making a Java reference final in Java means you can not change the reference to point to a different String . 现在用Java创建Java引用final意味着您不能将引用更改为指向不同的String That is equivalent to making the pointer const in C++: 这相当于在C ++中使用指针const:

final String s = "stuff"; // Java
const std::string* const s = new std::string("stuff"); // C++

Also Jave manages memory automatically for its String s. 此外,Jave自动为其String管理内存。 This can be roughly approximated in C++. 这可以用C ++粗略地近似。

Like this: 像这样:

String s = "stuff"; // Java
std::shared_ptr<const std::string> s =
    std::make_shared<const std::string>("stuff"); // C++

and

final String s = "stuff"; // Java
const std::shared_ptr<const std::string> s =
    std::make_shared<const std::string>("stuff"); // C++

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM