java什么是初始化对象_什么是初始化的java对象字段?

来自太阳队

java tutorial

It’s not always necessary to assign a

value when a field is declared. 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.

The following chart summarizes the

default values for the above data

types.

Data Type Default Value (for fields)

byte 0

short 0

int 0

long 0L

float 0.0f

double 0.0d

char '\u0000'

boolean false

String (or any object) null

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部