Strings in java

Let’s learn Strings in java.

Strings in java

A String is an object in java. It is not a primitive type.

For example:

String name = “Sachin”;

java.lang.String class implements Serializable, Comparable and CharSequence interfaces.

NOTE:

String object is immutable. Once created a String object cannot be changed.


There are two ways to string object. They are,

  • By string literal: String literal is created using double quotes. Example: String s = “VIRAT”;
  • By new keyword: Here JVM will create new string object in (non string constant pool) heap area.

Here’s an example on Strings in java.

public class JavaStringExample
{
   public static void main(String[] args)
   {
      // string literal in java
      String str1 = "core"; 
      char[] ch = {'j','a','v','a'};
      // convert char array to string
      String str2 = new String(ch);
      // create java string by new keyword
      String str3 = new String("helloworld");
      System.out.println(str1);  
      System.out.println(str2);  
      System.out.println(str3);
   }
}

Output:

core
java
helloworld


NOTE: String objects are stored in special memory area known as String Constant Pool.


String constant pool

String constant pool in java or string literal pool is a unique memory location inside heap area where string literal objects are stored. Let’s understand string constant pool.

strings in java

Let’s create string object using “new” keyword.

String str = new String(“Hello”);

In the above statement I’m creating string object “str” and this object gets memory allocation in heap area because of “new” keyword. Meanwhile there is string literal in above statement, i.e, “Hello”.

This string literal gets memory allocation in String constant pool area. Hence creating another object.

JVM internally creates reference variable for “Hello” object in string constant pool. If you notice we are creating two objects by writing one single statement.

Now let’s create string object using string literal like this,

String s = “World”;

Here i’m creating a string literal “World”. This creates object in string constant pool. By creating string object using string literal, object will not be created in heap area.

In case of string literal we are creating only one object in string constant pool. While using “new” keyword we are creating two string objects one in heap area and another in string constant pool.

This is the reason string literal is preferred in creating string objects.

NOTE:

If there is already a string literal “Hello” in the string constant pool, then it will not create new string object in string constant pool with string literal “Hello”.

If there is no string literal “Hello” in string constant pool, then two objects will be created. One in heap area and another in string constant pool.

Until java 1.6 version string constant pool was present in method area of memory area. After java 1.6 version it was shifted to heap area.

Refer – preface to java virtual machine and architecture

NOTE: Garbage collection is not applicable for string objects present in string literal pool since JVM internally maintain a reference.


MethodDescription
static String valueOf(int i)returns the string representation of the int argument.
String trim()returns a copy of the string, with leading and trailing whitespace omitted.
String toUpperCase(Locale locale)converts all of the characters in this String to upper case using the rules of the given Locale.
String toUpperCase()converts all of the characters in this String to upper case using the rules of the default locale.
String toLowerCase(Locale locale)converts all of the characters in this String to lower case using the rules of the given Locale.
String toLowerCase()converts all of the characters in this String to lower case using the rules of the default locale.
int indexOf(String str, int fromIndex)returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
int indexOf(String str)returns the index within this string of the first occurrence of the specified substring.
int indexOf(int ch, int fromIndex)returns the specified char value index starting with given index.
int indexOf(int ch)returns the index within this string of the first occurrence of the specified character.
String intern()returns a canonical representation for the string object.
String[] split(String regex, int limit)Splits this string around matches of the given regular expression.
String[] split(String regex)Splits this string around matches of the given regular expression.
boolean String equalsIgnoreCase(String anotherString)Compares this String to another String, ignoring case considerations.
String replace(CharSequence target, CharSequence replacement)Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
String replace(char oldChar, char newChar)Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
String concat(String str)Concatenates the specified string to the end of this string.
boolean isEmpty()Returns true if, and only if, length() is 0.
boolean equals(Object anObject)Compares this string to the specified object.
boolean contains(CharSequence s)Returns true if and only if this string contains the specified sequence of char values.
String substring(int beginIndex)Returns a new string that is a substring of this string.
char charAt(int index)Returns the char value at the specified index.
int length()Returns the length of this string.
char charAt(int index)Returns the char value at the specified index.
static String format(String format, Object… args)Returns a formatted string using the specified format string and arguments.
static String format(Locale l, String format, Object… args)Returns a formatted string using the specified locale, format string, and arguments.
String substring(int beginIndex, int endIndex)Returns a new string that is a substring of this string.

String Methods