课程: Java Memory Management: Values and References

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Strings in memory

Strings in memory

- [Instructor] Strings are the odd one out in terms of objects in memory management. Since strings are used a lot in Java, there's a mechanism to improve the performance of strings and to avoid having to create a new object for every string. So Java tries to deal with strings extra efficiently, and when the string is exactly the same as a different one, it will be assigned the same reference as the other one. And this mechanism is actually called the string pool. Instead of always creating a new string object, the Java memory checks a special memory segment called the string pool, to assign a reference of a string to a firewall. And this is why when you're in a class the equals equal sign on two strings with the same value will return true. The string object is being recycled from the string pool, and this can be done safely because strings are immutable and the value will never change anyways. So even if you pass the reference around, this will not cause the risk of your string being…

内容