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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:18:22+00:00 2026-05-17T20:18:22+00:00

Possible Duplicates: Booleans, conditional operators and autoboxing Java, Google Collections Library; problem with AbstractIterator?

  • 0

Possible Duplicates:
Booleans, conditional operators and autoboxing
Java, Google Collections Library; problem with AbstractIterator?

The code below produces a NPE:

Integer test = null;
Integer test2 = true ? test : 0;
System.out.println(test2);

To correctly print out “null” without an exception requires this code:

Integer test = null;
Integer test2 = true ? test : (Integer)0;
System.out.println(test2);

It’s obvious in the first example that “test” is being unboxed (converted to native int), but why? And why does changing the other expression in the ternary operator (as in the 2nd example) fix it? Can anyone provide some kind of narrative of exactly when, what, and why stuff in both of the examples gets boxed and unboxed?

  • 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-17T20:18:22+00:00Added an answer on May 17, 2026 at 8:18 pm

    From section 15.25 of the Java Language Specification:

    The type of a conditional expression is determined as follows:

    • If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression.
      • If one of the second and third operands is of type boolean and the type of the other is of type Boolean, then the type of the conditional expression is boolean.
      • If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.
      • Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:
        • If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.
        • If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T.
        • If one of the operands is of type Byte and the other operand is a constant expression of type int whose value is representable in type byte, then the type of the conditional expression is byte.
        • If one of the operands is of type Short and the other operand is a constant expression of type int whose value is representable in type short, then the type of the conditional expression is short.
        • If one of the operands is of type; Character and the other operand is a constant expression of type int whose value is representable in type char, then the type of the conditional expression is char.
        • 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).

    So it’s following the final bullet, performing binary numeric promotion, which performs an unboxing conversion. So the type of the conditional operator expression is int, even though you’re assigning it to an Integer. It’s trying to perform the unboxing conversion on null, hence the exception.

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

Sidebar

Related Questions

Possible Duplicates: What is the best approach for a Java developer to learn C++
Possible Duplicate: Shortcut “or-assignment” (|=) operator in Java I found the following example code
Possible Duplicates: Why is lock(this) {...} bad? In C# it is common to use
Possible Duplicates: Formatting of if Statements Is there a best coding style for identations
Possible Duplicates: Is String.Format as efficient as StringBuilder C# String output: format or concat?
Possible Duplicates: Valid use of goto for error management in C? Examples of good
Possible Duplicates: Do-While and if-else statements in C/C++ macros do { … } while
Possible Duplicate: Breaking out of a nested loop I have this code foreach (___)
Possible Duplicate: Python Ternary Operator In some languages including Java, C/C++, C#, etc. you
Possible Duplicate: Does java evaluate remaining conditions after boolean result is known Why do

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.