[英]Whats the difference between = and == in java?
我知道==
意味着等于,但我无法弄清楚=
意味着什么。
A single =
is assignment. 单个
=
是分配。 A value is assigned to a variable. 将值分配给变量。
int a = 1; // <-- assign 1 to a.
JLS-15.26.1. JLS-15.26.1。 Simple Assignment Operator
=
says (in part) 简单分配运算符
=
说(部分)
A compile-time error occurs if the type of the right-hand operand cannot be converted to the type of the variable by assignment conversion ( §5.2 ).
如果无法通过赋值转换将右侧操作数的类型转换为变量的类型(第5.2节 ),则会发生编译时错误。
The ==
is the equality operator, JLS-15.21. ==
是相等运算符JLS-15.21。 Equality Operators says (in part), 平等经营者说(部分),
The operators
==
(equal to) and!=
(not equal to) are called the equality operators .==
(等于)和!=
(不等于)的运算符称为相等运算符 。
=
means assignment operator which assigns the value on its right to the operand on its left whereas ==
(Equal to) means equality check. =
表示赋值运算符 ,它将其右侧的值赋给其左侧的操作数,而==
(等于)表示相等性检查。
Say, you want to assign 1
into a variable i
, so you have to write: 假设您想将
1
赋给变量i
,所以您必须编写:
i = 1;
But if you want to check whether the value of i
is 1
or not, you have to check: 但是,如果要检查
i
的值是否为1
,则必须检查:
if (i == 1) {
//do something
} else {
// do something else
}
=
is the assignment operator. =
是赋值运算符。 Eg, a = 5
means assigning the value of 5
to the variable a
. 例如,
a = 5
装置的值分配5
到可变a
。
the operator "=" assign the value to a certain instance 运算符“ =”将值分配给特定实例
but
但
an operator "==" means a certain instance has a value of something 运算符“ ==”表示某个实例具有某些值
example 例
x = 2; //It means x is 2
x == 2; //means x has a value of 2
=
is the assignment operator which is used to assign a value to a variable, property or fields. =
是分配运算符,用于将值分配给变量,属性或字段。 While ==
is used to check a condition for example in a if
condition 而
==
用于检查条件,例如if
条件
int houseAddress = 1;
This means that the variable houseAddress is given the value of 1, so you can think of this as the address of the house is equal to 1; 这意味着变量houseAddress的值为1,因此您可以将其视为房子的地址等于1;
if(houseAddress == 1){
//do something
}
This code is saying whether houseAddress is equal to 1 this would return TRUE or FALSE in this case we know that houseAddress is 1 so it return TRUE. 这段代码说的是houseAddress等于1,这将返回TRUE或FALSE,在这种情况下,我们知道houseAddress为1,因此它返回TRUE。
Hope this helps its missing a few technical detail which might confuse you so its missed out. 希望这有助于它丢失一些技术细节,这可能会使您感到困惑,因此错过了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.