String in Java


We can use strings when we need to process a group of characters which include alphabets, numbers, symbols, or combinations of them. 

Using strings we can save our time and increase code readability by not creating a variable for every character.

Let’s dive deep into strings in JAVA

Initialization of strings 

We can use a character array and later convert it into a string.

char[] ch={‘t’,’a’,’k’,’e’,’u’,’f’,’o’,’r’,’w’,’a’,’r’,’d’};
String s=new String(ch);

The following program depicts a simple usage of character array as a string


Java Code

class Main {
    public static void main(String[] args) {
        char[] ch={'t','a','k','e','u','f','o','r','w','a','r','d'};
        String s=new String(ch);

Using string object

We can create a string object of java.lang.String class to initialize a string. String objects are immutable. To create mutable strings, use StringBuffer or StringBuilder class.

There are two ways to create a string object in JAVA

  1. Using string literal
  2. Using new keyword
  • Using string literal

Using string literal is more memory efficient because, before the creation of an object, JVM checks the memory if there exists the same object or not.

We can create a string using the following syntax:

String s=”takeuforward”;

If we try creating a new object with the same value, no new instance would be created but a reference to the existing string would be returned.

String s1=”takeuforward”;
String s2=”takeuforward”;

The following code shows the simple working of string literals


Java Code

class Main {
    public static void main(String[] args) {
        String s1=”striver”;
  • Using new keyword

We can also use it to create a string object in JAVA

String s=new String(“Striver”);

The following code shows the simple working of string objects using the new keyword


Java Code

class Main {
    public static void main(String[] args) {
        String s2=new String(“Striver”);

Memory Management in methods of creating a string object

Under JVM architecture, there are different types of memory areas

  1. Method area
  2. Heap area
  3. Stack Area
  4. PC Register
  5. Native Method Area

Whenever an object is created, the memory allocation takes place under the heap area.

String s=”takeuforward”;

When we use string literal to create a string object, memory allocation takes place in the string constant pool area present in the heap area. So an object with the name “takeuforward” will be created and a reference variable s will point to this object.

Therefore, one object and one reference variable are created when we use string literals to create string objects.

Pictorial representation of the above explanation is as follows

String s=new String(“takeuforward”);

When we use the new keyword to create an object, the memory allocation takes place in heap memory (non-string constant pool area). So an object with the name “takeuforward” will be created and a reference variable s will be pointing to that object. 

But one interesting thing to note here is, we are also creating a string literal “takeuforward”. String literals allocate memory inside a special location present in the heap area, known as the string constant pool area. Hence another object with name “takeuforward” will be created.

Therefore two objects and one reference variable will be created when we use the new keyword to create a string object.

Following is the pictorial representation of the above explanation.

Comparing both the methods, one would prefer using string literals to create string objects since a single object and a single reference variable are created. This would prevent unnecessary load on the project unlike under the usage of the new keyword where two objects were created.

String functions

java.lang.String class offers a rich library of methods that can be useful under string manipulation and other processes. Some popular ones are mentioned below

  • Java string toUpperCase(): returns the string in uppercase format
String s="takeuforward";
String sUpper=s.toUpperCase();
System.out.println(sUpper); //prints TAKEUFORWARD
  • Java string toLowerCase(): returns the string in lowercase format
String sLower=s.toLowerCase();
System.out.println(sLower); // prints takeuforward
  • Java string indexOf(): returns the position of first occurence of the specified character or a substring
String s="takeuforward";
int index1=s.indexOf("a");
int index2=s.indexOf("for");
System.out.println(index1); // prints 1
System.out.println(index2); // prints 5
  • Java string concat(): combines two strings. Similar to appending one string at the end of another string.
String s="takeuforward";
s1=" striver's initiative";
System.out.println(s); // prints takeuforward striver's initiative
  • Java string substring(): returns a substring/part of a string.
String s="takeuforward";
System.out.println(s.substring(2)); // prints keuforward
System.out.println(s.substring(2,6)); // prints keuf
  • Java string length(): returns the length of the string
 String s="takeuforward";
 System.out.println(s.length()); // prints 12
  • Java string isEmpty(): returns true if the string is empty otherwise false.
String s1=”takeuforward”;
String s2=””;
System.out.println(s1.isEmpty()); // prints false
System.out.println(s2.isEmpty()); // prints true
  • Java string trim(): Eliminates leading and trailing spaces in the string.
String s=” striver ”;
System.out.println(“Old string is:“ + s); // prints Old string is: striver 
System.out.println(“New string is:“ +s.trim()); // prints New string is:striver
  • Java string equals(): Compares two strings based on the value and returns true or false.
String s1=”striver”;
String s2=”STRIVER”;
System.out.println(s1.equals(s2)); // prints false since letter casing doesn’t match
  • Java string replace(): Returns a string by replacing an old character or old set of characters with a new or new set of characters.
String s=”takeuforward”;
String newS=s.replace(‘t’,’T’);
System.out.println(newS); // prints Takeuforward

Special thanks to Yash Mishra for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article