Kyle T
Kyle T

Reputation: 198

Explicit initialization of primitives

I understand that Java primitives have default initialization values; for example 0 for int.

From my research it seems that I should not rely on these values. Should I always provide an explicit initialization and if so, where?

Here's part of my program:

public class Calculator {

// Initialize value where it's declared?
private int value;

public Calculator() {
    // Initialize value in constructor?
}

public void add(int other) {
    value += other;
}
}

Upvotes: 9

Views: 4579

Answers (7)

ZhongYu
ZhongYu

Reputation: 19682

The default values are dependable.


Actually they are "more" dependable than explicit zeroing, because the default assignments happens-before all other actions.

Upvotes: 0

Guillaume Darmont
Guillaume Darmont

Reputation: 5018

You can clearly trust JVM primitive initialization with default values.


BTW, explicit initialization will be translated into bytecode which is useless in this case.

Say you have this class :

public class PrimitiveInitialization {
    int i = 0;
    int j;
}

Corresponding bytecode will be

class PrimitiveInitialization {
  I i
  I j

  public <init>()V
   L0
    LINENUMBER 8 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V
   L1
    LINENUMBER 10 L1
    ALOAD 0
    ICONST_0
    PUTFIELD PrimitiveInitialization.i : I
    RETURN
   L2
    LOCALVARIABLE this LPrimitiveInitialization; L0 L2 0
    MAXSTACK = 2
    MAXLOCALS = 1
}

We can see that explicit initialization adds few bytecode operations for variable i.

That said, you must always prefer code readability, as JIT is probably able to optimize this kind of bytecode at runtime.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

Using default values produced by Java is absolutely OK, you do not need to explicitly initialize instance or static fields of your class. Java Language Specification requires this, so any implementation of the language must comply. In other words, your code would remain 100% portable if you use default values of member variables. This applies to both primitive and reference types (the later are initialized with null).

See Java Language Specification, page 81, for more details.

4.12.5 Initial Values of Variables

Every variable in a program must have a value before its value is used: - Each class variable, instance variable, or array component is initialized with a default value when it is created

The only case where you must provide explicit initialization is when you declare local variables:

A local variable must be explicitly given a value before it is used, by either initialization or assignment, in a way that can be verified using the rules for definite assignment.

However, since failure to initialize a local is a compile-time error, the compiler will pinpoint every missing initialization before you can run your program.

Upvotes: 4

wobblycogs
wobblycogs

Reputation: 4093

As a general rule the code you write should always do the least surprising thing possible. If you feel that a default of zero for a variable that isn't explicitly initialized is surprising then perhaps you should make sure that you always assign an initial value even if it's zero.

My personal preference in the code you've shown would be to initialize the variable to 0 when declared. This is simply because the first operation on the variable is += and to me it feels wrong to rely on the default value for numerical operations. There's no good reason for this as it will work fine as it is it just feels wrong.

Upvotes: 0

stinepike
stinepike

Reputation: 54672

The member variables will be initialized with default values. So you need not worry about that. But still if you want to initialize with default values or other values you can set them in constructor

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213223

From my research it seems that I should not rely on these values.

You can rely on them. There will be no harm, as it is done by default as you already know.

Should I always provide an explicit initialization and if so, where?

For primitives, no it's not necessary to provide the explicit initialization, and I prefer not to. I rarely do and see people to do an initialization like - int i = 0; for instance variables. But, if you are talking about local variables, then you need to give them explicit values. The defaults are not implicit there.

But when the case comes for non-primitives, then you should do an explicit initialization, either at the point of declaration, or inside the constructor, as the values are by default null, which might result in NPE, if not taken care of. The benefit of initializing at the point of declaration is that, you won't have to put the initialization in each of your constructors.

Upvotes: 1

William Morrison
William Morrison

Reputation: 11006

Initialize variables in the constructor if you're really worried about this. However, I've never seen a numeric primitive initialized to anything other than zero.

I'd think you'd only need to worry about this if you were working with some custom (and poor) JVM.

Upvotes: 0

Related Questions