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 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

Accoriding to javadoc, public class AtomicInteger extends Number implements java.io.Serializable { // code for
According MSDN, FxCop is an application that analyzes managed code assemblies (code that targets
according to my knowledge, this feature already exists in PHP. lets look at the
It appears to be common knowledge that interop with Excel and .Net is very
I've run into a rather interesting problem. The code below generates an array that,
Is it possible in Java to create a static factory method/class that uses an
Some background: I working on some java packages that need to take data, divide
hey all, is there any way to convert a given file (this could be
Given that I have the following in my knowledge-database: 1 0 6 20 0
I will be forward in admiting that my knowledge of Lisp is extremely minimal.

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.