Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
int phoneNumber = m.get(s);
            if(phoneNumber!=null)
                {
            System.out.println(s + "=" + phoneNumber);
            }


This code returns the following error statement (obviously)

error: incomparable types: int and <null>

             if(phoneNumber!=null)
                             ^


But this code works fine if I use a wrapper class.

Java
Integer phoneNumber = m.get(s);
                if(phoneNumber!=null)
                    {
                System.out.println(s + "=" + phoneNumber);     
                }


What I have tried:

Why the compiler is not throwing the incomparable types error here ?
Posted
Comments
Peter_in_2780 4-Oct-16 7:00am    
Short answer: Integer is an object, and objects can be compared with null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900