10

How can I check if an int I declared has been initialized with a variable? number == null will not work because ints are primitive types.

This question is not relevant because the user asks how to check if the int is null, to which the answerer responds to use Integer. However, I want to check if the primitive data type int is initialized.

This question is also irrelevant because that is for a reference type whereas I'm asking for a primitive type. Already clarified -.- and still getting marked...

Ishan_Arya
  • 155
  • 1
  • 1
  • 7
  • 1
    Is this `int` a field or local variable? – Logan Aug 03 '18 at 19:36
  • 2
    The same question answers it. If it is not a local variable, check if it is 0. If it is a local variable, it won't compile – Thiyagu Aug 03 '18 at 19:36
  • @user7 why shouldn't compile? – NiVeR Aug 03 '18 at 19:38
  • @NiVeR Java has a notion of definite assignment. A variable has to be definitely assigned before it can be used. – Logan Aug 03 '18 at 19:39
  • Oh ok, if you use it won't compile yeah. I missed this point – NiVeR Aug 03 '18 at 19:41
  • In the case of fields, the Java compiler will initialize primitives if you don't. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html In your case Java will set it to 0. So you could check if your variable is equal to 0. – Joshua Benz Aug 03 '18 at 19:43

2 Answers2

9

Instance fields are initialized to null, 0, etc. Local fields are not initialized. There is no way to test for this, short of attempting to use the field and generating an exception. A good practice is to always initialize local variables when you declare them.

Steve11235
  • 2,849
  • 1
  • 17
  • 18
  • 2
    Good answer. If one cares about detecting initialization, use [Null](https://en.wikipedia.org/wiki/Null_pointer) along with an object wrapper, a class defined for each of the eight primitive types. For example, [`Integer`](https://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html) class for `int` primitive. `Integer i = null ;` … `if( null == i ) { i = 42 ; }`. [*Auto-boxing*](https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html) automatically wraps primitive `int` into an `Integer` object. – Basil Bourque Aug 03 '18 at 19:42
  • While I agree with this point, the guys who designed Spring decided to initialize bean properties another way and make default assignments redundant (not to mention misleading). Don't shoot the messenger, I think Spring is obscene. – Sridhar Sarnobat Sep 16 '19 at 22:08
3

For a field variable you can check by comparing the int to 0 that is the default value for an int field :

private int x: // default 0 for an int
... 
public void foo(){
  if (x == 0){  // valid check
    // ...
  }
}   

Built-in types have a default "reasonable" value :

Default Values

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.

The following chart summarizes the default values for the above data types.

Data Type   Default Value (for fields)
byte    0
short   0
int     0
long    0L
float   0.0f
double  0.0d
char    '\u0000'
String (or any object)      null
boolean false

For a local variable no check is required and is even possible because the compiler does the check for you.
The compilation will indeed not pass if any statement refers a not initialized variable.
So the code that checks the int local variable will not compile itself for this reason :

public void foo(){
    int a;      
    if (a==0) { // doesn't compile : The local variable a may not have been initialized     
      ...   
    }
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215