Java中关于赋值语句作为if条件判断的问题
2018-04-01 21:30
573 查看
参考如下两段代码:public class Test{
public static void main(String [] lkl){
boolean flag = false;
if(flag = true){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
/*该程序输出结果为true,因为if条件语句通过赋值之后结果为true*/
public class Test{
public static void main(String [] lkl){
int flag = 520;
if(flag = 521){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
/*该程序执行过程中会报错:Type mismatch: cannot convert from int to boolean.表明类型不匹配*/总结:Java的条件判断括号内表达式最终返回的结果应该为boolean类型,否则会编译出错
public static void main(String [] lkl){
boolean flag = false;
if(flag = true){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
/*该程序输出结果为true,因为if条件语句通过赋值之后结果为true*/
public class Test{
public static void main(String [] lkl){
int flag = 520;
if(flag = 521){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
/*该程序执行过程中会报错:Type mismatch: cannot convert from int to boolean.表明类型不匹配*/总结:Java的条件判断括号内表达式最终返回的结果应该为boolean类型,否则会编译出错
相关文章推荐
- 关于JAVA调用ORACLE存储过程时,传入动态参数作为in语句条件的问题
- 赋值语句作为if的判断条件
- 关于JAVA调用ORACLE存储过程时,传入动态参数作为in语句条件的问题
- mybatis String的if条件判断问题 There is no getter for property named 'xxx' in 'class Java.lang.Integer'
- js关于变量作为if条件的真假问题
- mybatis 基本类型的if条件判断问题 There is no getter for property named 'xxx' in 'class Java.lang.Integer'
- java的if else if语句的判断条件不能相同
- javascript:if中可以使用那些作为判断条件
- 关于JAVA中if表达式中的赋值语句
- js面向对象编程:if中可以使用那些作为判断条件呢?
- 关于CSocket::Accept()能不能作为接收客户端消息循环的判断条件
- 关于java以竖线作为分隔符split字符串的问题
- JavaScript 哪些值可以作为If 判断条件
- 使用mybatis遇到的关于条件查询">"以及if test传参的使用问题
- 关于mybatis中基本类型条件判断问题
- 剑指Offer(Java版): 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
- C语言与或非位运算 Java判断中 if条件
- 关于IF判断的问题(一个有趣的事情)(分支-08. 高速公路超速处罚(15))
- 关于if条件中一个变量和0或其它值比较,变量放置位置的问题
- 关于mybatis中基本类型条件判断问题