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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:25:31+00:00 2026-05-16T03:25:31+00:00

It was very confusing to me to observe this situation: Integer i = null;

  • 0

It was very confusing to me to observe this situation:

Integer i = null;
String str = null;

if (i == null) {   //Nothing happens
   ...                  
}
if (str == null) { //Nothing happens

}

if (i == 0) {  //NullPointerException
   ...
}
if (str == "0") { //Nothing happens
   ...
}

So, as I think boxing operation is executed first (i.e. java tries to extract int value from null) and comparison operation has lower priority that’s why the exception is thrown.

The question is: why is it implemented in this way in Java? Why boxing has higher priority then comparing references? Or why didn’t they implemented verification against null before boxing?

At the moment it looks inconsistent when NullPointerException is thrown with wrapped primitives and is not thrown with true object types.

  • 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-16T03:25:32+00:00Added an answer on May 16, 2026 at 3:25 am

    The Short Answer

    The key point is this:

    • == between two reference types is always reference comparison
      • More often than not, e.g. with Integer and String, you’d want to use equals instead
    • == between a reference type and a numeric primitive type is always numeric comparison
      • The reference type will be subjected to unboxing conversion
      • Unboxing null always throws NullPointerException
    • While Java has many special treatments for String, it is in fact NOT a primitive type

    The above statements hold for any given valid Java code. With this understanding, there is no inconsistency whatsoever in the snippet you presented.


    The Long Answer

    Here are the relevant JLS sections:

    JLS 15.21.3 Reference Equality Operators == and !=

    If the operands of an equality operator are both of either reference type or the null type, then the operation is object equality.

    This explains the following:

    Integer i = null;
    String str = null;
    
    if (i == null) {   // Nothing happens
    }
    if (str == null) { // Nothing happens
    }
    if (str == "0") {  // Nothing happens
    }
    

    Both operands are reference types, and that’s why the == is reference equality comparison.

    This also explains the following:

    System.out.println(new Integer(0) == new Integer(0)); // "false"
    System.out.println("X" == "x".toUpperCase()); // "false"
    

    For == to be numerical equality, at least one of the operand must be a numeric type:

    JLS 15.21.1 Numerical Equality Operators == and !=

    If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible to numeric type, binary numeric promotion is performed on the operands. If the promoted type of the operands is int or long, then an integer equality test is performed; if the promoted type is float or double`, then a floating-point equality test is performed.

    Note that binary numeric promotion performs value set conversion and unboxing conversion.

    This explains:

    Integer i = null;
    
    if (i == 0) {  //NullPointerException
    }
    

    Here’s an excerpt from Effective Java 2nd Edition, Item 49: Prefer primitives to boxed primitives:

    In summary, use primitives in preference to boxed primitive whenever you have the choice. Primitive types are simpler and faster. If you must use boxed primitives, be careful! Autoboxing reduces the verbosity, but not the danger, of using boxed primitives. When your program compares two boxed primitives with the == operator, it does an identity comparison, which is almost certainly not what you want. When your program does mixed-type computations involving boxed and unboxed primitives, it does unboxing, and when your program does unboxing, it can throw NullPointerException. Finally, when your program boxes primitive values, it can result in costly and unnecessary object creations.

    There are places where you have no choice but to use boxed primitives, e.g. generics, but otherwise you should seriously consider if a decision to use boxed primitives is justified.

    References

    • JLS 4.2. Primitive Types and Values
      • "The numeric types are the integral types and the floating-point types."
    • JLS 5.1.8 Unboxing Conversion
      • "A type is said to be convertible to a numeric type if it is a numeric type, or it is a reference type that may be converted to a numeric type by unboxing conversion."
      • "Unboxing conversion converts […] from type Integer to type int"
      • "If r is null, unboxing conversion throws a NullPointerException"
    • Java Language Guide/Autoboxing
    • JLS 15.21.1 Numerical Equality Operators == and !=
    • JLS 15.21.3 Reference Equality Operators == and !=
    • JLS 5.6.2 Binary Numeric Promotion

    Related questions

    • When comparing two Integers in Java does auto-unboxing occur?
    • Why are these == but not equals()?
    • Java: What’s the difference between autoboxing and casting?

    Related questions

    • What is the difference between an int and an Integer in Java/C#?
    • Is it guaranteed that new Integer(i) == i in Java? (YES!!! The box is unboxed, not other way around!)
    • Why does int num = Integer.getInteger("123") throw NullPointerException? (!!!)
    • Java noob: generics over objects only? (yes, unfortunately)
    • Java String.equals versus ==
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is very confusing, I use AsDataView to bind query result to a dgv
This is probably very simple but it's really confusing me. When I implement the
This is thoroughly confusing me. I am trying to gather very basic statistics on
hey guys, this is very confusing... i am trying to find the minimum of
I have run into a very confusing situation with maps on the iPhone. Everything
My question is one line yet this is very confusing me. Why i can
I need help in this and am very confusing thinking how to do this.
Why is this allowed? It's very confusing when you inherit code that uses this
It is my first day to perl, and I find this warning very confusing.
As a Flash newbie I found this very confusing and it cost me a

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.