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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:10:50+00:00 2026-05-14T05:10:50+00:00

I’m a bit puzzled about the conditional operator. Consider the following two lines: Float

  • 0

I’m a bit puzzled about the conditional operator. Consider the following two lines:

Float f1 = false? 1.0f: null;
Float f2 = false? 1.0f: false? 1.0f: null;

Why does f1 become null and the second statement throws a NullPointerException?

Langspec-3.0 para 15.25 sais:

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).

So for false?1.0f:null T1 is Float and T2 is the null type. But what is the result of lub(T1,T2)? This para 15.12.2.7 is just a bit too much …

BTW, I’m using 1.6.0_18 on Windows.

PS: I know that Float f2 = false? (Float) 1.0f: false? (Float) 1.0f: null; doesn’t throw NPE.

  • 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-14T05:10:51+00:00Added an answer on May 14, 2026 at 5:10 am

    The difference is static typing of the expressions at compile time:

    Summary

    E1: `(false ? 1.0f : null)`
        - arg 2 '1.0f'           : type float,
        - arg 3 'null'           : type null 
        - therefore operator ?:  : type Float (see explanation below)
        - therefore autobox arg2
        - therefore autobox arg3
    
    E2: `(false ? 1.0f : (false ? 1.0f : null))`
        - arg 2 '1.0f'                    : type float
        - arg 3 '(false ? 1.0f : null)'   : type Float (this expr is same as E1)
        - therefore, outer operator ?:    : type float (see explanation below)
        - therefore un-autobox arg3
    

    Detailed Explanation:

    Here’s my understand from reading through the spec and working backwards from the result you got. It comes down to the type of the third operand of the f2 inner conditional is null type while the type of the third operand of the f2 outer conditional is deemed to be Float.

    Note: Its important to remember that the determination of type and the insertion of boxing/unboxing code is done at compile-time. Actual execution of boxing/unboxing code is done at run-time.

    Float f1 = (false ? 1.0f : null);
    Float f2 = (false ? 1.0f : (false ? 1.0f : null));
    

    The f1 conditional and the f2 inner conditional: (false ? 1.0f : null)

    The f1 conditional and the f2 inner conditional are identical: (false ? 1.0f : null). The operand types in the f1 conditional and the f2 inner conditional are:

    type of second operand = float
    type of third operand = null type (§4.1)
    

    Most of the rules in §15.25 are passed up and this final evaluation is indeed applied:

    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).

    S1 = float
    S2 = null type
    T1 = Float
    T2 = null type
    type of the f1 and f2 inner conditional expressions = Float
    

    Since for f1, the assignment is to a Float reference variable, the result of the expression (null) is successfully assigned.

    For f2 outer conditional: (false ? 1.0f : [f2 inner conditional])

    For the f2 outer conditional, the types are:

    type of second operand = float
    type of third operand = Float
    

    Note the difference in operand types compared to the f1/f2 inner conditionals that reference the null literal directly (§4.1). Because of this difference of having 2 numeric-convertible types, this rule from §15.12.2.7 applies:

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

      • 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).

    Because of the unboxing conversion performed on the result of the f2 inner conditional (null), a NullPointerException is raised.

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

Sidebar

Ask A Question

Stats

  • Questions 430k
  • Answers 430k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm pretty sure that that UITabBarController did indeed push your… May 15, 2026 at 1:48 pm
  • Editorial Team
    Editorial Team added an answer Python 3: The official csv documentation recommends opening the file… May 15, 2026 at 1:48 pm
  • Editorial Team
    Editorial Team added an answer WordPress supports Gravatar, which is essentially a globally recognised avatar.… May 15, 2026 at 1:48 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.