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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:11:44+00:00 2026-06-06T02:11:44+00:00

I wrote a same(..similar?) piece of code for Java 7 and C#(.net 3.5) and

  • 0

I wrote a same(..similar?) piece of code for Java 7 and C#(.net 3.5) and got a confusing output. Please help me in understanding this behavior:

Java:

public class strTest {
    public static void main(String [] s) {      
        String s1 = "abc";
        String s2 = new String(new char[] {'a', 'b', 'c'});
        System.out.println(s1 == s2);                      // false
        System.out.println(((Object)s1) == ((Object)s2));  // false
        System.out.println(s1.equals(s2));                 // true
    }
}

Output : false
false
true

C#:

namespace ConsoleApplication1
{
    class Program2
    {
        static void Main(string[] args)
        {
            String s1 = "abc";
            String s2 = new String(new Char[] {'a', 'b', 'c'});
            Console.WriteLine(s1 == s2);                     // true
            Console.WriteLine(((Object)s1) == ((Object)s2)); // false
            Console.WriteLine(s1.Equals(s2));                // true
        }
    }
}

Output : True
False
True

I am aware of concepts like String immutability and String pool but I guess I am missing the finer print.

In java: Why did it create a new String object for s2 when it could have used the one which is referenced by s1? Isn’t that what String pool is supposed to achieve?

  • 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-06T02:11:47+00:00Added an answer on June 6, 2026 at 2:11 am

    There are two aspects to your question: when objects are created, and how == comparisons work:

    Object creation

    In terms of interning (“pooling”) – only constants are interned automatically. You can intern other strings using Intern/intern, but when you just call the string constructor that will always create a new string. (Well, except for one bizarre corner case in .NET, but we can ignore that for now.)

    So in your code, s1 and s2 always refer to different objects.

    Comparison

    The cause of the difference is your use of ==. C# supports user-defined operator overloading, and String overloads it like this:

    public static bool operator ==(string x, string y)
    

    which compares the contents of the string rather than the references. So when you have two operands of type string, x == y ends up doing an equality comparison rather than an identity comparison. That’s why the first line of the C# output is True.

    Java doesn’t have this behaviour, which is why is prints false.

    When the operand types are Object, both C# and Java will use reference equality, which is why the second line is false in both cases.

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

Sidebar

Related Questions

I wrote this code because I'm having a similar problem in a larger program
I wrote the following loop since I have to repeat the same/similar code 25
I have two very similar pieces of ASP.NET code that send a file in
I wrote a function very similar to this: def writeMyEl (x: TypeA, y: TypeB,
Can i write oblects in java into file and retrieve the same?? In c++
I'm trying to write a java program to connect to the same MySQL database
I wrote a bunch of regular expressions for a C# (3.5) ASP.NET web application.
I have to write a simple piece of code that acts on a file;
This seems to be a stupid question since Java does short circuit, but I
I have four text boxes all with similar code. Each one on hover fades

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.