Introduction to String Class in Java

 


What is String in Java?

String is a sequence of characters. But in Java, String is an object that represents a sequence of characters. String class is in java.lang package.

All string literals in Java programs, such as “hello”, are implemented as instances of String class.

Strings are immutable; they are constant; their values cannot be changed after they are created.


What are Literals?

A Java literal is any value we specify as a constant in the code.

It can be of any type — integer, float, double, long, String, char, or boolean.


String Literal

Any text between double quotes is a String literal.

String literals can be on one line. If you want to have multi-line, you must use String concatenation (+) and new line character (\n). Below example shows single line String literal and multiline String literal.

The output:


Hierarchy of String class

Java String class implements three interfaces.

(Since the most super class is Object class String also extends Object class.)

  • CharSequence interface in java.lang package.
  • Comparable interface in java.lang package.
  • Serializable interface in java.io package.

(Since Java 12 String class implements Constable Interface and ConstantDesc Interface. (ConstantDesc is a sealed Interface. I will come up with sealed Interface and Class in another post 😉)


How to create a String Object?

In order to create String object, we can use 2 ways.

  • By using String literals

String person = “John”;

  • By using new keyword

String person = new Person(“John”);


String Pool/ String Constant Pool/ String Intern Pool

  • String Pool also known as String Constant Pool and String Intern Pool is a special memory location where JVM stores String instances.
  • String pool is a runtime constant pool.
  • String is stored in method area which is a part of heap memory area of JVM.
  • The runtime constant pool is created when the Class or the Interface created by JVM.

When a String is created, the JVM check whether if an identical String value already exists in the String Pool.

  • If it does, the new String refers to the existing String value in the String constant pool. This is because the JVM only stores one copy of a particular String in the String Pool.
  • If the String is not found in the pool it is added to the pool (interns to the pool) for future reuse.

This will enhance the optimization of application performance by tracking the frequency and quantity of Strings allocated.


What is an immutable object?

An immutable object is an object which never change its internal state, once the object is created. 

This means after the object is assigned to a variable, it can’t update the reference nor mutate the internal state.

If an object is immutable, it provides benefits such as security, easy to reuse without duplication, ease of cashing, thread safe, and no synchronization issues.


Why String is Immutable?

Below example describes the immutability of String class.

The output result:


  • String interning is not possible if String class would not be immutable.
  • If String is not immutable JRE cannot save the heap space.
  • If String is not immutable, it can lead to serious security threats.
  • Different threads can use same String instance. So, String is safe for multithreading.

String Interning

Since Strings are immutable, JVM can optimize the amount of memory space allocated for a String literal by keeping only one copy of String literal in the String pool. This process is called String interning. 

Below example describes String Interning.

The output:


There is more to talk about String class in Java. I wrap up for today. I will come up with another post. 👋

Thank you for reading ❤.

Follow me via LinkedIn: www.linkedin.com/in/shihara-kaumadi

Follow me via Medium Blog : https://medium.com/@shihara-kaumadi


Comments