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++ 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++. Asiis getting the value 4 and this is equivalent toif (4). But in Java boolean is different from int and you can not use int where boolean is required. Note then,i == 4is boolean buti = 4is int. The last one assignment, not compare.