String - String Literal and String Object
When you create a string literal(syntax String str1 ="abc") string is interned and memory is allocated in String constant pool in Method area. When you create a String object (syntax String str1 =new String("xyz")) , memory is allocated in heap as well as Method Area but it is referenced to heap.
Demo 1:
Output:
Literals are referring the same location(address): true
Objects are referring the same location(address): false
Literal and Object are referring the same location(address): false
From the comparison of addresses it is clear that the reference of a string literal is pointing to the same element abc and returns true whereas when we are creating two different object it will have two different existence in heap area.
let us understand the use of intern function
Demo 2:
Output:
Enter "Hi"
Hi
Before using intern function: false
After using intern function: true
Intern is use to return the reference to constant pool string literal. In this case if the literal is already available it will point to it otherwise it will create a literal in constant pool and refer to it.
One more use of intern.
Demo 3:
Output:
Before using intern function: false
After using intern function: true
There are three literals created in string constant pool "abc" , "ab" and "c". When we add two string literals it creates a new String on heap memory using StringBuilder to append "c" with "ab". The comparison of str1 and str4 returns false as str1 points to constant pool and str4 points to heap. When we intern str4 string it returns the reference of constant pool "abc" as it is already existing it will not create second literal in constant pool.
Note: String constant pool memory space is a part of Method Area and Method area can be a part of Heap or not a part of Heap as it depends on JVM implementation.
When you create a string literal(syntax String str1 ="abc") string is interned and memory is allocated in String constant pool in Method area. When you create a String object (syntax String str1 =new String("xyz")) , memory is allocated in heap as well as Method Area but it is referenced to heap.
Demo 1:
package com.a.b;
/**
*
* @author Ganesh.Rashinker
*
*/
public class StringDemo1 {
public static boolean checkLiteral() {
String str1 = "abc";
String str2 = "abc";
//memory allocated to accomodate one "abc" and that is why it will return true
return (str1 == str2);
}
public static boolean checkObject() {
String str1 = new String("abc");
String str2 = new String("abc");
//as new allocates new memory there will be different memory allocated for each that is why it will return false
return (str1 == str2);
}
public static boolean checkLieralnObject() {
String str1 = "abc";
String str2 = new String("abc");
return (str1 == str2);
}
public static void main(String[] args) {
System.out.println("Literals are referrring the same location(address): " + checkLiteral());
System.out.println("Objects are referrring the same location(address): " + checkObject());
System.out.println("Literal and Object are referrring the same location(address): " + checkObject());
}
}
/**
*
* @author Ganesh.Rashinker
*
*/
public class StringDemo1 {
public static boolean checkLiteral() {
String str1 = "abc";
String str2 = "abc";
//memory allocated to accomodate one "abc" and that is why it will return true
return (str1 == str2);
}
public static boolean checkObject() {
String str1 = new String("abc");
String str2 = new String("abc");
//as new allocates new memory there will be different memory allocated for each that is why it will return false
return (str1 == str2);
}
public static boolean checkLieralnObject() {
String str1 = "abc";
String str2 = new String("abc");
return (str1 == str2);
}
public static void main(String[] args) {
System.out.println("Literals are referrring the same location(address): " + checkLiteral());
System.out.println("Objects are referrring the same location(address): " + checkObject());
System.out.println("Literal and Object are referrring the same location(address): " + checkObject());
}
}
Output:
Literals are referring the same location(address): true
Objects are referring the same location(address): false
Literal and Object are referring the same location(address): false
From the comparison of addresses it is clear that the reference of a string literal is pointing to the same element abc and returns true whereas when we are creating two different object it will have two different existence in heap area.
let us understand the use of intern function
Demo 2:
package com.a.b;
import java.util.Scanner;
/**
*
* @author Ganesh.Rashinker
*
*/
public class StringDemo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter \"Hi\"");
String str1 = sc.next();
String str2 = "Hi";
System.out.print("Before using intern function: ");
System.out.println(str1 == str2);
str1 = str1.intern();
System.out.print("After using intern function: ");
System.out.println(str1 == str2);
}
}
import java.util.Scanner;
/**
*
* @author Ganesh.Rashinker
*
*/
public class StringDemo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter \"Hi\"");
String str1 = sc.next();
String str2 = "Hi";
System.out.print("Before using intern function: ");
System.out.println(str1 == str2);
str1 = str1.intern();
System.out.print("After using intern function: ");
System.out.println(str1 == str2);
}
}
Output:
Enter "Hi"
Hi
Before using intern function: false
After using intern function: true
Intern is use to return the reference to constant pool string literal. In this case if the literal is already available it will point to it otherwise it will create a literal in constant pool and refer to it.
One more use of intern.
Demo 3:
package com.a.b;
/**
*
* @author Ganesh.Rashinker
*
*/
public class StringDemo3 {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "ab";
String str3 = "c";
String str4 = str2 + str3;
System.out.print("Before using intern function: ");
System.out.println(str1 == str4);
str4 = str4.intern();
System.out.print("After using intern function: ");
System.out.println(str1 == str4);
}
}
/**
*
* @author Ganesh.Rashinker
*
*/
public class StringDemo3 {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "ab";
String str3 = "c";
String str4 = str2 + str3;
System.out.print("Before using intern function: ");
System.out.println(str1 == str4);
str4 = str4.intern();
System.out.print("After using intern function: ");
System.out.println(str1 == str4);
}
}
Output:
Before using intern function: false
After using intern function: true
There are three literals created in string constant pool "abc" , "ab" and "c". When we add two string literals it creates a new String on heap memory using StringBuilder to append "c" with "ab". The comparison of str1 and str4 returns false as str1 points to constant pool and str4 points to heap. When we intern str4 string it returns the reference of constant pool "abc" as it is already existing it will not create second literal in constant pool.
Note: String constant pool memory space is a part of Method Area and Method area can be a part of Heap or not a part of Heap as it depends on JVM implementation.