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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:48:45+00:00 2026-05-27T06:48:45+00:00

I am led to believe that the java compiler does all the work of

  • 0

I am led to believe that the java compiler does all the work of method selection at compile time (or am I wrong?). That is, it will decide exactly which method to use in which class at compile time by examining the class hierarchy and method signatures. All that is then required at run time is to choose the object whose method is to be called and this can only work up the inheritance chain.

If that is the case, how does this work?

int action = getAction ();
StringBuilder s = new StringBuilder()
    .append("Hello ") // Fine
    .append(10) // Fine too
    .append(action == 0 ? "" : action); // How does it do this?

Here, the type of the parameter can be either String or int. How can it decide at compile time which method of StringBuilder should be invoked?

  • 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-27T06:48:46+00:00Added an answer on May 27, 2026 at 6:48 am

    An expression such as

    action == 0 ? "" : action
    

    can have a single return type. The compiler understands this as an expression returning an Object instance. This, at runtime, can be either String or Integer.

    In your case, append(Object) will be called. The StringBuilder implementation will then call toString() on the parameter, which will give you the expected result (either "" or an integer value converted to String).

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

Sidebar

Related Questions

I came across a compile.. oddity? recently that led me to believe that a
I've been led to believe that for single variable assignment in T-SQL, set is
Some googling has led me to believe that C++ is the best language for
I have been led to believe that the best point to bind your controls
I've been led to believe that casting can, in certain circumstances, become a measurable
JBoss 6. I'm led to believe that I can mark a class as @Stateless
The Twisted documentation led me to believe that it was OK to combine techniques
A bit of Googling led me to believe that this error happens in Rails
I have a scrolling LED sign that takes messages in either ASCII or (using
First the practical application that led me to the problem: Given a set of

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.