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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:24:06+00:00 2026-05-20T06:24:06+00:00

When the Java developers make design decisions, they usually follow ‘best practices’ about code

  • 0

When the Java developers make design decisions, they usually follow ‘best practices’ about code maintainability and what not. So I was surprised to find the following situation in the String class source code (The comments are mine, not theirs obviously).

/* Makes sense, use the appropriate class for this */
public static String valueOf(int i) {
    return Integer.toString(i, 10);
}

/* Makes sense, use the appropriate class for this too */
public static String valueOf(float f) {
return Float.toString(f);
}

/* Magic values "true" and "false"? Seriously? */
public static String valueOf(boolean b) {
return b ? "true" : "false";
}

This made me think that for some reason it perhaps might not be defined in Boolean class, or that Boolean references String. Again, I am surprised to find this method is duplicated.

/* Identical code to String */
public static String toString(boolean b) {
    return b ? "true" : "false";
}

Now, I understand that it’s highly unlikely the canonical representation of boolean values is likely to change. But how many times have the people who send us (as developers) send us requirements say “Oh, this won’t change – it’s set in stone!” and yet, after a certain amount of time, they come back and say “We need this change, and we need it now!” and hang up before you can tell them they said it would never change.

Is there any particular reason they would do this? I know that sentence makes it sound like it’s murder or something, but still. Am I the only one surprised by this?

  • 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-20T06:24:06+00:00Added an answer on May 20, 2026 at 6:24 am

    "true" and "false" are mandated by the language itself; thus in this case it’s relatively safe to hardcode the string values.

    JLS 4.2.5 The boolean Type and boolean Values

    … will convert the boolean operand to a String (either "true" or "false") …

    Similarly, the language specifies e.g. null references becomes "null" during string conversion.

    JLS 15.18.1.1 String Conversion

    If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l)

    Generally it’s best not to hardcode constants all over the code base, but when the language guarantees what the constants should be, this becomes less of a problem.


    Here’s a snippet from OpenJDK for AbstractStringBuilder:

    public AbstractStringBuilder append(boolean b) {
        if (b) {
            ensureCapacityInternal(count + 4);
            value[count++] = 't';
            value[count++] = 'r';
            value[count++] = 'u';
            value[count++] = 'e';
        } else {
            ensureCapacityInternal(count + 5);
            value[count++] = 'f';
            value[count++] = 'a';
            value[count++] = 'l';
            value[count++] = 's';
            value[count++] = 'e';
        }
        return this;
    }
    

    Note that StringBuilder extends AbstractStringBuilder.

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

Sidebar

Related Questions

No related questions found

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.