Creating And Manipulating Strings In Java

Introduction 

 
The string class is a fundamental class because it is very hard to write the code without it. You can even write main() method without using the String class.
 
A string is a sequence of characters, as shown below. 
  1. String name = “Deepak”;  
Above are the examples of reference types in Java. As we have learned the reference type is created using new keywords in Java but wait a second, in the example given above, we don’t have a new one in it. In Java, we can create a string by having two types, which are given below. 
  1. String name = “Deepak”;  
  2. String name = new String(“String”);   
Both lines will give you a reference variable of type “name” pointing to the string object “Deepak”. Thus, we can say that the String class in Java is a special class and does not require new to be instantiated.

 
String concatenation

 
Combining two or more strings together is called string concatenation. Java does not provide lots of rules to learn about string concatenation but you have to know them well.
  1. If the operands are numeric, + means numeric addition (System.out.println(2+3+4); // 9).
  2. If the operands are string, + means concatenation (System.out.println(“2”+”3”+”4”); // 234).
  3. An expression is evaluated left to right (combination of first and second rule).(System.out.println(“2”+”3”+”4”+5); // 2345, System.out.println(2+3+”4”); // 54)
An example of all three rules are described above.
  1. int one = 1;  
  2. String two = "2";  
  3. System.out.println(1 + 2 + one + two);   
In the above example, we will start with the first rule, like 1+2 will give us 3, then next 3+one and one is of type int, will give us 4. Since two is type string, we get the final answer as 42.
We need to know the similarity about +=, what += does, as deepak += “1” means the same thing as deepak = deepak + “1”.
  1. String deepak = "1";  
  2. deepak += "2";  
  3. deepak += 3;  
  4. System.out.println(deepak);  

 
String Immutability

 
An object that can’t be changed, once it is created; is called immutable. Immutable simply means unmodifiable or unchangeable.
After creating an object of String class, we can’t change it, which means it can’t be made larger or smaller or can’t change the character inside it.
  1. public static void main(String args[]) {  
  2.  String s = "Deepak";  
  3.  s.concat(" Kumar"); // concat() method appends the string at the end    
  4.  System.out.println(s); // will print Deepak because strings are immutable    
  5.  // objects    
  6. }   
If we want to print Deepak Kumar, then it needs to write, as shown below.
  1. public static void main(String args[]) {  
  2.  String s = "Deepak";  
  3.  s = s.concat(" Kumar");  
  4.  System.out.println(s);  
  5. }   

Why is the string immutable in Java?

 
Since Java uses the literal concept for the string,  there is more than one reference variable, referring to an object deepak (String pool concept). If on reference, the variable will change the value of the object, then it will affect all the reference variables. Due to this, a string is immutable in Java.
 
String Pool

Since the strings are everywhere in Java, they use a lot of memory. String can use lots of memory of the entire program or an Application. Java realizes that many strings repeat in the program and solve this issue by reusing common ones. The string pools (also know as intern pool.) are the locations in JVM, which collect all the strings. In other words, we can say that the string pool is a mechanism to contain only one copy of each distinct string value.
String pool saves lots of space of Java runtime although it takes time to create the string.
String can be initialized by following two ways,

Method 1
  1. String name ="Deepak";  
When we use the method given above to create a string, then it checks the same value in a string pool. It found the same value in the string pool, then it returns the reference of the existing one and the other one creates a new string in the pool and returns the reference. 

Method2
  1. String name = new String(“Deepak”);  
When we used the method given above to initialize the string, then it created a new string object in Java heap memory and put the value of the string in the string pool or refers to the same string value object from the string pool.