String in java

String is a group of character or sequence of data. But in java String is a predefined class which is present in java.lang package.

We use double quotes to represent a string in Java. For example,

// create a string
String value = "Hello Java";

Here, we have created a string variable named value. The variable is initialized with the string Hello Java.

Two ways to create String in java

  • By literal

String first= “Hello”;

String second=”1234”;

Here, we are directly creating strings like primitive types. And in String we can store any type of data such as character ,integer ,float etc.

  • By Object

String value=new String (“Hello”);

String result=new String (“123”);

As we saw String is a class in java so we can create string using new keyword.

class Demo{
  public static void main(String[] args) {
    
    // create strings using literal
    String first = "Hello";
    String second = "123";
    
    // create a string using new keyword
    String value = new String (“Java”);
    String result = new String (“1234”);

    // print strings
    System.out.println(first);   // print Hello
    System.out.println(second);  // print 123
    System.out.println(value);   // print Java
    System.out.println(result);  // print 1234
  }
}

Create String using literals vs. new keyword

Now we know how strings are created using string literals and the new keyword, let’s see what the major difference between them.

  1. While creating strings using string literals

                 String value= “Hello”;

When we create string using literal so it will be store into String Constant pool in heap memory which is maintain by JVM.

Here, we are directly providing the value of the string (Hello). Hence, the compiler first checks the string constant pool to see if the string already exists.

  • If the string already exists, the new string will not be created. Instead, the new reference will points to the already existing string (Hello).
  • If the string doesn’t exist, the new string (Hello) will be created. In String constant pool.

Note- “by literal one time memory allocation will be happen in string constant pool if the new string is same it will refer to the existing string”.

2. While creating strings using the new keyword

String result = new String (“Hello”);

Here, the value of the string is not directly provided. Hence, a new "Hello" string is created in the heap memory. even though "Hello" is already present inside the heap memory.

Note-“by new keyword two times memory allocation will be happen in heap memory even though the new string is same as existing string

Let’s understand memory Allocation by Example-

For ex-   String s=hello; //memory allocation will be happen in string constant pool.

          String s1 = new String (“hello”); //memory allocation will be happen in heap memory and also it will refer existing string which is present in constant pool.
          
          String s2 =java; // memory allocation will be happen in string constant pool because java is new string.
          
          String s3 =hello; // memory allocation will not be happen in string constant pool because hello is already present in contant pool it will refer existing string.
          
          String s4 =new String(“hello”); // memory allocation will be again happen in heap memory even though hello is already present in heap memory

Which is the best way to create String-

 “By literal is the best to create string because one time memory allocation is happens in string constant pool. But by object two times memory allocation is happens it is consuming more memory thatswhy this way is not convenient ”

In Java String is immutable –

In Java, strings are immutable. This means, their values cannot be changed once they are created.

Let’s understand by the basic program-

Class Demo
{
   Public static void main (String arg [ ])
  {
   String value =Hello; //by literal
   System.out.println (value);
  }
          }   
Output- hello

Here, we have created a string variable named value. The variable holds the string “Hello”.

Now suppose we want to change the string

// add another string "java" to the previous String "Hello"
Value = value.concat (“java”); //reassign 

Here, we are using the concat() method to add another string java to the previous string. But directly we can’t change that String value hello.

Let’s see what is happening here,

  1. JVM takes the first string “Hello”
  2. creates a new string by adding “java” to the first string
  3. Reassign the new string “Hello java” to the value variable
  4. the first string “Hello” remains unchanged

Leave a Reply

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