Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1097719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:28:24+00:00 2026-05-17T00:28:24+00:00

public class Demo { public static void main(String[] args) { String s1 = Hello;

  • 0
public class Demo {  

    public static void main(String[] args) {

        String s1 = "Hello";
        String s2 = "Hello";
        System.out.println("s1 == s2 " + (s1 == s2));

        String s5 = "Hel" + "lo";
        String s6 = "He" + "llo";
        System.out.println("s5 == s6 " + (s5 == s6));

        String s7 = "He";
        String s8 = "Hello";
        s7 = s7.concat("llo");
        System.out.println("s7 == s8 " + (s7 == s8));

        String s10 = "He";
        s10 = s10 + "llo";
        System.out.println("s1 == s10 "+(s1 == s10));
    }
}

In the preceding code s7 == s8 and s1 == s10 give false. Can someone please explain me, what is actually happened here in s7 = s7.concat (“llo”); and s10 = s10 + “llo”; I understand == operator checks reference and equal() checks content of object. But I need to know why s7 and s10 reference variables bit patterns are different from s8 and s1. If these things are related with compile time generated strings and run time generated strings then how can I identify whether it is compile time or run time string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:28:25+00:00Added an answer on May 17, 2026 at 12:28 am

    The reason this is happening is because Java is optimizing in the compiler. When it sees that you’re assigning the literal string "Hello" to s1, it uses the same “Hello” for s2, since all Java String operations are non-destructive (eg they return a clone rather than modify the original), so that’s a safe thing to do.

    Same thing goes for "Hel" + "lo" vs "He" + "llo"; it’s clever enough to figure out that they’re the same thing.

    The others are complex enough that it can’t optimize them, and thus you end up with separate objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a simple program: class Demo{ public static void main(String[] args){ system.out.println("Hello");
import java.io.*; class demo { public static void main(String args[]) { PrintWriter pw=new PrintWriter(System.out);
public class Demo { public static void main(String args[]) { double d = 12.345;
import java.io.*; public class Demo{ public static void main(String[] args){ File f = new
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
Demo code: import java.lang.ref.PhantomReference; import java.lang.ref.ReferenceQueue; public class Main { public static void main(String[]
public class MainClass { public static void main(String [] args) throws ClassNotFoundException{ //String[] xyz
public class Demo { public static String doit(int x,int y) { returna; } public
At first step I run this code: public class Demo { public static void
// following code works fine n open notepad... class demo { public static void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.