Java String and String Pooling


This tutorial will help us to get deep knowledge on Java String and String pooling. Collection of characters are called as String. String is immutable which means it cannot be change and internally it stores in to char array. String belongs to java.lang package.

Ways of creating String:

  1. String literal:

Below is the most common way of creating string object.

String object stores in heap memory as in String pool. Java String pool is all about collection of Strings stored in heap memory. On creation of every string object, string pool first checks whether the same string is present in string pool. It will return the reference if string is present in String pool, else will create a new object.

String str = “This is my first blog”;

2. String with new operator:

String define with the new keyword will not check for the value is either present in string pool or not. Every time new keyword will create a new reference and fresh object in the String pool.

String str = new String(“This is my first blog”);

As mentioned in below diagram, String created value as one using String literal will get create inside String pool. If program tries to created a brand new one more String with value as one using String literal will point to same String value in String pool.

String created using new operator with same value as one mentioned above as will not be created as part of String pool and will always point to new location.

Important and most used predefined methods in String Class:

String Length:

String length can be identified with the help of length() method and it return the number of characters as a result.

String a = "three";

uppercase() and lowercase()

Methods will convert String in to complete in lower and upper case.

String a = "Three";

Equals() and equalIgnoreCase()

Equals() -> equals method don’t ignore the case and provide us the capability to compare two String and return true and false as a result.

equalIgnoreCase() -> equals method ignore the case and provide us the capability to compare two String and return true and false as a result.

String a = "ONE", b = "One";


This will return the first occurrence of specific character passed in method.

Most of the time we get confused to get very first character in between charAt(0) or charAt(1). This always starts from zero index.

String a = "Hello World";

Note: Java starts counting from zero and it also count the white space.

indexOf() and lastIndexOf()

indexOf() : indexOf method will return the position and first occurrence of word.

lastIndexOf() : lastIndexOf method will return the position and last occurrence of word given in function parameter.

String str = "the is my home. home sweat home";
System.out.println("Index Of "+str.indexOf("home"));
System.out.println("Last Index Of "+str.lastIndexOf("home"));OUTPUT:
Index Of 10
Last Index Of 27

String concatenation

Operator overloading only possible with String in Java. Using +(plus) operator we can concatenate two strings.

String a = "Hello ", b = "World";
System.out.println(a + b);OUTPUT:
Hello World


concatenation can also be done with the help of String predefined function concat().

String a = "Hello ", b = "World";
Hello World


This function checks the either the String contain substring or not. It will return true if substring found else return false. This will also be checking for case sensitive.

String a = "Hello World";
        System.out.println("hello = "+a.contains("hello"));
        System.out.println("Hello = "+a.contains("Hello"));OUTPUT:
        hello = false
        Hello = true


This will return true if the String is ending with the passed substring.

String a = "Hello World";
System.out.println("World = "+ a.endsWith("World"));
System.out.println("world = "+ a.endsWith("world"));OUTPUT:
World = true
world = false


This will return true if the String is starting with the passed substring.

String a = "Hello World";
System.out.println("Hello = "+ a.startsWith("Hello"));
System.out.println("hello = "+ a. startsWith("hello"));OUTPUT:
Hello = true
hello = false

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan