是编译器还是JVM分配的默认字段值? [英] Are default field values assigned by the compiler or the JVM?
问题描述
我有一个问题:在java中我们声明int,long,double等,(原始数据)
或非原始(对象数据),未使用默认值初始化,但是在运行
时它将采用默认值。现在我的问题是哪一个分配
默认值:java编译器或Java虚拟机(JVM)?
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)?
例如:
int x;
System.out.println(x) //Result is 0;
推荐答案
Java中有三种不同类型的声明变量。它们是实例,类和局部变量。
There are three different types of declared variables in Java. They are instance, class and local variables.
实例变量
实例变量是类的非静态字段,通常简称为字段。
Instance variables are the non-static fields of your class, often referred to simply as fields.
-
原始数字字段初始化为0。包括
字节
,短
,int
,long
,float
和double
。
boolean
s初始化为 false
。
char
s初始化为空字符 \ u0000
。
char
s initialize to the null character \u0000
.
引用类型初始化为 null
。
类变量
类变量是声明为static的类中的一个字段,通常被称为作为静态变量或静态字段。它也与实例变量初始化相同。
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.
本地变量
本地变量是在方法中定义的变量,它包括任何方法
参数。
必须在使用前初始化局部变量。它们没有默认值。
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.
创建方法时,初始化过程由 JVM 完成。
Initialization process is done by JVM when method is create.
这篇关于是编译器还是JVM分配的默认字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!