Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are default field values assigned by the compiler or the JVM?

Tags:

java

I have one question: in java we declare int,long,double etc.,(primitive data) or non primitive (object data), not initialized with default values, but at run time it will take default values. Now my question is which one assigns default values: java compiler or Java Virtual Machine (JVM)?

For Example:

int x;
System.out.println(x) //Result is 0;
like image 908
user1990992 Avatar asked Mar 13 '14 02:03

user1990992


People also ask

What are the default values assigned to variables in Java?

Default values are same as instance variables. For numbers, the default value is 0; for Booleans, it is false; and for object references, it is null. Values can be assigned during the declaration or within the constructor. Additionally, values can be assigned in special static initializer blocks.

Do data fields have default values in Java?

- Data fields have default values. - Local variables do not have default values. - A variable of a primitive type holds a value of the primitive type.

Are fields public by default in Java?

By default is not private, is "package only" (no key word for that). All classes in same package see the field. Save this answer.


2 Answers

There are three different types of declared variables in Java. They are instance, class and local variables.

Instance Variables

Instance variables are the non-static fields of your class, often referred to simply as fields.

  • Primitive numeric fields initialize to 0. This includes byte, short, int, long, float and double.

  • booleans initialize to false .

  • chars initialize to the null character \u0000.

  • Reference types initialize to null.

Class Variables

A class variable is a field within a class declared as static, often referred to as a static variable or static field. It is also same initialize as instance variable.

Local Variables

A local variable is a variable defined within a method, which includes any method parameters. Local variables must be initialized before use. They do not have a default value.

Initialization process is done by JVM when method is create.

like image 145
T8Z Avatar answered Sep 28 '22 19:09

T8Z


The default values for fields are assigned by the JVM at runtime. From JLS 15.9.4 (emphasis mine):

The new object contains new instances of all the fields declared in the specified class type and all its superclasses. As each new field instance is created, it is initialized to its default value.

Of course, given that this behavior is standardized in the JLS, a compiler could conceivably take advantage of that to perform certain optimizations based on the assumption that uninitialized fields start with their default value.

Fields are initialized to the equivalent of 0 in whatever type they are (null for reference types). This article gives a nice list:

Data Type:              Default Value:
boolean                 false 
char                    \u0000 
int,short,byte / long   0 / 0L 
float / double          0.0f / 0.0d 
any reference type      null

Local variables are not given an initial value, and it is a compiler error to use them if they are not assigned a value through all possible code paths prior to use.

Note that array elements are automatically initialized to default values as well when a new array is created (e.g. each element of new int[100] will be initialized to 0). This applies to both field and local array variables.

like image 39
Jason C Avatar answered Sep 28 '22 18:09

Jason C