String represents sequence of characters. In most of the application class variables, String is the most used instance variable. In this post, we will learn about Strings in Java.
String is an immutable class in Java. Which means, once a String object is created, it’s value can not be modified.
Creating a String object
//Literal based string creation: String s = "This is a String";
The above code creates a String reference object s, which is pointing to the String literal “This is a String”. Whenever we create a new string with above syntax, a new string object is created in String constant pool and a reference to the created object is returned back. String constant pool is a memory location, where all String constants are created and preserved by JVM. If the String literal is already present in the String constant pool, a reference of that instance is returned by the JVM.
This String constant pool is maintained by String class, which is empty initially. String class uses String.intern() method to check if already a String literal is present in the pool and returns the reference if present. Before Java 1.7, This pool was residing under Permanent Generation(PermGen space) part, which throws
OutOfMemory error if pool exceeds the memory. This part of memory is of fixed size. From Java version 1.7 onward, constant pool is part of Heap memory, which means it can use complete object heap memory and there by improves performance.
Using new operator for String creation
//Using 'new' keyword for String object creation: String s = new String("text"); String t = "text"; System.out.println(s == t); //returns false, as s and t are referring to different objects.
In above code, we are using new operator to create a String object. Here, A new object is created in object heap memory, as we are using new keyword. Also, another String literal object(“text”) is created in String constant pool, if it is already not present, for future references.
Few more String creation Examples
Following is an example of String creation using both new keyword and using string literal. Here, first line will create a new object in heap and another object in string constant pool, for future reference. Second line will create a new object only in heap memory and no object is created in string constant pool, as already object is present with same value(because of first line). For third and fourth statements, a reference for existing string literal is returned from String constant pool, as there is no change in string value “ASB”, which is already got created because of statement on line one.
String s1 = new String("ASB"); //2 Objects gets created(heap and SCP) String s2 = new String("ASB"); //1 Object is created in heap. String s3 = "ASB"; //A reference from SCP is returned. String s4 = "ASB"; //A reference from SCP is returned.
It is recommended not to create String objects with new keyword, as it will create a new object every time. Using String literals instead of new operator will return a reference of string, which is already present in String constant pool.
String class constructors
Few of the String class constructors are listed below.
- String s = new String() : An empty String class object is created in heap.
- String s = new String(“ASB”) : A new object will be created in heap memory area and a literal in SCP.
- String s = new String(StringBuffer sb) : An equivalent string object is created from given String buffer object.
- String s = new String(StringBuilder sb) : An equivalent string object is created from given String Builder object.
- String s = new String(char ch) : An equivalent string object is created from given character array.
String class important methods
Few important methods of String class are:
- public int charAt(int index) : Returns index of the character in a given string.
- public String concat(String s) : A new object is created and returned back back to reference variable. Ex: String s = s.concat(“ABC”);
- public boolean equals(Object o) : String class uses this method to compare content of two objects.
- public boolean equalsIgnoreCase(String s) : To compare with another String content, but ignore the case.
- public boolean isEmpty() : Returns true if string content is empty (String with value “”).
- public int length() : Returns length of the string.
- public String replace(char oldChar, char newChar) : Replaces all the oldChar with newChar value and returns new String back.
- public String substring(int beginIndex) : Returns string from beginIndex to end of the given string.
- public String substring(int beginIndex, int endIndex) : Returns String from beginIndex to endIndex of the given string.
That’s a few important things about java String class. String class is a very important class in java, which is used in almost every simplest Java program.