Java Strings

tutorial and example
tutorial and example

JAVA STRING

String class is the most used class in Java programming language. String is the sequence of characters, which is treated as objects in Java.

Creating String objects

We create String objects using new operator or by using string literal values within double quotes. But there is a big difference between them about how these objects are stored and referred to, by Java.

Example 1.

Output:

String objects created using new operator always refer to separate objects, even if they store the same sequence of characters.

Example 2.

Output:

When the string object is created using literal method, JVM searches for the same sequence of character in the string pool. If it finds the same sequence, JVM does not create the string object but make it to refer the same string previously created. Hence str2 will point to the object created by the str1 that’s why the result is true here.

We can also create string object using

If the matching value is found in the string pool the value is reused. If the matching value is not found in the string pool, JVM have to first create it and then place it to the spring pool.

Let’s count some string objects:

1 String star = new String(“Star”);

2 String star2 = “Star”

3 System.out.println(“Star”);

4 System.out.println(“Moon”);

5 System.out.println(“Moon” == “star”);

6 String star1 = new String(“Star”);

Line 1 creates String object  having value “Star” in heap memory.

Line 2 creates String object and place it to the string pool.

Line 3 does not creates String object since same value is stored in the string pool.

Line 4 creates String object having value “Moon” and place it to the string pool.

Line 5 creates String object since “star”  has lower case ‘s’ and it is treated as different value.

Line 6 creates String object having value “Star” in heap memory.

String class is immutable

String class is immutable, it means that the object of this class can not be modified.

This is for performance reason, the JVM can reuse this object which led to low memory overhead and good performance.

String class is made immutable by following three steps.

  • The value of the String object is stored in the char array (char value[]) and it is marked final so that it cannot be modified or re-initialized.
  • The length of the array is fixed and cannot be increased once initialized.
  • All methods defined in the string class do not modify the array value instead, they return new String object.

Some methods of the String Class.

charAt() : It is used to retrieve a character at a specified index of a String :

Example:

Output:

This method may throw runtime exception if we seek the position out of the range

Example:

the exception is java.lang.StringIndexOutOfBoundsException.

indexOf() :  This method search for the occurrence of a character or a String, if it found in the target String, it returns the first matching position else -1.

Example :

Output:

By default, JVM starts searching from index position 0 if we want to start searching at some specific position we may pass it like

Output:

substring() : It return the substring of the targeted string. It is of two kinds,

Example :

Output:

The first output will print from seeking position to the end of the given string.

The second will print from seeking position to the last value-1(does not include the character at the end position).

It should be noted that the total length of the resulted string is

(end Index) – (beginning Index).

trim() : This method removes the leading and trailing whitespace and returns new string.

Example :

Output:

replace(‘a’, ‘A’) : This method replaces all the specified occurrence of a char value with another value. We can also replace String with another String.

Example :

Output:

length() : length() method  is used to retrieve the length of a String.

Example :

Output:

Equality of Strings

equals() method is used to compare the equality of two string. This method returns true if the object being compared to, is a string object and have the same characters sequence.

Output:

String Concatenation

String can be concatenated using concat() or “+” operator.

or like

or like

Example:

Output: