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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:06:33+00:00 2026-06-05T22:06:33+00:00

I have a simple recursive method written in both Java and C# Java public

  • 0

I have a simple recursive method written in both Java and C#

Java

public static String reverse(String letter) {
    if (letter == "")
        return "";
    else
        return letter.substring(letter.length() - 1)
                + reverse(letter.substring(0, letter.length() - 1));
}

C#

public static string reverse(string letter)
{
    if (letter == "")
        return "";
    else
        return letter[letter.Length - 1]
                + reverse(letter.Substring(0, letter.Length - 1));
}

But the Java version fails at letter == "", it returns false even if the letter is empty. Why does the C# version work while the Java version fails?

  • 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-05T22:06:35+00:00Added an answer on June 5, 2026 at 10:06 pm

    In both C# and Java, the == operator checks for reference equality by default.

    However, in .NET, the == operator is overloaded for the string type to check for value equality instead:

    public static bool operator ==(string a, string b)
    {
        return string.Equals(a, b);
    }
    

    Another factor you need to take into account when using both languages is string interning, which can cause even reference equality to succeed for strings that might appear distinct. From MSDN (for C#):

    The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system.

    For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.

    String interning is applied across both Java and C#, and typically gives consistent results. For example, both languages evaluate string concatenation at compile-time, meaning that "a" + "b" is stored as "ab":

    "ab" == "a" + "b"                                      // Java: Gives true
    object.ReferenceEquals("ab", "a" + "b")                // C#:   Gives true
    

    However, when getting a zero-length substring, only C# returns the interned empty string:

    "" == "abc".substring(0, 0)                            // Java: Gives false
    object.ReferenceEquals("", "abc".Substring(0, 0))      // C#:   Gives true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple code: public class testing { private static Object objToSync = new
I've written a pretty simple recursive-descent parser in Java, but having some issues with
I have a simple recursive method, a depth first search. On each call, it
I have a rather annoying issue that I solved using a simple recursive method
I have a simple recursive algorithm, which returns Fibonacci numbers: private static double fib_recursive(int
I have a simple recursive function RCompare() that calls a more complex function Compare()
I have a simple recursive function to write in VBA that does the following
I have a recursive method call. When any exception is thrown, I would like
I have a simple WCF service which has a method that returns a complex
I have simple table in Sybase -- Creating table 'SimpleText' CREATE TABLE [dbo].[SimpleText] (

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.