0

只是为了在Java中清楚地表达出来。

原始类型:

这是一个声明权:

int a;//declared but unitialized

初始化和赋值:

a = 1;//intialization and assignment

a = 2;//this is no longer intialization but still an assignment the 2nd time?

int b = 1;//declaration and intialization = assignment combined?

b = 2;//just assignment because 2nd time?

班级类型:

String str;//declaration

str = "some words";//this is also an intialization and assignment?

str = "more words"; //since new object still both intialization and assignment even 2nd time?
4

2 回答 2

0

初始化是第一次赋值。总是。

于 2012-01-04T09:55:21.127 回答
0

当编译器知道已设置局部变量并且可以无错误地读取时,它会认为变量已初始化。

考虑这种情况,编译器无法确定a变量已被初始化。

int a;
try {
    a = 1;
    // a is initialised and can be read.
    System.out.println(a); // compiles ok.
} catch (RuntimeException ignored) {
}
// a is not considered initialised as the compiler 
// doesn't know the catch block couldn't be thrown before a is set.
System.out.println(a); // Variable 'a' might not have been initialized

a = 2;
// a is definitely initialised.
System.out.println(a); // compiles ok.
于 2012-01-04T09:22:17.063 回答