答案:没有区别Java语言规范 章节15.21 Equality Operators有详细的说明。总结一下: “==”运算符有三种用法, 1.数字比较,也就是说运算符两边都是数字类型 2.逻辑比较,运算符两边都是boolean或Boolean类型 3.引用比较(reference equality),用来比较对象(object)实例。楼主问的是第二种,只要等式两边都是(true)或(false),结果就是true,反之结果就是false这个和逻辑运算符(||)或(and)不一样,"and"是从左向右比较的,如A||B,A和B都要是true,结果才是true,但运行是表达式A先被运行,如果是false,B就不再被运行了。这个特点有时可以被利用上。 再符上java语言规范相关章节: The equality operators are syntactically left-associative (they group left-to-right), but this fact is essentially never useful; for example, a==b==c parses as (a==b)==c. The result type of a==b is always boolean, and c must therefore be of type boolean or a compile-time error occurs. Thus, a==b==c does not test to see whether a, b, and c are all equal. EqualityExpression: RelationalExpression EqualityExpression == RelationalExpression EqualityExpression != RelationalExpressionThe == (equal to) and the!= (not equal to) operators are analogous to the relational operators except for their lower precedence. Thus, a<b==c<d is true whenever a<b and c<d have the same truth value.The equality operators may be used to compare two operands that are convertible (§5.1.8) to numeric type, or two operands of type boolean or Boolean, or two operands that are each of either reference type or the null type. All other cases result in a compile-time error. The type of an equality expression is always boolean.In all cases, a!=b produces the same result as !(a==b). The equality operators are commutative if the operand expressions have no side effects.15.21.1 Numerical Equality Operators == and != If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2). If the promoted type of the operands is int or long, then an integer equality test is performed; if the promoted type is float or double, then a floating-point equality test is performed.Note that binary numeric promotion performs value set conversion (§5.1.13) and unboxing conversion (§5.1.8). Comparison is carried out accurately on floating-point values, no matter what value sets their representing values were drawn from.Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard: * If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN. (The methods Float.isNaN and Double.isNaN may also be used to test whether a value is NaN.) * Positive zero and negative zero are considered equal. Therefore, -0.0==0.0 is true, for example. * Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares unequal to all other values. Subject to these considerations for floating-point numbers, the following rules then hold for integer operands or for floating-point operands other than NaN: * The value produced by the == operator is true if the value of the left-hand operand is equal to the value of the right-hand operand; otherwise, the result is false. * The value produced by the != operator is true if the value of the left-hand operand is not equal to the value of the right-hand operand; otherwise, the result is false. 15.21.2 Boolean Equality Operators == and != If the operands of an equality operator are both of type boolean, or if one operand is of type boolean and the other is of type Boolean, then the operation is boolean equality. The boolean equality operators are associative.If one of the operands is of type Boolean it is subjected to unboxing conversion (§5.1.8).The result of == is true if the operands (after any required unboxing conversion) are both true or both false; otherwise, the result is false.The result of != is false if the operands are both true or both false; otherwise, the result is true. Thus != behaves the same as ^ (§15.22.2) when applied to boolean operands.15.21.3 Reference Equality Operators == and != If the operands of an equality operator are both of either reference type or the null type, then the operation is object equality.A compile-time error occurs if it is impossible to convert the type of either operand to the type of the other by a casting conversion (§5.5). The run-time values of the two operands would necessarily be unequal.At run time, the result of == is true if the operand values are both null or both refer to the same object or array; otherwise, the result is false.The result of != is false if the operand values are both null or both refer to the same object or array; otherwise, the result is true.While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters. The contents of two strings s and t can be tested for equality by the method invocation s.equals(t). See also §3.10.5.
旧话题里有讨论过,提问前先搜索吧○( ̄︿ ̄) ○
if(true == a){
System.out.println("true==a");
}
if(a == true){
System.out.println("a==true");
}结果:两个都会输出,
所以个人觉得区别不大,只是优先顺序不同。
boolean a = true;
if(true == a){
System.out.println("a");
}
if(a == true){
System.out.println("b");
}
if(a){
System.out.println("c");
}
结果是输出:a b c
所以 我感觉没什么区别!
是不允许写if(a==true)这样的代码的。正确:if(a) or if(!a).所以建议楼主不要再去深究这个没有意义的问题了。
“==”运算符有三种用法,
1.数字比较,也就是说运算符两边都是数字类型
2.逻辑比较,运算符两边都是boolean或Boolean类型
3.引用比较(reference equality),用来比较对象(object)实例。楼主问的是第二种,只要等式两边都是(true)或(false),结果就是true,反之结果就是false这个和逻辑运算符(||)或(and)不一样,"and"是从左向右比较的,如A||B,A和B都要是true,结果才是true,但运行是表达式A先被运行,如果是false,B就不再被运行了。这个特点有时可以被利用上。
再符上java语言规范相关章节:
The equality operators are syntactically left-associative (they group left-to-right), but this fact is essentially never useful; for example, a==b==c parses as (a==b)==c. The result type of a==b is always boolean, and c must therefore be of type boolean or a compile-time error occurs. Thus, a==b==c does not test to see whether a, b, and c are all equal.
EqualityExpression:
RelationalExpression
EqualityExpression == RelationalExpression
EqualityExpression != RelationalExpressionThe == (equal to) and the!= (not equal to) operators are analogous to the relational operators except for their lower precedence. Thus, a<b==c<d is true whenever a<b and c<d have the same truth value.The equality operators may be used to compare two operands that are convertible (§5.1.8) to numeric type, or two operands of type boolean or Boolean, or two operands that are each of either reference type or the null type. All other cases result in a compile-time error. The type of an equality expression is always boolean.In all cases, a!=b produces the same result as !(a==b). The equality operators are commutative if the operand expressions have no side effects.15.21.1 Numerical Equality Operators == and !=
If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands (§5.6.2). If the promoted type of the operands is int or long, then an integer equality test is performed; if the promoted type is float or double, then a floating-point equality test is performed.Note that binary numeric promotion performs value set conversion (§5.1.13) and unboxing conversion (§5.1.8). Comparison is carried out accurately on floating-point values, no matter what value sets their representing values were drawn from.Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard: * If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN. (The methods Float.isNaN and Double.isNaN may also be used to test whether a value is NaN.)
* Positive zero and negative zero are considered equal. Therefore, -0.0==0.0 is true, for example.
* Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares unequal to all other values. Subject to these considerations for floating-point numbers, the following rules then hold for integer operands or for floating-point operands other than NaN: * The value produced by the == operator is true if the value of the left-hand operand is equal to the value of the right-hand operand; otherwise, the result is false.
* The value produced by the != operator is true if the value of the left-hand operand is not equal to the value of the right-hand operand; otherwise, the result is false. 15.21.2 Boolean Equality Operators == and !=
If the operands of an equality operator are both of type boolean, or if one operand is of type boolean and the other is of type Boolean, then the operation is boolean equality. The boolean equality operators are associative.If one of the operands is of type Boolean it is subjected to unboxing conversion (§5.1.8).The result of == is true if the operands (after any required unboxing conversion) are both true or both false; otherwise, the result is false.The result of != is false if the operands are both true or both false; otherwise, the result is true. Thus != behaves the same as ^ (§15.22.2) when applied to boolean operands.15.21.3 Reference Equality Operators == and !=
If the operands of an equality operator are both of either reference type or the null type, then the operation is object equality.A compile-time error occurs if it is impossible to convert the type of either operand to the type of the other by a casting conversion (§5.5). The run-time values of the two operands would necessarily be unequal.At run time, the result of == is true if the operand values are both null or both refer to the same object or array; otherwise, the result is false.The result of != is false if the operand values are both null or both refer to the same object or array; otherwise, the result is true.While == may be used to compare references of type String, such an equality test determines whether or not the two operands refer to the same String object. The result is false if the operands are distinct String objects, even if they contain the same sequence of characters. The contents of two strings s and t can be tested for equality by the method invocation s.equals(t). See also §3.10.5.
只是 a==true, 常会被误写成 a=true (赋值表达式),
从而导致bug,并且不容易看出来所以一般写程序的时候,是把常量写在前面的, 即 true==a
如果错写成true=a(赋值表达式)的话,是编译不过的只是利用编译器,避免一些低级bug而已
编译之后都是
0 iload_1 [a]
1 ifeq 3
3 return(ifeq会转向某个语句,行号到是不一定。)实在找不到理由放弃if(a),要自找麻烦写成if(true==a)或者if(a==true)
这个要求用户自己实现objet父类中的equals方法
而这个方法要求用户实现时必须满足等价关系,这个属于等价关系中的 自反性所以这个结果是一致滴
因为jdk自己实现了自反性
{
int a = 0;
if(a == 3)
{
cout << "a=" << 3 << endl;
}else
{
cout << "a=" << a << endl;
}
}输出:
a=0有误代码:int main()
{
int a = 0;
if(a = 3) // 把判等误写为赋值
{
cout << "a=" << 3 << endl;
}else
{
cout << "a=" << a << endl;
}
}在 c 或者 c++ 中,编译器是无法检查出这个错误的,因为它的规则是只要非 0,就是真。
所以程序输出:
a=3习惯良好的代码:int main()
{
int a = 0;
if(3 = a) // 把判等误写为赋值
{
cout << "a=" << 3 << endl;
}else
{
cout << "a=" << a << endl;
}
}此时编译出错!但是这个 习惯在 java 中感觉不是那么重要,除了LZ的针对 boolean 型变量的示例外。
原因恰恰是因为a是个布尔变量,因此赋值表达式“a = true”也是布尔型的,因此
if(a = true)
是完全可以通过编译的,导致很难检查的错误。
而if(true = a)这种手误则是通不过编译的。
只是说在编码的时候为了规范,将true写在前面是为了防止粗心的程序员将==写成=,如果写成true=a那么在编译时就会报错,系统常量是不能被赋值的.
java里逻辑表达式结果只能是true或false,这是java和其他C语系语言的一个区别.
无论IBM还是SUN,都推荐if(a)的书写方式,把==true或者==false写出来不是自找麻烦么
<(  ̄︿ ̄)︵
if (true == a)
if (a)
当你用if(true=a)编译器很容易检测到
我认为还是有区别的,就像if(null!=a){}和if(a!=null){}
看林悦博士的《高质量C++编程》第一章,
if(true==a)好一些,因为可以避免if(true=a)这样的错误
if(a==true)要差一些,因为if(a=true)在语法上是没有错误,但是逻辑是错误的
不知道79楼的这个a和null有什么区别