String in Java

In java String is an object that represents as a sequence of characters.

String in java can be defined in the following ways:

  • String is a referenced data type in java
  • String is immutable in nature.
  • String is a final class in java

String is not a primitive data type in java

But the java compiler has special support to treat string literals into String instances as well as it also performs string concatenation. String is a final class in java and a reference data type of java.

String s=”Ayushi”; is similar to collection of array of characters

char ch[]={‘A’,’y’,’u’,’s’,’h’,’i’};

Let us see a program to understand the concept
class Array {
public static void main(String args[]) {
char ch[]={'A','y','u','s','h','i'}; // array of characters
String s="Ayushi"; // storing string value
String obj=new String(ch);// passing character array into String object
System.out.println(ch);
System.out.println(obj);
System.out.println(s); } }

Output:

What is String in Java

In java String is an object which represents the sequence of characters which we have already discussed above. String class in java is found in java.lang package. Therefore if we want to create the object of String class then we have to import java.lang.String class

A string literal or string constant may consist of a set of characters, alphanumeric characters and digits. Example: “COMPUTER”, “GOOGLE PAY”, “Pay500”, “2209” , etc.

NOTE: A string containing digits within double quotes is treated as a symbol and therefore it can’t be used for arithmetic calculations.

How to create String objects in java

There are two ways to create String objects in java. They are as under:

  1. through String literals.
  2. through new keyword

1. through String literals

One can create String objects with the help of String literals. When any string is created with the help of double quotes then it is referred to as String literal. Java uses this concept to make Java more memory efficient. For example:

String a=”Ayushi Academy”;

Note: Whenever we create an object with the help of String literal concept, the JVM first checks the “String Constant Pool” and if the created String object exists in that pool, then a reference to that pooled object is returned. If the String object does not exists in that “String Constant Pool” then a new instance of that String object is created and placed in the pool. For example:

String a=”Hello”;

String b=”Hello”;

In the above statements only one instance (object) will be created. Let’s understand through the given diagram below.

Let us understand through an example.

class StringLiteralDemo {
public static void main(String args[]) {
String a="Hello";
String b="Hello";
if(a==b) System.out.println("Same address"); else System.out.println("Different address"); } }

Output:

If we create String literal objects as the given below statements then the concept change. Let us understand the concept through the given diagram below:

String a=”Hello”;

String b=”Hello”;

String c=”hello”;

Let us understand the concept through the given program below:

class StringLiteralDemo {
public static void main(String args[]) {
String a="Hello";
String b="Hello";
String c="hello";
if(a==b) System.out.println("Same address"); else System.out.println("Different address");
if(a==c) System.out.println("Same address"); else System.out.println("Different address"); } }

Output:

2. through new keyword

String object can be also be created using new keyword.

String s=new String(“Hello”);

When we write the above code of statement for creating String object, then in that case two objects are created, first in Heap area because of new keyword and second in String Constant Pool because of string literal “Hello”. Let us understand this concept through the given diagram below.

Let us go through another example to understand the concept of memory allocation internally.

String a=new String (“Hello”);

String b=new String (“Hello”);

String c=”Hello”;

String d=”Hello”;

Explanation:

From the above diagram it is now clear that only 3 objects will be created. Two object at Heap area and one object at String Constant Pool. Whenever we create String objects through new keyword for first time, a new object will be created in Heap area and for the first time a copy of that object is also created at String Constant Pool for future use. Therefore for second time when we create String object through new keyword, then again a second object is created in Heap area and JVM checks that String literal “Hello” is already maintained in String Constant Pool, that’s why no new object is created in String Constant Pool as in first time. When we create object for 3rd and 4th time using String literal concept then no new object is created, JVM checks that “Hello” is already maintained in String Constant Pool, that’s why again no new object is created, and reference variable starts pointing to the object of String Constant Pool as shown in the above diagram.