String in Java

Introduction

The string is one of the most widely used classes in java programming by developers. Strings are immutable, so StringBuffer and StringBuilder classes come to make the string mutable (manipulations) easy way.

 

String

In Java, a string is an object which represents a sequence of characters. The string is immutable, which means it can not be changed.

String uses the + operator to concatenate two strings and internally used the StringBuffer to perform this action.

 

How many ways do we create String Objects in java?

There are two ways we create a String Object in java.

  1. By using string literal
  2. By using new keyword

 

1. String literal

String in java

Java String literal is created by using double quotes.

Syntax

String s="Hello";

When a new string literal is created, the JVM first checks in the “string constant pool” for the String with the same value. If the string already exists in the pool, then it returns the reference instance. If string is not available with the same value in “string constant pool”, a new string instance is created, and places that instance in the String pool.

 

Benefits of String literal in java?

String literal makes, Java more memory efficient because no new objects are created, if it already that object is available in the string constant pool.

 

1. new keyword

Syntax

String s=new String("Hello");

In the above syntax, JVM will create a new string object in heap memory and nd the literal “Hello” will be placed in the string constant pool.

 

Example of String class

public class Test {
public static void main(String args[]) {
String s1 = "TechnicalRound";// creating string by using java string literal
char ch[] = { 't', 'e', 'c', 'h', 'n', 'i', 'c', 'a', 'l' };
String s2 = new String(ch);// converting char array to string
String s3 = new String("Java");// creating java string by using new keyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}

Output:

TechnicalRound
technical
Java

 

What is Immutable String in Java?

Immutable means unchangeable, if once we create string object then its data or state can not be changed but a new string object is created.

 

Let’s try to understand with Example.

class Example {
public static void main(String args[]) {
String s = "Niraj";
s.concat(" Singh");// concat() method is appends the string at the end of existing object
System.out.println(s);// It will print Niraj
// because strings are immutable objects that's why it not appends
}
}

Output:

Niraj

In the above example, “Niraj” is not changed, but a new object is created with “NirajSingh” in String Constant Pool. It will print “Niraj”, because s variable is point to the “Niraj” object, that why spring is Immutable.

 

Let’s try to understand with another Example.

class Example {
public static void main(String args[]) {
String s = "Niraj";
s = s.concat(" Singh");
System.out.println(s);
}
}

Output:

Niraj Singh

In the above example, String creates an object “Niraj” and assigns to the s variable. And again we cancate “Singh”, and explicitly assign it to the reference variable s. In this case, s is printing “Niraj Singh”, because in the s variable we explicitly assign “Niraj Singh”. But still “Niraj” object is not modified.

 

Read more topics related to java

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

 

Leave a Reply

Your email address will not be published. Required fields are marked *