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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:40:52+00:00 2026-05-23T10:40:52+00:00

A simple expression: Object val = true ? 1l : 0.5; What type is

  • 0

A simple expression:

Object val = true ? 1l : 0.5;

What type is val? Well, logically, val should be a Long object with value 1. But Java thinks that val is a Double with value 1.0.

It doesn’t have to to anything with autoboxing as

Object val = true ? new Long(1) : new Double(0.5);

results with same behavior.

Just to clarify:

Object val = true ? "1" : 0.5;

results in the correct String.

Can anybody explain me why they have defined this like that? For me it seems to be rather really bad designed or actually a bug.

  • 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-23T10:40:52+00:00Added an answer on May 23, 2026 at 10:40 am

    This is described in the Java Language Specification Section 15.25 (relevant parts in bold face):

    The type of a conditional expression is determined as follows:

    1. If the second and third operands have the same type […]

    2. If one of the second and third operands is of type boolean […]

    3. If one of the second and third operands is of the null type […]

    4. Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:

      1. If one of the operands is of type byte […]

      2. If one of the operands is of type T where T is byte, short, or char, […]

      3. If one of the operands is of type Byte […]

      4. If one of the operands is of type Short […]

      5. If one of the operands is of type; Character […]

      6. Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands. Note that binary numeric promotion performs unboxing conversion (§5.1.8) and value set conversion (§5.1.13).

    5. Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7).

    The “lub” mentioned in the last paragraph stands for least upper bound and refers to the most specific super type common for T1 and T2.


    As for the case with Object val = true ? 1l : 0.5; I agree that it would be more precise if it applied rule 5 (on the boxed values). I guess the rules would become ambiguous (or even more complicated) when taking autoboxing into account. What type would for instance the expression b ? new Byte(0) : 0.5 have?

    You can however force it to use rule 5 by doing

    Object val = false ? (Number) 1L : .5;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following very simple Javascript-compatible regular expression: <script type=text/javascript id=(.+) src=([^]+)> I
I'm trying to write a simple script expression that allows me to identify the
I'm trying to write a simple generator that uses an expression tree to dynamically
I've been playing around with Expression Trees. I have the following simple method that
The question is simple: what is lifetime of that functor object that is automatically
A simple expression like (x) - y is interpreted differently depending on whether x
The following simple calculator expression grammar (BNF) can be easily parsed with the a
i have simple regular expression: ^123$ Matches are for example 123 1234 etc. How
I'm using this simple regular expression to validate a hex string: ^[A-Fa-f0-9]{16}$ As you
I'm looking for a simple regular expression to match the same character being repeated

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.