Java:定义术语初始化、声明和赋值
我发现 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?
I find the defs circular, the subjects are defined by their verbs but the verbs are undefined! So how do you define them?
The Circular Definitions
initialization: to initialize a variable. It can be done at the time of
declaration.
assignment: to assign value to a variable. It can be done anywhere, only once with the final-identifier.
declaration: to declare value to a variable.
[update, trying to understand the topic with 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
赋值:丢弃变量的旧值并用新值替换
初始化:这是一种特殊的赋值:第一种。在初始化之前,对象具有
null
值,原始类型具有默认值,例如0
或false
。可以与声明一起进行。声明:声明说明变量的类型及其名称。一个变量只能声明一次。编译器使用它来帮助程序员避免错误,例如将字符串值分配给整数变量。在读取或分配变量之前,必须先声明该变量。
assignment: throwing away the old value of a variable and replacing it with a new one
initialization: it's a special kind of assignment: the first. Before initialization objects have
null
value and primitive types have default values such as0
orfalse
. Can be done in conjunction with declaration.declaration: a declaration states the type of a variable, along with its name. A variable can be declared only once. It is used by the compiler to help programmers avoid mistakes such as assigning string values to integer variables. Before reading or assigning a variable, that variable must have been declared.
声明并不是向变量声明“值”;它是声明变量的类型。
赋值只是将值存储到变量。
初始化是在声明时为变量赋值。。
这些定义也适用于字段。
不过,应该提到的是,“初始化”对于“第一次给变量赋值”也有更宽松的定义,无论它发生在哪里。
然而,这可以编译:
这里,可以通过简单的赋值从两个可能的位置“初始化”i。因此,如果
i
是一个数组,则不能在此构造中使用特殊的数组初始值设定项简写语法。因此,基本上“初始化”有两种可能的定义,具体取决于上下文:
final
变量进行赋值。final
变量还有 JVM 上下文类和实例初始化、OOP 上下文对象初始化, ETC。
Declaration is not to declare "value" to a variable; it's to declare the type of the variable.
Assignment is simply the storing of a value to a variable.
Initialization is the assignment of a value to a variable at the time of declaration.
These definitions also applies to fields.
However, it should be mentioned that "initialization" also has a more relaxed definition of "the first assignment to a variable", regardless of where it happens.
This, however, compiles:
Here
i
can be "initialized" from two possible locations, by simple assignments. Because of that, ifi
was an array, you can't use the special array initializer shorthand syntax with this construct.So basically "initialization" has two possible definitions, depending on context:
final
variable at multiple places.final
variableThere's also JVM-context class and instance initialization, OOP-context object initialization, etc.
这是带有一些示例的简短说明。
声明:
声明是用名称声明一个变量,并且一个变量只能声明一次。
示例:
int x;
、String myName;
、Boolean myCondition;
初始化:
初始化是当我们将值放入变量时,这在我们声明变量时发生。
示例:
int x = 7;
、String myName = "Emi";
、Boolean myCondition = false;
赋值:
赋值是指我们已经声明或初始化了一个变量,并且正在更改该值。您可以根据需要多次更改变量的值。
示例:
int x = 7;
x = 12; ......我们刚刚更改了值。
String myName = "艾米";
myName = "John" ......我们刚刚更改了值。
布尔值 myCondition = false;
......我们刚刚更改了值。myCondition = true;
注意:内存中将保存我们输入的最后一个值。
Here is a short explanation with some examples.
Declaration:
Declaration is when you declare a variable with a name, and a variable can be declared only once.
Example:
int x;
,String myName;
,Boolean myCondition;
Initialization:
Initialization is when we put a value in a variable, this happens while we declare a variable.
Example:
int x = 7;
,String myName = "Emi";
,Boolean myCondition = false;
Assignment:
Assignment is when we already declared or initialized a variable, and we are changing the value. You can change value of the variable as many time you want or you need.
Example:
int x = 7;
.......We just changed the value.x = 12;
String myName = "Emi";
.......We just changed the value.myName = "John"
Boolean myCondition = false;
.......We just changed the value.myCondition = true;
Note: In memory will be saved the last value that we put.
声明:每当您使用其类型定义新变量时
赋值:每当您通过给变量赋予新值来更改其值时
初始化:与声明一起完成的赋值,或者在任何情况下与变量一起完成的第一个赋值,通常是对象的构造函数调用或变量的普通赋值
declaration: whenever you define a new variable with its type
assignment: whenever you change the value of a variable by giving it a new value
initialization: an assignment that is done together with the declaration, or in any case the first assignment that is done with a variable, usually it's a constructor call for an object or a plain assignment for a variable
我是C/C++出身,但是想法应该是一样的。
声明 - 当声明变量时,它告诉编译器留出一块内存并将名称(和变量类型)与其关联。在 C/C++ 中,它可能如下所示:
编译器看到这一点并为 x 留出一个地址位置,并知道应该使用什么方法对 x 执行操作(不同的变量类型将使用不同的访问操作)。这样,当编译器运行到行时,
它知道将整数值 8 (不是浮点值 8)放入也称为“x”的内存位置。
赋值 - 这是您将值填充到先前声明的变量中的时候。赋值与“等号”相关。在前面的示例中,变量“x”被分配了值 8。
初始化 - 这是为变量预设值的情况。不能保证变量在变量声明期间都会被设置为某个默认值(除非您明确地这样做)。可以说初始化是变量的第一次赋值,但这并不完全正确,我将很快解释。典型的初始化是变量声明与赋值的混合,如下所示:
在处理常量时,初始化和赋值之间的区别变得更加重要,例如这样...
当处理常量时,您只能在声明/初始化的时间。不然的话,他们是碰不到的。这是因为常量通常位于程序存储器与数据存储器中,并且它们的实际赋值发生在编译时与运行时。
I come from a C/C++ background, but the ideas should be the same.
Declaration - When a variable is declared, it is telling the compiler to set aside a piece of memory and associate a name (and a variable type) with it. In C/C++ it could look like this:
The compiler sees this and sets aside an address location for x and knows what methods it should use to perform operations on x (different variable types will use different access operations). This way, when the compiler runs into the line
It knows to put the integer value 8 (not the floating point value 8) into the memory location also known as 'x'.
Assignment - This is when you stuff a value into the previously declared variable. Assignment is associated with the 'equals sign'. In the previous example, the variable 'x' was assigned the value 8.
Initialization - This is when a variable is preset with a value. There is no guarantee that a variable will every be set to some default value during variable declaration (unless you explicitly make it so). It can be argued that initialization is the first assignment of a variable, but this isn't entirely true, as I will explain shortly. A typical initialization is a blend of the variable declaration with an assignment as follows:
The distinction between initialization and assignment becomes more important when dealing with constants, such as this...
When dealing with constants, you only get to assign their value at the time of declaration/initialization. Otherwise, they can't be touched. This is because constants are often located in program memory vs data memory, and their actual assignment is occurring at compile time vs run time.
步骤1:声明:int a;
步骤2:初始化:a = 5;
步骤3:赋值:a = b; (例如:int b = 10;现在 a 变为 10)
Step 1: Declaration : int a;
Step 2: Initialization : a = 5;
Step 3: Assignment: a = b; (ex: int b = 10 ; now a becomes 10)
简单的程序
它保存了25的值。
Simple program
it saves the value of 25.