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

  • SEARCH
  • Home
  • 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 9164579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:43:03+00:00 2026-06-17T14:43:03+00:00

I have two questions: public static void main(String[] args) { String s1 = bla;

  • 0

I have two questions:

public static void main(String[] args) {
  String s1 = "bla";
  String s2 = "b" +"l" + "a";
  String s3 = "b".concat("l").concat("a");

  if(s1 == s2) 
        System.out.println("Equal");
  else
        System.out.println("Not equal");
  if(s1 == s3) 
        System.out.println("Equal");
  else
        System.out.println("Not equal");
}
  • Why does s1 and s2 point to the same object, whereas s1 and s3 doesn’t? (There is no usage of new keyword).

  • If I get a string from the user and add to the above code these lines:

    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    String name=in.readLine();
    if(name.equals("test"))
        s1 = s1 + "xyz";
    

    If the user enters xyz the program will print Not equal, when the user enters another thing the program outputs Equal. Does this mean that the pool changes through the execution of the whole program? Does the optimizer works at the compile time and continues to work in the runtime?

  • 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-06-17T14:43:05+00:00Added an answer on June 17, 2026 at 2:43 pm

    Why does s1 and s2 point to the same object, whereas s1 and s3 doesn’t? (There is no usage of new keyword).

    Because the concatenation happens at compile time, and the completed string therefore goes in the constant pool the same as in the first example. It’s a special case “known” to the compiler. It’s really meant so that long strings, concatenated this way over several lines, still get the benefit of the same performance improvements as simple string constants.

    In the second example, you’re performing the calculation at runtime, so it won’t be part of the constant pool.

    Note however that in the JLS the details of what can and can’t go in the string constant pool is deliberately left vague, so different implementations may optimise in different ways. It specifies certain rules as to what has to go in there, but don’t rely on this behaviour being consistent across implementations.

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

Sidebar

Related Questions

I have written this piece of code public class Test{ public static void main(String[]
i have this example: class One { public void testOne(){System.out.println(One!!!);} public void testTwo(){System.out.println(One!!!);} }
I have IMessageSender interface. using System.ComponentModel.Composition; public interface IMessageSender { void Send(string message); }
I have two classes : import android.cla; public class CW { public static void
I have a question, here are two classes below: class Base{ public: virtual void
I have two question on this code public class Override { private void f()
I have two questions here. How can I show the header of the pop
I have two questions. How can I get the spinner in the figure below
I have two questions. Does it make sense when I have to choice of
I have two questions. Question1: How can we add a reference to a third-party

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.