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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:17:41+00:00 2026-06-17T01:17:41+00:00

Possible Duplicate: String comparison and String interning in Java I have small doubt regarding

  • 0

Possible Duplicate:
String comparison and String interning in Java

I have small doubt regarding String comparisons in Java, consider the following code:

if("String".replace('t','T') == "String".replace('t','T')) {
  System.out.println("true");
}
else {
  System.out.println("false");
}

The above code always print’s false, where as if I try like this:

if("STring" == "STring") {
  System.out.println("true");
}
else {
  System.out.println("false");
}

It will always print me true. Yes, I know String comparisons should be done with String.equals() or equalsIgnoreCase() method. But this is one of the question was asked in interview and I am confused. Can anyone guide me on this behavior?

As per my knowledge, in code snippet 1, "String.replace('t','T') is returning object, so object comparisons returns in false. Am I right?

  • 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-17T01:17:42+00:00Added an answer on June 17, 2026 at 1:17 am

    “String.replace(‘t’,’T’) is returning object, so object comparisons
    returns in false. Am I right?

    Yes, as for this case, you are right. String#replace(or any method of String class for that matter), will return a new String object (You can guess why? Immutability). And thus you would have to do the comparison using equals method, to compare their contents.

    Now, in the second case: –

    "STring" == "STring"
    

    You are comparing two string literals. Now, since String literals are interned in Java, so both the literals are same (in the sense, they point to the same memory location), and hence == comparison gives you true.

    The difference in comparison using == and equals is that, == compares the reference value – i.e value of memory location of objects, which will be different for two different string objects, as you are having in first case. Whereas, equals compares the actual content in those objects.

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

Sidebar

Related Questions

Possible Duplicate: Java string comparison? I have encounter the following problem, I have an
Possible Duplicate: Difference Between Equals and == String comparison and String interning in Java
Possible Duplicate: How do I compare strings in Java? String Comparison in Java…? I'm
Possible Duplicate: How do I compare strings in Java? Demonstrating string comparison with Java
Possible Duplicates: String comparison and String interning in Java What is the difference between
Possible Duplicate: Implied string comparison, 0='', but 1='1' Executing the following case in javascript,
Possible Duplicate: Case insensitive string comparison in C++ I've written some code for a
Possible Duplicate: String comparison in Java heres my script: public class euler4 { /**
Possible Duplicate: Java string comparison? I was trying to do this: boolean exit =
Possible Duplicate: Convert String to code in Java Dynamic code execution on Java I

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.