What is String?
A string is a sequence of Characters, Anything you write inside double quotes ex:"Hello ishu.dev"
is considered a String in Java.
The string is a Non-primitive data type, it's a Class in nature.
Ways to create String in Java?
There are two ways you can create String Object.
1. String Literal
String Literal = "" (double quotes)
whatever you write inside double quotes is considered a string in java.
example
public class Test{
public static void main(String[] args) {
String s = "ishu.dev";
System.out.println(s);
// ishu.dev
}
}
2. String Class Constructor
Using new Keyword with String class.
example
public class Test{
public static void main(String[] args) {
String s = new String("ishu.dev")
System.out.println(s);
//ishu.dev
}
}
String Interview Notes:
- String represents a sequence of character
- Many operations can be performed on a String
- All the operations are segregated into a single class in the form of methods
- The class which consists of these methods is String Class
- It is present in “java.lang” package
- It is a final class
- The string has an immediate super class called as “Object Class”. It doesn’t have any sub class.
String immutability.
String Class is a final class that's why it is immutable.
What is immutable?
An Object is said to be immutable when it cannot be changed/modified after its creation.
String Objects
The string is immutable in Java, it is a very important concept to understand about String.
The object of the String can’t be modified once created, even if the programmer tries to modify them using a readymade method JVM creates a new object with a modified String instead of updating the previous object.
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "ishu.dev";
System.out.println(s);
System.out.println(s.hashCode());
//replace the value
s = "new value";
System.out.println(s);
System.out.println(s.hashCode());
}
}
/*
Output
ishu.dev
382153054
new value
-2054449423
*/
In the above example, When we change the value, the hashcode comes is different, which that means rather than changing the value in the same place, it created a new Object with the value "new value" and assigned it to variable s.
(hashcode is a unique code assign to Java Object to identify in the memory)
a. The object of String is an immutable object
b. There are two types of immutability i.e. Weak & Strong
c. String object has a Strong Immutability because the class is final
d. In the case of weak immutability methods of the class are final
Memory Allocation of String Objects.
Both types of Objects (literal and Class) are stored in different memories. let's understand where there are stored.
1. Literal Object
All Objects created using literals are stored in Special memory Called "String Constant Pool".
What is String Constant Pool (SCP)?
String Constant pool is a special memory in Heap memory where all the String literal Objects are stored.
The benefit of the String constant pool is, if you create an Object using literal and again create an Object with the same value, then no new Object will be created in memory just the new Object will get a reference of the previously created Object.
Lets us understand with an example:
package demo;
public class Test {
public static void main(String[] args) {
String s1 = "Spring";
System.out.println("s1 reference" + " " + s1.hashCode());
String s2 = "Spring";
//Spring Object is already created in String Constant Pool
//and assigned to s1 reference, now s2 will have a reference
//of "Spring" value which is already created in Constant Pool
// Now, s1 and s2 both pointing to same value.
System.out.println("s2 reference" + " " + s2.hashCode());
String s3 = "Spring";
// s3 also pointing to same value
System.out.println("s3 reference" + " " + s3.hashCode());
System.out.println("testing".hashCode());
String s4 = "testing";
System.out.println("s4 reference" + s4.hashCode());
if (s1 == s2 && s2 ==s3 && s1 ==s3) {
System.out.println("Objects have the same reference :s1=s2=s3");
} else {
System.out.println("Objects does not have the samereference");
}
}
}
/*
Output
s1 reference -1811812819
s2 reference -1811812819
s3 reference -1811812819
-1422446064
s4 reference-1422446064
Objects have the same reference :s1=s2=s3
*/
In the above example, all s1,s2,s3 are referring to the Same value in String Constant Pool (SCP), that's why all the references are the same.
2. String Class Object
All String class Objects are stored in heap memory, all references are different to the same value. All Objects in heap memory are unique.
example
public class Test {
public static void main(String[] args) {
String s1 = new String("ishu.dev");
String s2 = new String("ishu.dev");
System.out.println("s1 reference : " + s1.hashCode());
System.out.println("s2 reference : " + s2.hashCode());
System.out.println("Verify s1 reference is equal to s1 or not :" + s1 == s2);
}
}
/*
Output
s1 reference : 382153054
s2 reference : 382153054
false
*/
Watch my video to understand it through video tutorial.