Java:定义术语初始化、声明和赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2614072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:54:24  来源:igfitidea点击:

Java: define terms initialization, declaration and assignment

javainitializationdeclarationvariable-assignment

提问by hhh

I find the defs circular, the subjects are defined by their verbs but the verbs are undefined! So how do you define them?

我发现 defs 循环,主语是由他们的动词定义的,但动词是未定义的!那么你如何定义它们呢?

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]

[更新,尝试用 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?

采纳答案by Silvio Donnini

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 nullvalue and primitive types have default values such as 0or false. Can be done in conjunction with declaration.

初始化:这是一种特殊的赋值:第一个。在初始化对象具有null值之前,原始类型具有默认值,例如0false。可以与声明一起完成。

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.

声明:声明声明变量的类型及其名称。一个变量只能声明一次。编译器使用它来帮助程序员避免错误,例如将字符串值分配给整数变量。在读取或分配变量之前,必须已声明该变量。

回答by Hyman

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

初始化:与声明一起完成的赋值,或者在任何情况下对变量完成的第一次赋值,通常是对对象的构造函数调用或对变量的普通赋值

回答by polygenelubricants

Declaration is not to declare "value" to a variable; it's to declare the typeof 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.

这些定义也适用于字段。

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


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.

但是,应该提到的是,“初始化”还有一个更宽松的定义,即“对变量的第一次赋值”,无论它发生在何处。

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

This, however, compiles:

然而,这编译:

int i; // the following also compiles if i were declared final
if (something) i = 42;
else i = 666;
System.out.println(i);

Here ican be "initialized" from two possible locations, by simple assignments. Because of that, if iwas an array, you can't use the special array initializer shorthand syntax with this construct.

这里i可以通过简单的赋值从两个可能的位置“初始化”。因此,如果i是数组,则不能在此构造中使用特殊的数组初始值设定项简写语法。

So basically "initialization" has two possible definitions, depending on context:

所以基本上“初始化”有两种可能的定义,具体取决于上下文:

  • In its narrowest form, it's when an assignment is comboed with declaration.
    • It allows, among other things, special array shorthand initializer syntax
  • More generally, it's when an assignment is first made to a variable.
    • It allows, among other things, assignments to a finalvariable at multiple places.
      • The compiler would do its best to ensure that exactly one of those assignments can happen, thus "initializing" the finalvariable
  • 在最狭隘的形式中,指的是赋值与声明相结合。
    • 它允许,除其他外,特殊的数组速记初始值设定项语法
  • 更一般地,这是第一次对变量进行赋值的时候。
    • 除其他外,它允许final在多个位置对变量进行赋值。
      • 编译器将尽最大努力确保这些赋值中的一个可以发生,从而“初始化”final变量


There's also JVM-context class and instance initialization, OOP-context object initialization, etc.

还有 JVM 上下文类和实例初始化、OOP 上下文对象初始化等。

回答by John

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

回答by bill

I come from a C/C++ background, but the ideas should be the same.

我来自 C/C++ 背景,但想法应该是相同的。

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:

声明- 声明变量时,它告诉编译器留出一块内存并将名称(和变量类型)与其关联。在 C/C++ 中,它可能如下所示:

int x;

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

编译器看到这一点并为 x 留出一个地址位置,并知道它应该使用什么方法来对 x 执行操作(不同的变量类型将使用不同的访问操作)。这样,当编译器运行到该行时

x = 3 + 5;

It knows to put the integer value 8 (not the floating point value 8) into the memory location also known as 'x'.

它知道将整数值 8(不是浮点值 8)放入也称为“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.

赋值- 这是将值填充到先前声明的变量中的时候。赋值与“等号”相关联。在前面的示例中,变量“x”被赋值为 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:

初始化- 这是当变量预设有值时。不能保证在变量声明期间将每个变量都设置为某个默认值(除非您明确这样做)。可以说初始化是变量的第一次赋值,但这并不完全正确,我将在稍后解释。典型的初始化是变量声明与赋值的混合,如下所示:

int x = 6;

The distinction between initialization and assignment becomes more important when dealing with constants, such as this...

在处理常量时,初始化和赋值之间的区别变得更加重要,例如...

const int c = 15;

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.

在处理常量时,您只能在声明/初始化时分配它们的值。否则,他们不能被触碰。这是因为常量通常位于程序存储器与数据存储器中,并且它们的实际分配发生在编译时与运行时。

回答by user2934504

Step 1: Declaration: int a;

第一步:声明:int a;

Step 2: Initialization: a = 5;

第二步:初始化:a = 5;

Step 3: Assignment: a = b; (ex: int b = 10 ; now a becomes 10)

第 3 步:赋值:a = b;(例如: int b = 10 ;现在 a 变为 10)

回答by Emm

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;

示例: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;

示例: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; x = 12;.......We just changed the value.

int x = 7; x = 12;.......我们只是改变了值。

String myName = "Emi"; myName = "John".......We just changed the value.

String myName = "Emi"; myName = "John".......我们只是改变了值。

Boolean myCondition = false; myCondition = true;.......We just changed the value.

Boolean myCondition = false; myCondition = true;.......我们只是改变了值。

Note: In memory will be saved the last value that we put.

注意:在内存中将保存我们放置的最后一个值。