社区
Java SE
帖子详情
cannot convert from boolean to String
Jamesmack
2008-06-30 04:16:32
1 String str1 = "Haha" + true == false + "heihei";
2 String str2 = "Haha" + true == false;
3 boolean b = "Haha" + true == false + "heihei";
为什么1,2两句都有错误,而3却是正确的,JAVA中语句是按什么样解析顺序来解析的?
...全文
696
9
打赏
收藏
cannot convert from boolean to String
1 String str1 = "Haha" + true == false + "heihei"; 2 String str2 = "Haha" + true == false; 3 boolean b = "Haha" + true == false + "heihei"; 为什么1,2两句都有错误,而3却是正确的,JAVA中语句是按什么样解析顺序来解析的?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
9 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
xuhaiyang
2008-06-30
打赏
举报
回复
优先级不一样
adrain_sengtang
2008-06-30
打赏
举报
回复
这个问题主要
1、+的
优先级
高于==
2、String类型与boolean进行+运算,会
自动
调用Boolean.toString()
结果将是String类型
3、不能直接将Boolean类型转换为String类型
ldy214
2008-06-30
打赏
举报
回复
[Quote=引用 3 楼 bestaone 的回复:]
1 String str1 = "Haha" + true == false + "heihei";
2 String str2 = "Haha" + true == false;
3 boolean b = "Haha" + true == false + "heihei";
相当与
1 String str1 = ("Haha" + true) == (false + "heihei");
("Haha" + true) == (false + "heihei")左右两边都是String可以比较,但比较结果为boolean,所以出错
2 String str2 = ("Haha" + true) == false;
("Haha" + true)强制转换为…
[/Quote]
正解
qusic
2008-06-30
打赏
举报
回复
[Quote=引用 3 楼 bestaone 的回复:]
1 String str1 = "Haha" + true == false + "heihei";
2 String str2 = "Haha" + true == false;
3 boolean b = "Haha" + true == false + "heihei";
相当与
1 String str1 = ("Haha" + true) == (false + "heihei");
("Haha" + true) == (false + "heihei")左右两边都是String可以比较,但比较结果为boolean,所以出错
2 String str2 = ("Haha" + true) == false;
("Haha" + true)强制转换为String…
[/Quote]
完全分析好了~
MagicianLiu
2008-06-30
打赏
举报
回复
运算符优先级的问题。。
==运算符比+低
所以头两句相当于String s = false;所以肯定报错
但String s = false+"";不会报错。
其实这个是JDK1.5以后的自动拆箱装箱功能,即将基本数据类型与对象进行转换
例如String s = false+""相当于 String s = Boolean.toString(false)+"";
JDK1.5之前,没有自动拆箱装箱功能,必须写第二种写法
以上是装箱的例子,下面是拆箱的例子
int i = new Integer(1)+2;
lisl2003
2008-06-30
打赏
举报
回复
不能把boolean型转成string。(boolean值也不能与整型进行相互转化。)
语法的问题,至于到底是为什么,偶也不知。
码道功臣
2008-06-30
打赏
举报
回复
1 String str1 = "Haha" + true == false + "heihei";
2 String str2 = "Haha" + true == false;
3 boolean b = "Haha" + true == false + "heihei";
相当与
1 String str1 = ("Haha" + true) == (false + "heihei");
("Haha" + true) == (false + "heihei")左右两边都是String可以比较,但比较结果为boolean,所以出错
2 String str2 = ("Haha" + true) == false;
("Haha" + true)强制转换为String类型,与boolean比较所以出错
3 boolean b = ("Haha" + true) == (false + "heihei");
这完全符合java语法。
lisl2003
2008-06-30
打赏
举报
回复
不能把boolean型转成string?
Type mismatch: cannot
convert
from Object to
String
Type mismatch: cannot
convert
from Object to
String
java bool to int_java Type mismatch: cannot
convert
from int to
boolean
2017-08-17 08:06:100i had Problem with my code i has the error Type mismatch: cannot
convert
from int to
boolean
the variable i will use in a for Loop here is the codeimport java.awt.Color;import java...
Java报错 “cannot
convert
from element type Object to
String
”
想用增强的for循环输出数据,结果遇到 “cannot
convert
from element type Object to
String
” 如下为出错图片 下面这张是显示的错误报告 究其原因是ArrayList后面没有加类型,加上所需要的字符类型就可以了,我的是加<
String
> 完美解决! ...
Type mismatch: cannot
convert
from double to int
在对List集合按照所存对象的某一属性进行排序时,有如下表达式: Pictures.sort((o1,o2)-> o1.getTagX() - o2.getTagX()),tagX是对象Picture的一个属性,且为double型,错误提示是:Type mismatch: cannot
convert
from double to int,只知道是应该进行强制类型转换,因为两数相减为doub...
Day03-数据类型
1.java是一种强类型语言(安全性提高,速度变慢) 它要求变量的使用必须符合规定,所有变量都必须先定义后使用。 对其进行赋值为10: 还是有一个报错:Type mismatch: cannot
convert
from int to
String
(类型不匹配:无法从int转换为
String
) 原因:数据类型不同
String
是一个字符串,10是一个数字(int类型) 可以赋值为hello:给字符串赋值需要 " " 号 同一个类里面不可以有两个相同的方法名: ...
Java SE
62,615
社区成员
307,307
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章