Java String Class, The java.lang.String class gives a considerable measure of methods to deal with string. By the assistance of these methods, one can perform operations on string, for example, trimming, concatenating, converting, comparing. Java String is an intense idea in light of the fact that everything is dealt with as a string in the event that user present any structure in window based, electronic or versatile application.
In Java String Class, toString() method is overrided to return the string value of the object.
The hashcode() method is also overrided to return the hash code value generated based on string value of the object.
The equals() method is also overrided in the Java String Class to compare 2 string objects based on the string value.
The Java String Class object created are stored in the string pool.
The string pool is divided into
1)String Non-Constant Pool.
2)String Constant Pool.
Description
Inside string constant pool duplicate string object are not created, where as inside non-constant pool, duplicate objects are created.
Whenever string object is created using new operator the object is created in string non-constant pool which allows duplicate object.
When the string objects are created using (“ ”) directly, then objects are created inside the string constant pool.
If one tries to create duplicate objects inside constant pool, the object will not be created. Instead, the existing objects is redefined.
Example
The following is an example for java String toUpperCase() and toLowerCase() methods
[java]
package com;
abstract class SimpleExample{
public static void main(String args[]){
String s="Splessons";
System.out.println(s.toUpperCase());//SPLESSONS
System.out.println(s.toLowerCase());//splessons
System.out.println(s);//Splessons(no change in original)
}
}
[/java]
In the above toUpperCase and toLowerCase methods are used to change the string physical text to lowercase and uppercase.
Output:
When compile the code result will be as follows.
[c]
SPLESSONS
splessons
Splessons
[/c]
String builder replace method.
[java]StringBuilder replace() method[/java]
Output:
[java]HJavalo [/java]
Example
The following is an another example to compare the given two strings by equals() method and '==' operator .
[java]
package com.spl.string;
public class StringClass1 {
public static void main(String[] args) {
System.out.println("Program starts");
//creating object using ""
String str="splessons";
String str1="splessons";
//returns a string value inside object
System.out.println(str.toString());
//returns a hash value based on the value inside the object
System.out.println(str.hashCode());
//compare str1 and str2
System.out.println(str==str1);
//compare values inside object
System.out.println(str.equals(str1));
System.out.println("Program ends");
}
}
[/java]
The equals() method of an object class compares the current object with argument object.Returns true, if both the object are same else false. The == operator is utilized to compare 2 objects, it expels to find if the given objects refer to the same place in memory or not.
Output:
When compile the code result will be as follows.
[c]
Program starts
splessons
289831230
true
true
Program ends
[/c]
Immutable
Description
Immutable means no change. To have immutable, class must be final i.e. one cannot change the values of object member after instance creation. If anyone tries to change the values, JVM will create another object with that values. The following is an example.
[java]
package com;
abstract class Sample2{
public static void main(String args[]){
String s="Welcome to splessons";
s.concat(" Stop thinking start coding");//concat() method appends the string at the end
System.out.println(s);//will print Sachin because strings are immutable objects
}
}
[/java]
Output:
When compile the code result will be as follows.
[c]Welcome to splessons[/c]
The following is an example to reverse the string.
INPUT: Welcome to splessons
OUTPUT: snosselps ot emocleW
[java]package com;
import java.io.*;
import java.util.*;
public class reverseString {
public static void main(String[] args) {
String input="Welcome to splessons";
StringBuilder input1 = new StringBuilder();
input1.append(input);
input1=input1.reverse();
for (int i=0;i<input1.length();i++)
System.out.print(input1.charAt(i));
}}[/java]
In the above example reverse() method of the StringBuilder class has been used. There is no reverse() method for the string. So convert the input string to StringBuilder , which is achieved by using the append method of the StringBuilder.
Output:
Now compile the code result will be as follows.
[java]snosselps ot emocleW[/java]
String Buffer and String Builder:
Description
Java provides two classes String builder and String buffer to store string values. Both these classes are mutable i.e, the value can be modified after the object creation.
In both the classes only toString() method is overrided. hashcode() and equals() methods are not overrided.
In both the classes, there are methods for appending, inserting and modifying characters of the string.
In both the classes, reverse() method is available.These classes are present in java.lang package.
Difference between string buffer and string builder is ,string buffer is a thread safe class where as builder is not a thread safe.
String buffer and builders are mutable but string class is immutable.
Example
The following is an example for the StringBuilder and StringBuffer.
[java]
package com.spl.string;
public class BufferBuilder {
public static void main(String[] args) {
System.out.println("Program starts");
//string buffer
System.out.println("StringBuffer");
StringBuffer sb1=new StringBuffer("splessons");
System.out.println("sb1="+sb1);
StringBuffer sb2=new StringBuffer("splessons");
System.out.println("sb2="+sb2);
//gives false because both methods are not overrided
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.reverse());
//string builder
System.out.println("StringBuilder");
StringBuilder sb3=new StringBuilder("splessons");
System.out.println("sb3="+sb3);
StringBuilder sb4=new StringBuilder("splessons");
System.out.println("sb4="+sb4);
//gives false because bothe methods are not overrided
System.out.println(sb3==sb4);
System.out.println(sb3.equals(sb4));
System.out.println(sb3.reverse());
System.out.println("Program ends");
}
}
[/java]
When compile the code result will be as follows.
Output:
[c]Program starts
StringBuffer
sb1=splessons
sb2=splessons
false
false
snosselps
StringBuilder
sb3=splessons
sb4=splessons
false
false
snosselps
Program ends[/c]
Summary
Key Points
The string trim() method disposes of white spaces previously and after the string.
The string charAt() method gives back a character at determined index.