Generally, String is a sequence of characters. But in java, String is an object that represents a sequence of characters. String class is used to create string object.
String can be defined as a ‘sequence of characters that is treated as a single data item and initialized by null’ like String x = null;
String class is an immutable class which is present inside java.lang package. Immutable means it cannot be changed.
Now, the question arises as to why the String class is immutable. Memory in Java is divided into three parts, Heap, Stack and String Pool. String is so important in java that it has its own dedicated memory location. What String pool does it that whenever a string gets repeated, it does not allocate new memory for that string. Instead, it uses the same string by making a reference to it, thereby saving memory.
We can create String object in two ways :
String s1 = "javabykiran";
String s2 = new String("javabykiran");
Internally, String class uses the String Pool concept. In String pool, we do not have duplicate strings.
String s1 = "javabykiran";
String s2 = "javabykiran";
//will not create new instance.In the above example, only one instance will be created. First, JVM will not find any string with the value "javabykiran" in string constant pool, so it will create a new instance. After that, for s2, it will find the string with the value "javabykiran" in the pool. This will not create a new instance but will return the reference to the same instance of s1.
In java, there are two equals () methods at two places. One in Object class and another in String class.
The equals () method of object will check reference of objects i.e. addresses. Whereas equals method of String will check contents of string with character sequence.
In java, string objects are immutable. Immutable simply means unmodifiable or unchangeable. Once string object is created, its data or state can't be changed but a new string object is created.
Example: Consider the above example of s1 and s2. Now, String s3 = s1 + s2;
// s3 will be "javabykiranjavabykiran”.
In this case, s1 and s2 will not change and still exist after concatenation ('+' sign is overloaded in java, if it gets digits it adds two numbers and if strings then it concatenates).
The advantages of a String Pool are stated below:
Example:
String st1="jbk";
String st2="pune";
String st3="kiran";
st1=st2;
st3=st1;
System.out.println(st1);
System.out.println(st2);
System.out.println(st3);
When you create a String object without a new operator, the following task happens
String literal will be taken and verified in string constant pool.
If literal is not available in pool then a new object will be created in the pool and that address will be assigned to reference variable.
If string literal is available in the pool then the available object address will be assigned to reference variable.
Program - Lab1
package com.javabykiran;
class A {
String s;
public A(String s){
this.s = s;
}
}
public class StringLab1 {
public static void main(String[] args){
A a = new A("kiran");
A a1 = new A("jbk");
A a2 = new A("kiran");
String x = "javabykiran";
String y = "jbk";
String z = new String("javabykiran");
//guess true or false without seeing output
System.out.println(x==z);
System.out.println(y==z);
System.out.println(x.equals(y));
System.out.println(x==y);
System.out.println(x.equals(z));
System.out.println(a==a);
System.out.println(a==a1);
System.out.println(a1==a2);
System.out.println(a.equals(a2));
System.out.println(a1.equals(y));
}
}
Output:
false
false
false
false
true
true
false
false
false
false
The "==" operator in Java is used to compare two objects. It checks to see if the objects refer to the same place in memory. In other words, it checks to see if the two object names are basically references to the same memory location.
Program -Lab2
package com;
public class StringLab2 {
public static void main(String[] args) {
String s1 = "Hello"; // String literal
String s2 = "Hello"; // String literal
String s3 = s1; // same reference
String s4 = new String("Hello"); // String object
String s5 = new String("Hello"); // String object
System.out.println(s1 == s1); // true,same pointer
System.out.println(s1 == s2); // true,s1 and s2 share
//storage in pool
System.out.println(s1 == s3); //true, s3 is assigned same
//pointer as s1
System.out.println(s1.equals(s3)); // true, same contents
System.out.println(s1 == s4); //false, different pointers
s1.equals(s4); // true, same contents
System.out.println(s4 == s5); // false, different pointers in
//heap
System.out.println(s4.equals(s5)); // true, same contents
}
}
Output:
true
true
true
true
false
false
true
In java, StringBuffer is a class present in java.lang package. It provides the same functionality as String class but there are also some additional features in StringBuffer class.
It is slower than String as it has synchronised methods, so it is a Thread-safe class.
The String class is immutable, i.e objects are unmodifiable, whereas StringBuffer class provides mutable sequence of characters. A stringBuffer is like a String, but can be modified.
The principal operations on a StringBuffer are append() and insert() methods, which are overloaded so as to accept data of any type. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point. For example, if "s" refers to a String Buffer object whose current contents are "java", then the method call s.append("kiran") would cause the String Buffer to contain "javakiran", whereas z.insert(4, "by") would alter the String Buffer to contain “javabykiran”.
Lab Examples:
package com.javabykiran;
//use of append method
public class StringBufferLab1 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("java");
sb.append("bykiran"); //now original String is changed
System.out.println(sb); //will print javabykiran
}
}
//using insert method
package com.javabykiran;
public class StringBufferLab2 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("java");
sb.insert(4,"bykiran");//now original String is changed
System.out.println(sb); //will print javabykiran
}
}
//default capacity
package com.javabykiran;
public class StringBufferLab3 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity()); //default capacity is 16
sb.append("java");
System.out.println(sb.capacity());// again will be 16
sb.append("is my best lang");
//exceeds 16
System.out.println(sb.capacity());//so now (16*2+2)
}
}
Output:
16
16
34
String | StringBuffer |
---|---|
1. It is an immutable class, i.e. its objects cannot be changed. Example: package com;
|
1. It is a mutable class, i.e its objects can be changed only once. Example: package com;
|
2. String object can be created in two ways:
|
2. StringBuffer object can be created in only one way
|
3. String object will use String constant pool. | 3. In StringBuffer object there is no concept of String constant pool. |
The appropriate class should be used according to the given requirements. Study the points given below:
String class should be used when the content is fixed and will not change frequently.
StringBuffer should be used when the content is not fixed and frequently changes, but thread safety is required.
StringBuilder should be used when the content is not fixed and frequently changing and thread safety is not required.
String is immutable, whereas StringBuffer and StringBuilder are mutable.