Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the advantage of string object as compared to string literal

Tags:

java

i want to know where to use string object(in which scenario in my java code). ok i understood the diff btwn string literal and string object, but i want to know that since java has given us the power to make string object, there must be some reason, at some point string object creation would be useful. so i want to know in which scenario can we prefer string object in place of string literal.

like image 504
kaka Avatar asked Jul 29 '10 06:07

kaka


People also ask

Which is better string literal or String object?

In general, we should use the String literal notation when possible. It is easier to read and it gives the compiler a chance to optimize our code.

What is the benefit of using string object?

Advantages of C-strings: Compile-time allocation and determination of size. This makes them more efficient, faster run-time when using them.

What is the difference between string and string object in Java?

Generally, String is a sequence of characters. But in Java, string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object.

What is the difference between string and string literal in C?

C-strings are simply implemented as a char array which is terminated by a null character (aka 0 ). This last part of the definition is important: all C-strings are char arrays, but not all char arrays are c-strings. C-strings of this form are called “string literals“: const char * str = "This is a string literal.


2 Answers

In most situations, you should use String literals to avoid creating unnecessary objects. This is actually Item 5: Avoid creating unnecessary objects of Effective Java:

Item 5: Avoid creating unnecessary objects

It is often appropriate to reuse a single object instead of creating a new functionally equivalent object each time it is needed. Reuse can be both faster and more stylish. An object can always be reused if it is immutable (Item 15). As an extreme example of what not to do, consider this statement:

String s = new String("stringette"); // DON'T DO THIS!

The statement creates a new String instance each time it is executed, and none of those object creations is necessary. The argument to the String constructor ("stringette") is itself a String instance, functionally identical to all of the objects created by the constructor. If this usage occurs in a loop or in a frequently invoked method, millions of String instances can be created needlessly. The improved version is simply the following:

String s = "stringette";

This version uses a single String instance, rather than creating a new one each time it is executed. Furthermore, it is guaranteed that the object will be reused by any other code running in the same virtual machine that happens to con- tain the same string literal [JLS, 3.10.5]

There is however one situation where you want to use the new String(String) constructor: when you want to force a substring to copy to a new underlying character array like in:

String tiny = new String(huge.substring(0, 10));

This will allow the big underlying char[] from the original huge String to be recycled by the GC.

like image 128
Pascal Thivent Avatar answered Nov 11 '22 00:11

Pascal Thivent


Don't use a new String object if you know what the string is. For example:

String str = new String("foo"); // don't do this

You are thus creating an unnecessary object - once you have a String object created from the literal, and then you create another one, taking the first one as constructor argument.

like image 33
Bozho Avatar answered Nov 11 '22 00:11

Bozho