社区
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中语句是按什么样解析顺序来解析的?
...全文
748
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
作业
写回复
配置赞助广告
用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错误 Cannot cast from
boolean
to int或cannot
convert
from int to
boolean
不要在Java学习过程中保留C语言里的坏习惯 例如以下片段: int cmp = 1; if(cmp){ //do something } 在C和C++中是合法的,但在Java中不能通过类型检测!
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...
字符串的错误
Type mismatch: cannot
convert
from java.lang.
String
to
String
类的名字,
String
是javaJDK中的类,重名了,改成
String
s
java小白训练营2109-day03-数据结构:基本类型+包装类型+数组+对象
文章目录程序 程序
Java SE
62,635
社区成员
307,269
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章