Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道在使用 new 关键字初始化字符串以及在没有 new 关键字的情况下为字符串设置值时如何分配内存。
阿比舍克·斯里坎特
使用 new 关键字初始化它的方式或使用 '=' 直接设置字符串的方式没有区别。内存都是在堆上分配的,因此一旦超出范围,垃圾收集器就负责收集内存。唯一的区别是 'new' 调用构造函数,而如果您直接使用 = 赋值,则调用重载运算符并初始化字符串的新实例。