Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
134 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...