如果条件在java中 [英] If condition in java

查看:76
本文介绍了如果条件在java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果下面给出的逻辑在C语言中运行良好,但它在java中不起作用....为什么.. ??

它在编译时在java中给出错误。

If logic given below works well in C language but it doesn't work in java....Why..??
It gives an error in java while compiling.

class test
  {
    public static void main(String[] args)
    {
    int i;
    if(i=4)
    System.out.println("hello");
    }
  }


推荐答案

In C / C ++任何非零值都被视为真,零视为假。也就是说,int和bool是可以互换的。所以 if(i = 4)在C / C ++中为真。当 i 获得值4时,这相当于 if(4)。但是在Java中,boolean与int不同,你不能使用int,其中boolean是必需的。注意, i == 4 是布尔值,但是 i = 4 是int。最后一项任务,而不是比较。

In C/C++ any non-zero value is considered as true, zero considered false. That is, int and bool are interchangeable. So if (i = 4) is true in C/C++. As i is getting the value 4 and this is equivalent to if (4). But in Java boolean is different from int and you can not use int where boolean is required. Note then, i == 4 is boolean but i = 4 is int. The last one assignment, not compare.

这篇关于如果条件在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆