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.

java

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java Core (2020) | Java for beginners | Brush up your Java Skills | Get Hired

In this video we will learn about all the major concepts that come under Java Core . The course is very carefully designed and is made with keeping simplicit...

Java File Class Tutorial | Java.io File Class in Java Example

Java File class represents the path of directories and files. It provides the methods for renaming, deleting, and obtaining the properties of file or directory.

Best Ways To Improve Skills In Java and Python Programming

Join CETPA, India's best institute for Java online training and Certification Course. Enroll now to learn Core to Advanced Java & avail 50% discount on Java Online Course with Certificate.

Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners