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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:38:08+00:00 2026-06-13T23:38:08+00:00

I always thought that primitive types in Java cannot be null , as it

  • 0

I always thought that primitive types in Java cannot be null, as it is a compile time error if i attempt to do something like this:

int test = null;

However in a ternary operation, it seems to be allowed:

int test = something != 0 ? 5 : null;

Isn’t a ternary operation just short for (in this case):

int test;
if (something != 0){
    test = 5;
} else {
    test = null
}

which of course should not be allowed. if that condition fails, It will automaticly throw a NullPointerException due to autoboxing. So why the java-compiler doesn’t fetch nonsense like this?

  • 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-13T23:38:09+00:00Added an answer on June 13, 2026 at 11:38 pm

    What happens is that the Java compiler first tries to make the types of the expressions on either side of the : equal. In this case, it autoboxes the 5 to an Integer; note that null is a valid value for Integer. The result of the whole ternary expression is Integer. You assign that to an int, so the Integer is then autounboxed.

    Essentially the compiler applies autoboxing and -unboxing so that the line is going to look like this:

    int test = (something != 0 ? Integer.valueOf(5) : null).intValue();
    

    Indeed, autounboxing null leads to a NullPointerException.

    So why the java-compiler doesn’t fetch nonsense like this?

    Because the designers of the Java language defined the language in such a way that it works like this and didn’t decide that this has to be treated as an error…

    Section 15.25 of the Java Language Specification explains how the type of the whole expression is determined.

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

Sidebar

Related Questions

I've always thought that it's impossible for this to be null inside instance method
I always thought base.Something was equivalent to ((Parent)this).Something , but apparently that's not the
This is a question that's been nagging me for some time. I always thought
I always thought that you could use OR in a LIKE statment to query
I always thought that *&p = p = &*p in C. I tried this
I have always been taught that non-primitive types should be passed by const reference
I always thought that varibales are mapped to stack locations once your Java source
In C#, I have always thought that non-primitive variables were passed by reference and
In a jQuery.each() loop, I always thought that this was equivalent to valueOfElement .
I always thought that the nullable types of the .NET framework where nothing but

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.