File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String related doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String related doubt" Watch "String related doubt" New topic
Author

String related doubt

Naga Venkata Pradeep Namburi
Greenhorn

Joined: Apr 04, 2008
Posts: 13
Java uses Immutability concept for Strings. Which means that a String is completely immutable. And also is uses the concept of pooling all the strings. So when you use the same string again, instead of creating, it will point to the object already present in the pool.
So why will it be a perfomance improvement if you declare a String (which is being used again and again in your code) as a constant and use it.
(Any how the VM is not going to create another object. It will just pick the one in the pool.)

Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 155
It's just a concept matter. You're right in your point of view. The problem is, if you don't declare a String as a constant that String can later be changed (pointed to another object) and lose the reference to the previous one.
Chitta Ranjan Mahato
Ranch Hand

Joined: Jun 20, 2009
Posts: 38
Strings are immutable means once you assigned a value to String it never be changed.
Important Facts about Strings and Memory
One of the key goals of any good programming langugae is to make efficient use of memory.To make java more memory efficient ,JVM sets aside a special memory area of memory called the "String constant Pool." When the compiler
encounters a String literal , it checks the pol to see if an identical String already exists.If a match is found,the reference of the new literal is directed to the existing String.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Naga Venkata Pradeep Namburi wrote:Java uses Immutability concept for Strings. Which means that a String is completely immutable. And also is uses the concept of pooling all the strings. So when you use the same string again, instead of creating, it will point to the object already present in the pool.
So why will it be a perfomance improvement if you declare a String (which is being used again and again in your code) as a constant and use it.
(Any how the VM is not going to create another object. It will just pick the one in the pool.)



But if you declare the variable as final, then the compiler will insert a reference to the String constant wherever the variable is used. This saves the Run Time having to look up a reference, then find the proper String in the constants pool. So it is more efficient. For example this code:

compiles to the same exact byte code as this:


You save an extract byte-code command for each cycle through the for loop when compared to not using final Strings, maintain safe references to String objects that won't change, and you clearly describe to anyone else who sees your code that 'This String Will Not Change!' And self-documenting code is always a good thing.


Steve
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Naga Venkata Pradeep Namburi wrote:Java uses Immutability concept for Strings. Which means that a String is completely immutable. And also is uses the concept of pooling all the strings. So when you use the same string again, instead of creating, it will point to the object already present in the pool.
So why will it be a perfomance improvement if you declare a String (which is being used again and again in your code) as a constant and use it.
(Any how the VM is not going to create another object. It will just pick the one in the pool.)


In addition to what Eduardo said, consider the following example:
Now suppose you want to change "Hello World" in the entire class to "Hello World!". You forget about using the editor's replace functionality, and do it manually. You find the first occurrance, and you change it. You continue searching but miss the second one. Oops! There's still a "Hello World" hardcoded.

The following can fix that:
Now you only have to change one line - the declaration.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Naga Venkata Pradeep Namburi
Greenhorn

Joined: Apr 04, 2008
Posts: 13
Thanks!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String related doubt