13

I was going through a test on itester.org and found a question which I don't understand:

public class Runner 
{
    public static Integer i;

    public static void main(String[] args) 
    {
        if (i == 42) {
            System.out.printf("wow");
        }
    }
 }

I read before, that integer variable is assigned by default 0. Why is it assigned null here?

2
  • 3
    Because it's not a primitive, but an object. The default value for an object is null. When you are doing i == 42, it tries to unbox the int value in i but since i is null, you get a NPE. Commented Nov 30, 2014 at 9:13
  • Gotta love Java -- it makes so much sense that int and Integer are different data types. Commented Nov 30, 2014 at 9:19

6 Answers 6

18

Any reference type (i.e. any variable whose type is Object or a sub-class of Object) has a default value of null. This includes Integer.

The primitive int, on the other hand, has a default value of 0.

Sign up to request clarification or add additional context in comments.

Comments

6

Because the JLS 4.12.5. Initial Values of Variables:

For all reference types (§4.3), the default value is null.

And since Integer is a reference type, it gets null:

ReferenceType:
    ClassOrInterfaceType
    TypeVariable
    ArrayType

See the link for other types

Comments

5

The primitive int type is assigned 0 by default, but an Integer reference is assigned null by default. Integer is a wrapper class -- it's an object, not a primitive type.

You may want to read about autoboxing and unboxing in Java, the process by which Java automatically converts between primitive types and wrapper classes.

Comments

2

In Java Integer is a Object Type. In this sample code, you need primitive type which is int. In Java any Object type/Reference type or any Sub type of object type variable which is not initialized automatically initialized by null. Where as primitive type has a default value.

The reason behind of this is, Objects provide facilities for polymorphism, are passed by reference (or more accurately have references passed by value), and are allocated from the heap. Conversely, primitives are immutable types that are passed by value and are often allocated from the stack.

Comments

1

All non-primitive non-local variables are assigned to null, if not assigned explicitly.

Comments

1

Integer is a wrapper class and in this code sample I is reference variable. (In java everything is not object so we use wrapper classes to make object and java perform boxing and unboxing ) All reference variables in java by default null value and all primitive type have its default value (e.g int i -> 0 )

Use:- Private static int; then it have 0 value default .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.