Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java initialize variable with null or not?

I was wondering whether I should initialize class members in java with an initial value and then change that value to some other given value in the constructor, or should I avoid doing such a thing?

code example

public class Test {
    private int value = 5;

    public Test(int value) {
        this.value = value;
 }
}
like image 332
G. Sz. Avatar asked Aug 21 '14 19:08

G. Sz.


People also ask

Can we initialize int variable with null in Java?

An int can't be null .

Should I initialize to null or undefined?

What reason is there to use null instead of undefined in JavaScript? Javascript for Web Developers states "When defining a variable that is meant to later hold an object, it is advisable to initialize the variable to null as opposed to anything else.

Is it necessary to initialize a variable with zero everytime?

Considering we say about C++ (now your question placed in the C++ section) compiler does not need variables be initialized to 0. But it do that automaticaly for arithmetic types defined in a global scope f.e. That also does not mean that someone need it.

Can You initialize an int value with null in Java?

First, you cannot initialize an int value with null. The default value of an int is zero, but initializing it in both the field declaration and the constructor is pointless. Assuming the compiler does not omit the field initializer completely, it effectively compiles to this:

What happens if a field is not initialized in Java?

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. Can variables be used in Java without initialization?

Why uninitialized variables are initialized to zero in Java?

Answer Wiki. Yeah in Java , uninitialized variables automatically initialized to zero but if the variable is an instance variable and a class variable because if we don’t initialize the instance variable the system by default initialize them to their respective default values. Following are the instance variable with their default values.

What is Default initialization in Java with example?

1. Default Initialization of Instance Variables in Java When you declare a variable without assigning it an explicit value, the Java compiler will assign a default value. Let’s consider the following example: NOTE: This default initialization applies for instance variables, not for method variables.


3 Answers

If not specified,:

primitive bytes, shorts, ints, longs, floats and doubles are initialized to 0

booleans are initialized to false

Objects are initialized to null

like image 95
dkatzel Avatar answered Oct 23 '22 21:10

dkatzel


If we are talking about class fields than all unset variables of

  • primitive types are set to
    • 0 (numeric ones like int, long, double...)
    • \u0000 (char)
    • false (boolean).
  • object types like String, Integer, or AnyOtherClass are set to null

so actually it doesn't matter if you set it explicitly, because

private int x;    
private Integer y;

is equivalent of

private int x = 0;    
private Integer y = null;
like image 35
Pshemo Avatar answered Oct 23 '22 21:10

Pshemo


Java give value class variables, I mean they are initialized by JVM and you can use them. But you must to search their default values to use them correctly.

On the other hand, JVM does not initialize the local variables which is created in methods. So if you create any variable on methods you have to assign them to a value before use them.

like image 1
PeerNet Avatar answered Oct 23 '22 21:10

PeerNet