hhh*_*hhh 36 java initialization declaration variable-assignment
我发现defs是圆形的,主语是由他们的动词定义的,但动词是未定义的!那么你如何定义它们呢?
通函定义
初始化:初始化变量.它可以在声明时完成.
赋值:为变量赋值.它可以在任何地方完成,只有最终标识符一次.
声明:向变量声明值.
[更新,尝试用lambda calc理解主题]
D(x type) = (?x.x is declared with type)
A(y D(x type)) = (?y.y is assigned to D(x type))
%Then after some beta reductions we get initialization.
D(x type) me human // "me" declared with type "human"
A(y (D(x type) me human)) asking // "asking" assigned to the last declaration
%if the last two statemets are valid, an initialization exists. Right?
Run Code Online (Sandbox Code Playgroud)
Sil*_*ini 67
赋值:抛弃变量的旧值并将其替换为新值
初始化:这是一种特殊的赋值:第一种.在初始化对象具有null
值之前,基元类型具有默认值,例如0
或false
.可以与声明一起完成.
声明:声明声明变量的类型及其名称.变量只能声明一次.编译器使用它来帮助程序员避免错误,例如将字符串值分配给整数变量.在读取或分配变量之前,必须声明该变量.
小智 62
String declaration;
String initialization = "initialization";
declaration = "initialization"; //late initialization - will initialize the variable.
// Without this, for example, in java, you will get a compile-time error if you try
// to use this variable.
declaration = "assignment"; // Normal assignment.
// Can be done any number of times for a non-final variable
Run Code Online (Sandbox Code Playgroud)
声明不是要声明变量的"价值"; 它是声明变量的类型.
赋值只是将值存储到变量中.
初始化是在声明时为变量赋值.
这些定义也适用于字段.
int i; // simple declaration
i = 42 // simple assignment
int[] arr = { 1, 2, 3 };
// declaration with initialization, allows special shorthand syntax for arrays
arr = { 4, 5, 6 }; // doesn't compile, special initializer syntax invalid here
arr = new int[] { 4, 5, 6 }; // simple assignment, compiles fine
Run Code Online (Sandbox Code Playgroud)
但是,应该提到的是,"初始化"对"变量的第一次赋值"也有一个更宽松的定义,无论它发生在何处.
int i; // local variable declaration
if (something) i = 42;
System.out.println(i);
// compile time error: The local variable i may not have been initialized
Run Code Online (Sandbox Code Playgroud)
然而,这编译:
int i; // the following also compiles if i were declared final
if (something) i = 42;
else i = 666;
System.out.println(i);
Run Code Online (Sandbox Code Playgroud)
这里i
可以通过简单的分配从两个可能的位置"初始化".因此,如果i
是数组,则不能将特殊数组初始化程序简写语法与此结构一起使用.
所以基本上"初始化"有两种可能的定义,具体取决于上下文:
final
在多个位置分配变量.
final
变量还有JVM上下文类和实例初始化,OOP上下文对象初始化等.
以下是一些示例的简短说明。
声明: 声明是当你声明一个带有名字的变量时,一个变量只能被声明一次。
示例:int x;
, String myName;
,Boolean myCondition;
初始化: 初始化是当我们将一个值放入一个变量中时,这发生在我们声明一个变量时。
示例:int x = 7;
, String myName = "Emi";
,Boolean myCondition = false;
赋值: 赋值是当我们已经声明或初始化了一个变量,并且我们正在改变它的值。您可以根据需要多次更改变量的值。
例子:
int x = 7;
x = 12;
.......我们只是改变了值。
String myName = "Emi";
myName = "John"
.......我们只是改变了值。
Boolean myCondition = false;
myCondition = true;
.......我们只是改变了值。
注意:在内存中将保存我们放置的最后一个值。
归档时间: |
|
查看次数: |
83587 次 |
最近记录: |