Strings in java

Let’s learn strings in java.

Strings in java

Java string is a sequence of characters which represent character values. To create string in java, java.lang.String package is used.

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

Syntax:

<String_type> <string_variable_name> = “<sequence>”;

Example:

String name = “George”;

strings in java

When you create a java string object, two objects are created, one in heap memory area and another in string constant pool.

In java, String object reference points to heap memory area.

Objects of String are immutable (constant, cannot be changed) in java. There are two ways to create string in java,

  • String literal: String name = “George”;
  • using “new” keyword: String name = new String(“George”);

Here’s an example on how to declare a string in java,

public class StringDemo 
{
   public static void main(String[] args) 
   {
      // creating string using string literal
      String str1 = "helloworld"; 
      // declaring using new operator 
      String str2 = new String("flowerbrackets"); 
      System.out.println(str1);  
      System.out.println(str2);
   }
}



Output:

helloworld
flowerbrackets


String constant pool in java

String constant pool in java is a pool of string objects stored in java heap memory. Let’s understand string constant pool,

strings in java
String str1 = "Hello";
String str2 = "Hello";
String str3 = "Hi";
String str4 = new String("Hello");



In java, if we declare a string literal (as shown above) we are calling intern() method. This method creates an exact copy of heap string object in string constant pool.

In the image above you can see java heap. Inside java heap we have string constant pool where pool of strings have been stored.

Now I’m declaring string object using string literal. Here JVM checks whether string “Hello” is present in the string constant pool or not.

(For explanation purpose, assume, initially string constant pool is empty.)

It is not there. So JVM will create new string instance and place it in the string constant pool and returns the reference.

“str1” will point to the string object in the pool. Again I will create string object using string literal.  JVM checks whether this string “Hello” is present in the string constant pool or not.

If you notice “Hello” string is present in the string constant pool. So it will just return the reference and “str2” will point to the same string object in the string constant pool.

Now we will create another string object using string literal. Again JVM will check whether string “Hi” is present in the string constant pool or not.

If you notice the string “Hi” is not present in the string constant pool. So JVM will create a new string object in the string constant pool and returns the reference.

“str3” will point to string “Hi” in the string constant pool. Lastly we create string object using new keyword and String constructor.

Also read – preface to JVM and architecture

So what JVM will do is, it will create string object in the java heap. Now “str4” will point to the string object which is in the java heap.

If you check for the equality operator, str1 == str2 will return true. Because both “str1” and “str2” are pointing to same string “Hello”.

str1 == str3 will return false. Because “str1” pointing to string “Hello” and “str3” pointing to string “Hi”.

str1 == str4 will return false. Because “str1” points to string “Hello” which is in string constant pool but “str4” points to string “Hi” which is in java heap.

Now let’s see java program on string constant pool,

public class StringConstantPoolDemo 
{
   public static void main(String[] args) 
   {
      String str1 = "Hello";
      String str2 = "Hello";
      String str3 = "Hi";
      String str4 = new String("Hello");
 
      System.out.println("str1 == str2 : " + (str1 == str2));
      System.out.println("str1 == str3 : " + (str1 == str3));
      System.out.println("str1 == str4 : " + (str1 == str4));
   }
} 



Output:

str1 == str2 : true
str1 == str3 : false
str1 == str4 : false


String class methods: Strings in java

MethodDescription
static String valueOf(int value)converts given type into string. This is an overloaded method.
String trim()removes beginning and ending spaces of this string.
String toUpperCase(Locale l)returns a string in uppercase using specified locale.
String toUpperCase()returns a string in uppercase.
String toLowerCase(Locale l)returns a string in lowercase using specified locale.
String toLowerCase()returns a string in lowercase.
int indexOf(String substring, int fromIndex)returns the specified substring index starting with given index.
int indexOf(String substring)returns the specified substring index.
int indexOf(int ch, int fromIndex)returns the specified char value index starting with given index.
int indexOf(int ch)returns the specified char value index.
String intern()returns an interned string.
String[] split(String regex, int limit)returns a split string matching regex and limit.
String[] split(String regex)returns a split string matching regex.
static String equalsIgnoreCase(String another)compares another string. It doesn’t check case.
String replace(CharSequence old, CharSequence new)replaces all occurrences of the specified CharSequence.
String replace(char old, char new)replaces all occurrences of the specified char value.
String concat(String str)concatenates the specified string.
boolean isEmpty()checks if string is empty.
boolean equals(Object another)checks the equality of string with the given object.
boolean contains(CharSequence s)returns true or false after matching the sequence of char value.
String substring(int beginIndex)returns substring for given begin index.
char charAt(int index)returns char value for the particular index
int length()returns string length

Reference – Oracle Docs