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 String class.
A string is a sequence of characters, as shown below.
Above are the examples of reference type in Java. As we have learned that 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.
- String name = “Deepak”;
- 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 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.
- If the operands are numeric, + means numeric addition (System.out.println(2+3+4); // 9).
- If the operands are string, + means concatenation (System.out.println(“2”+”3”+”4”); // 234).
- 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)
Example with all three rules are described above.
- int one = 1;
- String two = "2";
- System.out.println(1 + 2 + one + two);
In the above example, we will start with 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”.
- String deepak = "1";
- deepak += "2";
- deepak += 3;
- System.out.println(deepak);
String Immutability
An object that can’t be changed, once it is created; it 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.
- public static void main(String args[]) {
- String s = "Deepak";
- s.concat(" Kumar");
- System.out.println(s);
-
- }
If we want to print Deepak Kumar, then it needs to written, as shown below.
- public static void main(String args[]) {
- String s = "Deepak";
- s = s.concat(" Kumar");
- System.out.println(s);
- }
NOTE
Why is the string immutable in Java?
Since Java uses literal concept for the string, there is more than one reference variable, refering 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 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 solves 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 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
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 return the reference.
Method2
- 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.