内存位置如何在此代码中发生?当我们创建字符串的子字符串是否新的字符串分配是由子字符串创建时会发生什么? [英] how the memory location takes place in this code ? and what happen when we create sub-string of string whether the new allocation of string is created by sub-string?

查看:103
本文介绍了内存位置如何在此代码中发生?当我们创建字符串的子字符串是否新的字符串分配是由子字符串创建时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 字符串 a =   ABC; 
字符串 b = xyz;
字符串 c = a + b;

解决方案

In你描述的所有情况,都会为新的字符串对象分配新的内存。



但是并非在所有情况下都会发生这种情况。要了解内存分配的不同情况,您必须了解两个事实:1)Java字符串是不可变对象,2)字符串实习被使用。



请参阅:

https://en.wikipedia.org / wiki / String_interning [ ^ ],
http://javatechniques.com/blog/string-equality-and-interning [ ^ ]。



顺便说一句,看看这些与你的问题没有直接关系的问题的有趣答案:





-SA

String a="abc";
String b="xyz";
String c=a+b;

解决方案

In all cases you described, new piece of memory is allocated for new string object.

But this does not happen in all cases. To understand different cases of memory allocation, you have to understand two facts: 1) Java strings are immutable objects, 2) string interning is used.

Please see:
https://en.wikipedia.org/wiki/String_interning[^],
http://javatechniques.com/blog/string-equality-and-interning[^].

By the way, look at this interesting answers to the questions not directly related to yours:


—SA


这篇关于内存位置如何在此代码中发生?当我们创建字符串的子字符串是否新的字符串分配是由子字符串创建时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆