The Best Technical Blog Ever


String in java

In java, Strings are objects, which defines a sequence of characters. String class in java is used to create and manipulate strings. The String class is immutable (constant), i.e. Strings in java, cannot be changed but we can create a new instance. So If there is a necessity to make a lot of modifications to Strings of characters, then you should use StringBuffer & StringBuilder Classes.

Creating Strings:

We can create String object in two ways, they are

  1. using string literal
  2. using new keyword

1. Using String Literal:

If you create String object using String literal , JVM first checks the string pool. If it already exists, it will return an existing object from String pool. Otherwise new String object will be created and placed in string pool for future use.

String strobj1="Solsoftsys";
String strobj2="Solsoftsys";

string literal

In the above example, only one string object will be created in string pool where String strobj1 and strobj2 will refer a single object "Solsoftsys". When the first statement encounters, JVM will not find any string object with the value "Solsoftsys" in string pool, so it will create a new object. After that it will find the string with the value " Solsoftsys" in the pool, so it will not create any new object instead it returns the reference of the same instance. Here, the double quoted literal is known as String literal and the cache which stored these String objects are known as as String pool.

string literal

String strobj1="Solsoftsys";
String strobj2="Solsoftsys1";

string literal

In the this example, two string will be created in string pool ("Solsoftsys " and " Solsoftsys1") where String strobj1 will refer string object " Solsoftsys " and strobj2 will refer another string object " Solsoftsys1".

2. Using new keyword:

When you create String object using new() operator, it always create a new object in heap memory.

String s1= new String("Solsoftsys");
String s2= new String("Solsoftsys");

String Object

For the above example, separate String object (two string objects) will be created in the heap.

Advantage of creating String through literals is, if that String value is already available in String Pool then you get the same reference. On the other hand, new operator always creates a new String object. The below example clearly explains by comparing two String objects created using String literal and new operator.

String Object

Output: true

it will return true because both (s1 and s2) are pointing the same object.

String Object

Output false false

Here, s1==s2 and s2==s3 will return false because they are pointing to different string object. The equality "==" operator compares object memory location and not characters of String.

By default Java doesn't put all string object into string pool, but you can also place any string into pool by calling intern() method of java.lang.String class. Even String literal concept automatically call intern() method to put string object into String pool, that was not present in the pool already that means java puts all string literal into string pool. In case of new, interning doesn't happen automatically, until you call intern() method on that object explicitly.

Garbage Collection for String

An object is eligible for garbage collection when it is no longer referenced. String literals are never eligible for Garbage Collection. Because string literals always have a reference to them from the String Literal Pool, not eligible for garbage collection. But String Object which don't have any reference then they will be eligible for garbage collection.

Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status