Introduction to String Handling
- String is a collection of characters and it is a class it is there in “Java.lang” package.
- It is a predefined class.
How to create String :
We can create String in 3 ways
String s1= new String();
In the above statement just we are creating the String but we are not passing any value .
In the above statement just we can create String and passing the value
i.e., “Geek Labs” s2 is directly pointing to that object only
In the above statement “Geek labs” is the String literal and it is a compiletime constantEx1:
In ex1 s is pointing to the String literal “geek” after that s is concatenates the “labs” to the “geek”. After concatenation in s “geek labs” will be stored. Next it checks the condition and concatenate lab to the 's' whenever condition is failed the loop will be closed.
In the above example however we are analyze .i.e., is not done
In String class we have two main concepts they are
- String constant pool
1.String constant pool (SCP) :
- The String literals are stored in a pool called String constant pool which is located in non-heap memory.
- Whenever JVM encounters String literals it checks whether they are present in SCP or not. If not, then JVM insert them into SCP.
- At runtime whenever we see new object that will be created at Heap Memory that object is referring to the SCP.
The word immutable refers to “constant”. So here the String class is immutable because once object is created we cannot change the state of an object (read purpose only we are used).Ex:
How to create instance for string?
There are two ways.
1. Using assignment operator:
Syntax: String obj=”string”; Eg: String s = “abc”;
2. Using constructor:Method 1:
Method 2:Syntax: String object = new String(); Eg: String s = new String(); s=”abc”;
Helper Methods:-Syntax: String obj = new String(“string”); Eg: String s = new String(“abc”);
We have 60+ helper methods in String class but we use some methods only
This method is useful,if want replace any character with another character then we will use this replace() method.Syntax:String replace(char c1,char c2) Ex:
String s= “geek”; s=s.replace (‘g’,’G’);
In the above example it gives as the output “Geek”
length(): By using this method we find out how many characters are there for a particular string.Ex:
Output: 4String s= “geek”; s=s.length (s);
charAt(): This method is used to retrieve the character at particular index.Ex:
String s =“geek”; s= s.charAt ;
In the above example geek will be stored in an array(0-3) size of the array is 4
so we find out charAt  so it gives “e” as the output.
substring(): In this method we pass two parameters i.e., starting index and ending index, it gives the output like starting index to excluding last index.Ex:
String s=”geek”; S= s.subString (0,2);
In the above example geek stored in an array the size of the array is 4 the index starts from 0 to 3 so output will be “ge”String toLowerCase( ):
This method is useful to convert the all characters of the string into lower case and returns the output as lowered case string.Ex: String s = “GEEK”;
It will returns the output as geek.Srtring toUpperCase( ):
This method is usefull to convert the all characters present in the string to uppercase and gives the uppercased string as output.Ex:
String s = “geek”; System.out.println(s.toUppercase( ));
It wil gives the output as GEEK.String trim( ):
This method is usefull to remove the unwanted space present in the particular string.Ex:
String s = “ geek labs “; System.out.println(s.trim( ));
The thing which we have to remember here is, it will remove the space which is present before and after the string only but it will not remove the space between the two words of a string. The above example will returns the output as geek labs.
indexOf():This method is used to retrieve the index value at particular character.Ex:
Example:String s =“geek”; s= s.indexOf (‘e’) ;
output of above example ‘e’ located in the first index.
Here String is immutable then every time new object will be created in Heap Memory. In the above example memory wastage because of every time new object will be created
What is the specific reason for develop StringBuffer & StringBuilder?
As we know that string is the immutable class because of we cannot change the state of an object. So for the sake of developer perspective StringBuffer & StringBuilder concept has been introduced to over come the immutability of the strings. These two are mutable classes here we can change the state of an object.
- String class is not mutable. This is used to save memory instead of using String class.
- This is a thread safe class. If a thread acquires a lock on the instance of this class then no other thread can access this instance until it releases the lock.
- It is because of the reason that StringBuffer classes are by default Synchronized. These will prevents the another threads acting on same object.
- If you want to create mutable object then this class can be used.
- This class alters the same object but does not create new object as String class.
- The yield of reliability of the result is good.
- It is best recommended when the programmer wants to use multiple threads.
There are three ways to create instance for this class.Method 1:
StringBuffer obj = new StringBuffer();
Here obj refers to the reference
variable of the string.
StringBuffer sb = new StringBuffer( ); In this case we are just creating the object to the String but we are not passing any value to it.
StringBuffer obj = new StringBuffer(size);
The above syntax allocates memory for StringBuffer object based on
Ex: StringBuffer s = new StringBuffer(9); Here 9 will indicate the size of the string.
But even we can assign a string which size is greater than 9. The size canexpand dynamically as StringBuffers are immutable.
StringBuffer obj = new StringBuffer(“string”);
- Performance is slow
Methods in StringBuffer class:StringBuffer append(x):
This method is used to add another string or variable to to the present string.
Here ‘x’ represents any string, int, Boolean, char or double value
StringBuffer s = new StringBuffer(“geek”); System.out.println(s.append(“labs”));
Here we will get the output as geeklabs.StringBuffer insert(i,x):
This method is used to insert the value ‘i’ into the string. Here x may be any value which is of int, Boolean, char , double or any other string.Ex:
StringBuffer s = new StringBuffer(“welcome geek labs”); s.insert(7, “to”);
here by counting from 0 we welcome has 6 characters, so in place of 7 we will get output like this welcome togeek labs.StringBuffer reverse( ): This method is used to reverse the present string. Ex:
StringBuffer s = new StringBuffer(“string”); System.out.println(“s.reverse( ));
It will give the output as following gnirtsStringBuffer toString( ):
this method is used to convert the stringbuffer object to string.int lenth( ):
this method is used to find out the length of the string (i.e) the no of characters present in the string.Ex:
The output will be 11StringBuffer s = new StringBuffer(“hello world”); System.out.println(s.length( ));
- It is similar to StringBuffer class.
- All stringBuffer methods are valid in StringBuilder class.
- It is not a thread safe class. StringBuider is not synchronized it does not allows the prevention of multiple threads acting on same object.
- It is best recommended when the programmer wants to use a single thread as it improves the excecution time when compared to stringbuffer class.
- String Buider Performance is fast when compared to String buffer.
Creation of objects of StringBuilder is also same like StringBuffer. We use the following three methods.
Method1: StringBuilder sb = new StringBuilder( ): Method2: SringBuilder sb = new StringBuilder(“geek labs”); Method3: STringBuider sb = new StringBuilder(8);