Sunday, 18 August 2013

Core Java Concepts: Strings & Memory

Visit My Page in TutorIndia.net
Core Java Concepts: Strings & Memory


Strings & Memory

# Key Points
>>Java sets aside a special area in memory called the "String constant pool"
>>Several reference variables refer to the same string object without knowing about each other thus  this is the reason why string objects are made "Immutable"
>>String class is final so that nobody can override it

One of the key goals of any good programming language is to make efficient use of memory.
As applications grow, it`s very common for String literals to occupy large amounts of a program`s memory, and there is often a lot of redundancy within the universe of String literals for a program. To make Java more memory efficient, the
JVM sets aside a special area of memory called the "String constant pool."

When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created.


Now we can start to see why making String objects
immutable is such a good idea. If several reference variables refer to the same String without even knowing it, it would be very bad if any of them could change the String`s value.


1 comment:

  1. Concepts about strings are very clear and understandable...thx a lot..

    ReplyDelete