Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

Β 

History

History
History
44 lines (35 loc) Β· 993 Bytes

File metadata and controls

44 lines (35 loc) Β· 993 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package GeeksForGeeks;
public class StringPoolDemo {
public static void main(String[] args) {
String s1 = "hello"; // string lateral
String s2 = "hello"; // created in string pool
String s3 = s1;
String s4 = new String("hello"); // string object
String s5 = new String("hello"); // hello object is created in heap, and
// reference is stored in stack
System.out.println(s1 == s2); // same reference in string pool
System.out.println(s1 == s4);
System.out.println(s2 == s3);
System.out.println(s4 == s5); // reference are different in heap
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s4));
System.out.println(s2.equals(s3));
System.out.println(s4.equals(s5)); // match the content of object
// string are immutable
String s6 = s1 + " pool"; // s1 remains unchanged and new reference is created
System.out.println(s1);
System.out.println(s6);
}
}
/* output:
true
false
true
false
true
true
true
true
hello
hello pool
*/
Morty Proxy This is a proxified and sanitized view of the page, visit original site.