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 7804061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:47:34+00:00 2026-06-02T01:47:34+00:00

public class Test { int multiple; public static void main(String[] args){ String string1 =

  • 0
public class Test {
    int multiple;
    public static void main(String[] args){

        String string1 = "string";
        String string2 = "string";
        String string4 = "Changed";

        String string3 = new String("string");

        System.out.println("string1 == string2: " + (string1 == string2));\\true
        System.out.println("string1 == string4: " + (string1 == string4));\\false
        System.out.println("string1 == string3: " + (string1 == string3));\\false

    }


}

I understand that the == operator will return true if the references are same. What I want to know is, Does Java check the content of string literals before creating their objects?

  • 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-02T01:47:36+00:00Added an answer on June 2, 2026 at 1:47 am

    It is the compiler that does the string interning. So at compile time identical strings are optimised. So I think the answer you want is “no”. The Virtual Machine doesn’t do it, it is the compiler. You can call String.intern() to acquire the shared string object in the string pool:

    String str1 = "string";
    String str2 = new String("string");
    String str3 = str2.intern();
    
    str1 == str2 // false
    str2 == str3 // false
    str1 == str3 // true
    

    Strings, build at runtime are not interned automatically.

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

Sidebar

Related Questions

I have following code public class TEST { public static void main(String arg[]){ try
class test{ int x; public: //public functions getx() and putx() }; void main() {
say I have: class Test { public static int Hello = 5; } This
public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){
Here is my code: public class Test { static { main(null); } public static
I have the following code : using System.Collections.Generic; public class Test { static void
Lets say i have this usercontrol public class test : UserControl { public int
I have an entity and its mapping: public class Test { public virtual int
class Test { public: SOMETHING DoIt(int a) { float FLOAT = 1.2; int INT
I have the following class to test: public abstract class Challenge { protected int

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.