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

发布于 2024-08-28 18:10:22 字数 681 浏览 7 评论 0原文

我发现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

沙与沫 2024-09-04 18:10:22

赋值:丢弃变量的旧值并用新值替换

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

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

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 as 0 or false. 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.

水水月牙 2024-09-04 18:10:22
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
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
猫弦 2024-09-04 18:10:22

声明并不是向变量声明“值”;它是声明变量的类型

赋值只是将值存储到变量。

初始化是在声明时为变量赋值。。

这些定义也适用于字段。

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

不过,应该提到的是,“初始化”对于“第一次给变量赋值”也有更宽松的定义,无论它发生在哪里。

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

然而,这可以编译:

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

这里,可以通过简单的赋值从两个可能的位置“初始化”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.

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 i can be "initialized" from two possible locations, by simple assignments. Because of that, if i 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:

  • 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 final variable at multiple places.
      • The compiler would do its best to ensure that exactly one of those assignments can happen, thus "initializing" the final variable

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

迷途知返 2024-09-04 18:10:22

这是带有一些示例的简短说明。

声明:
声明是用名称声明一个变量,并且一个变量只能声明一次。

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

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

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

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

花心好男孩 2024-09-04 18:10:22

声明:每当您使用其类型定义新变量时

赋值:每当您通过给变量赋予新值来更改其值时

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

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

寒冷纷飞旳雪 2024-09-04 18:10:22

我是C/C++出身,但是想法应该是一样的。

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

int x;

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

x = 3 + 5;

它知道将整数值 8 (不是浮点值 8)放入也称为“x”的内存位置。

赋值 - 这是您将值填充到先前声明的变量中的时候。赋值与“等号”相关。在前面的示例中,变量“x”被分配了值 8。

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

int x = 6;

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

const int c = 15;

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

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:

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 = 3 + 5;

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:

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.

暗地喜欢 2024-09-04 18:10:22

步骤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)

独夜无伴 2024-09-04 18:10:22
Declaration
When we first create a variable of any Data Type is call Declaration. Example: 

int obj;
String str;

Initialization
When (Declaration is completed) or variable is created the assigned any value to this variable is call Initialization. Example 
    int obj = 10;
    String str = "Azam Khan";

Assignment
Reassign value to that variable that is already initialized is call Assignment. Example
int obj = 15;
obj = 20;       // it print **20** value.
String str = "simple"
str = "Azam Khan"  // it's result or print **Azam Khan** value...

简单的程序

class ABC{
public static void main(String args[]){
int obj; // this is **Declaration:**
int  testValue= 12;   // After the **Declaration**, 
testValue is **Initialize**. 
testValue = 25;
System.out.println(textValue);
}
} 
the output result is 25.

它保存了25的值。

Declaration
When we first create a variable of any Data Type is call Declaration. Example: 

int obj;
String str;

Initialization
When (Declaration is completed) or variable is created the assigned any value to this variable is call Initialization. Example 
    int obj = 10;
    String str = "Azam Khan";

Assignment
Reassign value to that variable that is already initialized is call Assignment. Example
int obj = 15;
obj = 20;       // it print **20** value.
String str = "simple"
str = "Azam Khan"  // it's result or print **Azam Khan** value...

Simple program

class ABC{
public static void main(String args[]){
int obj; // this is **Declaration:**
int  testValue= 12;   // After the **Declaration**, 
testValue is **Initialize**. 
testValue = 25;
System.out.println(textValue);
}
} 
the output result is 25.

it saves the value of 25.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文