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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:39:10+00:00 2026-05-20T14:39:10+00:00

Acordding to my knowledge in java I know, that there is no operator overloading

  • 0

Acordding to my knowledge in java I know, that there is no operator overloading in the Java language.
So, why this code prints ‘true’ twice ?

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

    Integer i1 = 1;
    Integer i2 = 1;
    System.out.println(i1==i2);
  • 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-20T14:39:11+00:00Added an answer on May 20, 2026 at 2:39 pm

    == for reference types compares the references; == for primitive types compares values. In case of your first example, the two object references turn out to be the same due to a concept known as string pool. Hence two true in the given case. Another code snippet you might want to try out:

    String s1 = "abc";
    String s2 = new String("abc");
    System.out.println(s1 == s2);
    System.out.println(s1.equals(s2));
    

    As you must have already tried out; it prints out false and then true. The reason for this is that using the new keyword results in the creation of a completely new string even though a string object with the exact same contents already exists in the string pool. In this case, s1 now points to an interned string with the contents “abc” (or to a string in the string pool) whereas s2 now points to a completely new string object (again with the content “abc”). Hence the false in the first print statement.

    In the second print statement, what we are doing is comparing the contents of the String object rather than its reference, which as it should prints true.

    This is one of the most common mistakes made by beginners of the Java language; they use == for logical comparison when it actually results in a reference comparison. Read the link posted in one of the answers here for more details about string pooling. On a related note, String class “overrides” the equals method of the Object class to provide a logical comparison. Unless the class you write doesn’t provide a logical implementation of the equals method, it doesn’t matter whether you call equals or use the == operator; the result would be the same i.e. reference comparison.

    For a more in-depth view on equality, read Brian’s article; an excellent read.

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

Sidebar

Related Questions

According to this discussion , the iphone agreement says that it doesn't allow loading
hi all as i know that asp.net provide server side validation control which has
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to
According to MSDN form.RightToLeftLayout = True; form.RightToLeft = ifWeWantRTL() ? RightToLeft.True : RightToLeft.False; is
According to what I have found so far, I can use the following code:
According to the answers to this question, I cannot embed a file version in
According to Wikipedia, on the Comparison of programming languages page, it says that F#
According to this article Silverlight 2 Beta 2 supports the DataContractJsonSerializer object. But, when
According to this http://steve-yegge.blogspot.com/2007/06/rich-programmer-food.html article, I defnitely should. Quote Gentle, yet insistent executive summary:
Ok here is my situation. I've studied Java in my college for 2 semesters.

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.