当前位置:编程学习 > JAVA >>

if(true==a) 和 if(a==true) 的区别???

Java中,if(true==a)和if(a==true)的区别啊,谁知道说一下,最好举个例子。谢谢了。 --------------------编程问答-------------------- ding --------------------编程问答-------------------- if(true==a)是两个变量之间的比较,if(a==true)是变量a是否为真的判断!不知这样说可否... --------------------编程问答-------------------- 既然a是boolean了,为什么还要a==true,直接if(a)不就得了。 --------------------编程问答-------------------- 问的 太深奥了 --------------------编程问答--------------------
引用 3 楼 oklinsong 的回复:
既然a是boolean了,为什么还要a==true,直接if(a)不就得了。

顶 --------------------编程问答-------------------- java使用if(a),这个问题在C/C++里才有意义。
旧话题里有讨论过,提问前先搜索吧○( ̄︿ ̄) ○ --------------------编程问答-------------------- 看视频时,说是有区别,但具体什么区别这段视频,没看见,所以问一下。“指定有区别” --------------------编程问答-------------------- 深奥,我记得看尚学堂那视频里面提到过,但是没有说明为什么。 --------------------编程问答-------------------- 什么视频?给个出处...<(  ̄︿ ̄)︵ --------------------编程问答-------------------- 就是尚学堂的 --------------------编程问答-------------------- boolean a = true;

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
所以 我感觉没什么区别!
--------------------编程问答-------------------- 最多可能效率上有点差别 但肯定差别不大  这种肯定只能出现在极端的面试题中   。。。。 --------------------编程问答-------------------- 根据java代码的编写规则
是不允许写if(a==true)这样的代码的。

正确:if(a) or if(!a).

所以建议楼主不要再去深究这个没有意义的问题了。 --------------------编程问答-------------------- 以我在项目中经验,这两个区别我们在项目中推荐使用true==a,是为了防止有人漏写一个=号,做这种判断时定值放在前面少写=号会报编译错,而反过来写就不会,导致结果出错后还不容易调试出来。 --------------------编程问答-------------------- 答案:没有区别

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 != RelationalExpression

The == (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. --------------------编程问答-------------------- 虽然,没怎么明白,不过还是谢谢了 --------------------编程问答-------------------- 其实没什么差别的,顺便说下
引用 2 楼 bingshanyijiao_fkx 的回复:
if(true==a)是两个变量之间的比较,if(a==true)是变量a是否为真的判断!不知这样说可否...


2楼,在你的java程序里,true可以当变量???? --------------------编程问答--------------------
引用 18 楼 valen_jia 的回复:
其实没什么差别的,顺便说下
引用 2 楼 bingshanyijiao_fkx 的回复:
if(true==a)是两个变量之间的比较,if(a==true)是变量a是否为真的判断!不知这样说可否...


2楼,在你的java程序里,true可以当变量????


哈,我也想说这个来着。。当时看到的时候愣了一下,还想了一会。。。 --------------------编程问答-------------------- 效果上没区别
只是  a==true, 常会被误写成  a=true  (赋值表达式),
从而导致bug,并且不容易看出来

所以一般写程序的时候,是把常量写在前面的, 即  true==a
如果错写成true=a(赋值表达式)的话,是编译不过的

只是利用编译器,避免一些低级bug而已 --------------------编程问答--------------------
引用 10 楼 zjg_snail 的回复:
就是尚学堂的


教程的东西不一定是全对的。我看到过不少教程都有错误。 重要是能自己判断,自己找原因,即使判断错了,也没关系,等知道正解后影响更深刻。 --------------------编程问答-------------------- 无论if(true==a)还是if(a==true),以及sun推荐的书写规范if(a)
编译之后都是
0 iload_1 [a]
1 ifeq 3
3 return

(ifeq会转向某个语句,行号到是不一定。)

实在找不到理由放弃if(a),要自找麻烦写成if(true==a)或者if(a==true)
--------------------编程问答-------------------- 一般来说 a ==b 和b==a 结果程序肯定会认为是一致的
这个要求用户自己实现objet父类中的equals方法 
而这个方法要求用户实现时必须满足等价关系,这个属于等价关系中的 自反性

所以这个结果是一致滴
因为jdk自己实现了自反性
--------------------编程问答--------------------
引用 15 楼 ltandfyy1 的回复:
以我在项目中经验,这两个区别我们在项目中推荐使用true==a,是为了防止有人漏写一个=号,做这种判断时定值放在前面少写=号会报编译错,而反过来写就不会,导致结果出错后还不容易调试出来。


同意15楼,没有区别,这样写是为了避免写成赋值语句,语句更安全。 --------------------编程问答-------------------- 呵呵 15楼提的好处 学习了
避免手误 --------------------编程问答-------------------- 在面试题当中遇到过这个问题,  问你习惯用  a=="abc" 还是 "abc"==a  --------------------编程问答-------------------- 我也是慢慢地习惯"abc"==a 这种写法了 --------------------编程问答-------------------- true==a是变量之间的比较而a==true是判断它是否为真
--------------------编程问答-------------------- 好像在底层比较时有一个区别,和先后指向有关
代码里区别不大 --------------------编程问答-------------------- 15楼正解!

在写代码的时候为了避免手误,把“==”(判等)敲成“=”(赋值)引起错误,推荐采用 if(常量 == 变量) 的模式。

但这个模式一般不用在 java 中,而是在 C++ 或 c 中。因为 java 中如果用IDE的话,会直接给出提示,没有用IDE也会在编译时报错。

而在 c 或 c++ 中就不一样了,请看下面程序。

正确代码:

int main()
{
    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 型变量的示例外。 --------------------编程问答--------------------
引用 15 楼 ltandfyy1 的回复:
以我在项目中经验,这两个区别我们在项目中推荐使用true==a,是为了防止有人漏写一个=号,做这种判断时定值放在前面少写=号会报编译错,而反过来写就不会,导致结果出错后还不容易调试出来。

主要还是这个原因 --------------------编程问答-------------------- 就 if(a)省的引起分歧。 --------------------编程问答-------------------- 按人类思维来看是有区别,可是编译输出是没区别的 --------------------编程问答-------------------- 尚学堂在讲这个问题的时候的确讲的是对的,楼主不去听听看看,就胡下定论。

记住:科学是严谨的,就算有培训的地方有错误也很是正常,如果没错误,还要我们这些程序员干嘛! --------------------编程问答-------------------- 我考,A==B与Equals有什么鸟关系? --------------------编程问答-------------------- 呵呵

好像还真没有什么区别 --------------------编程问答-------------------- 顶15楼,这是书写代码的一个好习惯。写程序时少写个=号是常见,像a = true;不一定会报错,但true = a;就是有错的,编译就不会通过。 --------------------编程问答--------------------
引用 37 楼 kaguhuno 的回复:
顶15楼,这是书写代码的一个好习惯。写程序时少写个=号是常见,像a = true;不一定会报错,但true = a;就是有错的,编译就不会通过。

正解。
原因恰恰是因为a是个布尔变量,因此赋值表达式“a = true”也是布尔型的,因此
if(a = true)
是完全可以通过编译的,导致很难检查的错误。
而if(true = a)这种手误则是通不过编译的。 --------------------编程问答-------------------- 没有区别 --------------------编程问答-------------------- 学习了! --------------------编程问答-------------------- 在java中应该没有什么区别吧,但在c++中0==一个数与一个数==于0应该有区别吧? --------------------编程问答-------------------- if(true==a)有时候这种判断之所以把变量放后面是为了防止空指针异常,因为如果a为null的话把a放前面就会报空指针异常,而放后面不会。代码是从前往后执行的 --------------------编程问答-------------------- 不要把简单的问题复杂化了,这是没有区别的


只是说在编码的时候为了规范,将true写在前面是为了防止粗心的程序员将==写成=,如果写成true=a那么在编译时就会报错,系统常量是不能被赋值的. --------------------编程问答--------------------
引用 15 楼 ltandfyy1 的回复:
以我在项目中经验,这两个区别我们在项目中推荐使用true==a,是为了防止有人漏写一个=号,做这种判断时定值放在前面少写=号会报编译错,而反过来写就不会,导致结果出错后还不容易调试出来。


看来不用我打出来了 --------------------编程问答-------------------- 这问题真硬啊 看似只是一个简单的分支判断语句 其实很深奥啊 根本没注意还有这么用的 我真是失败啊 ... --------------------编程问答--------------------
引用 15 楼 ltandfyy1 的回复:
以我在项目中经验,这两个区别我们在项目中推荐使用true==a,是为了防止有人漏写一个=号,做这种判断时定值放在前面少写=号会报编译错,而反过来写就不会,导致结果出错后还不容易调试出来。


 认为这是对的 --------------------编程问答-------------------- 怎么还那么多人支持==true
java里逻辑表达式结果只能是true或false,这是java和其他C语系语言的一个区别.
无论IBM还是SUN,都推荐if(a)的书写方式,把==true或者==false写出来不是自找麻烦么
<(  ̄︿ ̄)︵ --------------------编程问答--------------------
引用 46 楼 mintazoedeng 的回复:
引用 15 楼 ltandfyy1 的回复:
以我在项目中经验,这两个区别我们在项目中推荐使用true==a,是为了防止有人漏写一个=号,做这种判断时定值放在前面少写=号会报编译错,而反过来写就不会,导致结果出错后还不容易调试出来。


认为这是对的


楼主的例子引发了两种讨论唉。 --------------------编程问答-------------------- if (a == true)
if (true == a)
if (a)
--------------------编程问答-------------------- 15楼正解 30楼详细说明的很清楚。 --------------------编程问答-------------------- 呵呵,避免低级错误的出现 if(a=true)
当你用if(true=a)编译器很容易检测到 --------------------编程问答-------------------- 楼上正解 http://www.leadtoit.cn --------------------编程问答-------------------- java也有人讨论这种破问题啊。。。 --------------------编程问答-------------------- ding --------------------编程问答--------------------
引用 51 楼 Aoqun 的回复:
呵呵,避免低级错误的出现 if(a=true)
当你用if(true=a)编译器很容易检测到
--------------------编程问答-------------------- JAVA初学者,受教了,谢谢! --------------------编程问答-------------------- 有区别吗? --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 学习了! --------------------编程问答--------------------
引用 15 楼 ltandfyy1 的回复:
以我在项目中经验,这两个区别我们在项目中推荐使用true==a,是为了防止有人漏写一个=号,做这种判断时定值放在前面少写=号会报编译错,而反过来写就不会,导致结果出错后还不容易调试出来。


正解。 --------------------编程问答-------------------- 学习 --------------------编程问答--------------------
引用 3 楼 oklinsong 的回复:
既然a是boolean了,为什么还要a==true,直接if(a)不就得了。


这个对。。哈哈  --------------------编程问答-------------------- 有点晕 --------------------编程问答-------------------- 一个 当a为空的时候 会引发异常...
  而后者却不会.

  --------------------编程问答--------------------
引用楼主 zjg_snail 的帖子:
Java中,if(true==a)和if(a==true)的区别啊,谁知道说一下,最好举个例子。谢谢了。

这种写法是多此一举的。 --------------------编程问答-------------------- 这样问是有前提的,a是一个引用,如果这个引用为null呢 --------------------编程问答-------------------- 没有区别。
--------------------编程问答--------------------
引用 15 楼 ltandfyy1 的回复:
以我在项目中经验,这两个区别我们在项目中推荐使用true==a,是为了防止有人漏写一个=号,做这种判断时定值放在前面少写=号会报编译错,而反过来写就不会,导致结果出错后还不容易调试出来。

这个在C/C++里面很多见, 这个应该就是你想要的区别...但要说实际执行时是没区别 --------------------编程问答-------------------- 应该是为了避免手误吧 --------------------编程问答-------------------- 学习了,15楼是对的。顶!!!! --------------------编程问答-------------------- 小问题,大哲学 --------------------编程问答--------------------
引用 15 楼 ltandfyy1 的回复:
以我在项目中经验,这两个区别我们在项目中推荐使用true==a,是为了防止有人漏写一个=号,做这种判断时定值放在前面少写=号会报编译错,而反过来写就不会,导致结果出错后还不容易调试出来。


dddddddddddddd --------------------编程问答-------------------- 没有区别哈写a==true时如果漏写"="还是可以通过,不便于检查错误,如果写成true==a 漏写"="是会报错  避免后面的麻烦事 --------------------编程问答-------------------- 这个问题没想过! --------------------编程问答-------------------- 楼主视频有问题!!!呵呵 --------------------编程问答-------------------- 这两种写法说明写代码的人意识还不到位, 写成 if(a) 才算是明白了 --------------------编程问答-------------------- adfadgasdgasgd --------------------编程问答-------------------- adfasdfasdf --------------------编程问答--------------------
引用 12 楼 czp0608 的回复:
Java code
        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(null!=a){}和if(a!=null){} --------------------编程问答-------------------- 学习了 --------------------编程问答-------------------- 学习了! --------------------编程问答-------------------- 看看 --------------------编程问答-------------------- 正解!!这是在实际工程项目中的经验。
引用 15 楼 ltandfyy1 的回复:
以我在项目中经验,这两个区别我们在项目中推荐使用true==a,是为了防止有人漏写一个=号,做这种判断时定值放在前面少写=号会报编译错,而反过来写就不会,导致结果出错后还不容易调试出来。
--------------------编程问答-------------------- 先写常量是怕你把“==”写成“=”吧
--------------------编程问答-------------------- 楼上正解 --------------------编程问答-------------------- 保证程序员在任何时候都不会因为少写一个=号而错误,如果写成a = true可以编译通过,但写成true = a就编译不通过 --------------------编程问答-------------------- 我来说说我的理解吧!

你在ECLIPSE中试试,if(a == true)和 if(true == a), 你故意少写一个“=”号,看看有什么不同 --------------------编程问答-------------------- 保证程序员在任何时候都不会因为少写一个=号而错误,如果写成a = true可以编译通过,但写成true = a就编译不通过 --------------------编程问答-------------------- 业余程序员和专业程序员的写作习惯问题,减少潜在的bug
看林悦博士的《高质量C++编程》第一章, --------------------编程问答-------------------- 有区别的
if(true==a)好一些,因为可以避免if(true=a)这样的错误
if(a==true)要差一些,因为if(a=true)在语法上是没有错误,但是逻辑是错误的 --------------------编程问答-------------------- 学习了 --------------------编程问答--------------------
引用 79 楼 xiaohandsame123 的回复:
引用 12 楼 czp0608 的回复:
Java code 
        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(null!=a){}和if(a!=null){}


不知道79楼的这个a和null有什么区别 --------------------编程问答-------------------- a为null的话,是不是会抛出异常?? --------------------编程问答-------------------- 是防止赋值操作吧? --------------------编程问答--------------------
引用 20 楼 truediego 的回复:
效果上没区别
只是  a==true, 常会被误写成  a=true  (赋值表达式),
从而导致bug,并且不容易看出来

所以一般写程序的时候,是把常量写在前面的, 即  true==a
如果错写成true=a(赋值表达式)的话,是编译不过的

只是利用编译器,避免一些低级bug而已



UP --------------------编程问答-------------------- 有人说看视频说不同,但是具体怎么个不同呢·~ --------------------编程问答--------------------
引用 18 楼 valen_jia 的回复:
其实没什么差别的,顺便说下 

引用 2 楼 bingshanyijiao_fkx 的回复:
if(true==a)是两个变量之间的比较,if(a==true)是变量a是否为真的判断!不知这样说可否... 



2楼,在你的java程序里,true可以当变量????

不值你是口误!  还是!!~~呼呼~~~!!! --------------------编程问答-------------------- 不是很清楚~~ --------------------编程问答-------------------- 对啊,三楼说的很对,也许你是在问问题但是你问的不符合逻辑,而且没有必要,编程不是想怎样就怎样的,是要符合规范和标准的 --------------------编程问答-------------------- 郁闷了。。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,