String comparison with intern

String comparison with intern

I was trying to understand String#intern method. Now it has caused even more confusion.

I was trying to understand String#intern method. Now it has caused even more confusion.

package com;

public class Main {
public static void main(String[] args) {

    String s1 = new String("GFG"); // Line-1  
    String s2 = s1.concat("GFG"); // Line-2  
    s2.intern(); // Line-3
    String s4 = "GFGGFG"; // Line-4

// s2.intern(); // Line -5 System.out.println(s2 == s4); } }

The above code prints true. If I comment line 3 and uncomment line 5 it is printed false.

For line 3 the SCP is checked and the String is added to SCP. But how did s2 equals to s4 in that case ?

s2 is still referencing to an object in heap which is pointing to SCP constant. Right ?

Can anyone please explain what is happening ? I've gone through different SO questions, but was not able to understand it still.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners

Java Essentials : Learn Core Java From Basic to Advance

Learn Java Programming Using Practical Assignments. Start Building Back-end Web Applications Robust Test Automation Frameworks By End Of The Course. Learn More!

Fundamentos de Java: Aprende Java desde cero, sin misterios | Simpliv

Fundamentos de Java: Aprende Java desde cero, sin misterios