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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:27:31+00:00 2026-06-01T09:27:31+00:00

In the following inline conditionals, one might expect an integer and a double to

  • 0

In the following inline conditionals, one might expect an integer and a double to be printed, respectively:

System.out.println(true ? 0 : 0.0);
System.out.println(false ? 0 : 0.0);
System.out.println(true ? new Integer(0) : new Double(0.0));
System.out.println(true ? 0 : "");

Instead they are both printed as doubles when occurring together:

 0.0
 0.0
 0.0
 0

Why are numbers auto-cast when occurring with other numbers in inline conditionals?

Edit: If this is occurring because System.out.println is overloaded what is the case for:

list.add(true ? 0 : 0.0);
list.add(false ? 0 : 0.0);
list.add(true ? new Integer(0) : new Double(0.0));
list.add(true ? 0 : "");
System.out.println(list);

outputs:

[0.0, 0.0, 0.0, 0]
  • 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-01T09:27:32+00:00Added an answer on June 1, 2026 at 9:27 am

    Why are numbers auto-cast when occurring with other numbers in inline conditionals?

    The conditional expression has to have a single result type, and that type is used to determine which overload of System.out.println to use. Overloads are always determined at compile-time, and it would be really awkward for the compiler to take two completely separate paths for an expression depending on which condition was picked.

    If you want to do two different things based on a condition, use an if. If you want to pick between two values, with one result type, based on a condition, then the conditional operator is perfect.

    EDIT: The interesting case here, IMO, is the third one. The compiler could have chosen to not perform any conversions, and just call println(Object). To show that it’s not doing that, here’s a separate test:

    Object x = true ? new Integer(0) : new Double(0.0);
    System.out.println(x.getClass());
    

    This prints out class java.lang.Double – and if you look at the bytecode, you’ll see it’s unboxing the int then reboxing it as a Double. For the gory details of how it’s all determined, see section 15.25 of the JLS.

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

Sidebar

Related Questions

The following GCC inline asm is taken from LuaJit's coco library. Can someone provide
I have the following function: inline auto iterateSomething(obj & o) { auto iterators =
We can write CSS as the following types: Inline CSS Embedded CSS External CSS
Given the following code (copied from the attoparsec library) what does the inline pragma
I am trying to assign a form-inline class to the following form in rails
I have the following code <a href=#inline class=fancy id=item1>hello item1</a> <a href=#inline class=fancy id=item2>hello
Suppose I have the following HTML: <div id=test> <span style=display:inline;>foo</span> <span style=display:none;>bar</span> <span style=display:inline;>baz</span>
When i use use following inline jQuery Based validation in asp.net c# web form
What is the basic difference between the following CSS: display:inline and this: display:block Using
I have the following that I execute inline on a number of screens: tinyMCE.init({

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.