-1

So I have this code:

public static void main( String[] args )
{
    int i = 3;
    int j = new Integer(5);

    JOptionPane.showMessageDialog(null, Boolean.toString(j.equals(5)));
}

It should open a message dialog with the text "true". The problem is it gives a compiler error saying that I can't invoke a method on the primitive type int. Why is this happening?

Casting ((Integer) j).equals(5) works as expected. I just don't understand why the object j is turning into a primitive type.

1 Answers1

3

I just don't understand why the object j is turning into a primitive type.

Because j is not an object in the first place. It is a primitive int. Your new Integer(5) is automatically unboxed on assignment.

lexicore
  • 42,748
  • 17
  • 132
  • 221