Java:定义术语初始化、声明和赋值
我发现defs循环,主语由动词定义,但动词未定义!那么你如何定义它们呢 循环定义 初始化:初始化变量。可以在 声明 赋值:为变量赋值。它可以在任何地方进行,只有一次使用最终标识符 声明:向变量声明值 [更新,尝试用lambda calc理解主题]Java:定义术语初始化、声明和赋值,java,initialization,declaration,variable-assignment,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 w
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?
声明:每当您使用其类型定义新变量时
赋值:每当您通过给变量一个新值来更改它的值时
初始化:与声明一起完成的赋值,或在任何情况下与变量一起完成的第一个赋值,通常是对象的构造函数调用或变量的普通赋值声明不向变量声明“值”;这是为了声明变量的类型 赋值就是将一个值存储到一个变量中 初始化是在声明时向变量赋值 这些定义也适用于字段
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
- 编译器将尽最大努力确保这些赋值中只有一个可以发生,从而“初始化”
- 除其他外,它允许在多个位置为
还有JVM上下文类和实例初始化、OOP上下文对象初始化等。赋值:丢弃变量的旧值并用新值替换它 初始化:这是一种特殊的分配:第一种。在初始化之前,对象具有
null
值,基元类型具有默认值,例如0
或false
。可与声明一起完成
声明:声明声明变量的类型及其名称。变量只能声明一次。编译器使用它来帮助程序员避免错误,例如将字符串值分配给整数变量。在读取或分配变量之前,必须先声明该变量。我有C/C++背景,但想法应该是一样的
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
声明-声明变量时,它告诉编译器留出一段内存,并将名称(和变量类型)与之关联。在C/C++中,它可能如下所示:
int x;
编译器会看到这一点,并为x留出一个地址位置,并知道应该使用什么方法对x执行操作(不同的变量类型将使用不同的访问操作)。这样,当编译器运行到该行时
x = 3 + 5;
它知道将整数值8(不是浮点值8)放入内存位置,也称为“x”
赋值-将值填充到先前声明的变量中。赋值与“等号”关联。在上一个示例中,变量“x”的值为8
初始化-这是当一个变量预设了一个值时。在变量声明过程中,不能保证每个变量都会被设置为某个默认值(除非您显式地这样做)。可以说,初始化是变量的第一个赋值,但正如我稍后将解释的那样,这并不完全正确。典型的初始化是变量声明与赋值的混合,如下所示:
int x = 6;
在处理常量时,初始化和赋值之间的区别变得更加重要,例如
const int c = 15;
在处理常量时,您只能在声明/初始化时分配它们的值。否则,它们就不能被触摸。这是因为常量通常位于程序内存和数据内存中,它们的实际赋值发生在编译时和运行时 步骤2:初始化:a=5
第三步:分配:a=b;(例如:int b=10;现在a变为10)这里是一个简短的解释,并附有一些示例 声明: 声明是指使用名称声明变量,并且变量只能声明一次 例如:
intx代码>,字符串myName代码>,布尔型myCondition代码>
初始化:
初始化是当我们在变量中放入一个值时,在我们声明一个变量时会发生这种情况
例如:intx=7代码>,字符串myName=“Emi”代码>,布尔myCondition=false代码>
作业:
赋值是指我们已经声明或初始化了一个变量,并且正在更改该值。您可以根据需要随时更改变量的值
例如:
intx=7;
x=12代码>。
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.