Shi*_*eph -10 c java boolean while-loop
我在java和c ++中尝试了下面的代码,但它在java中引发了一个错误,而它没有在c ++中引发错误.为什么会这样?
while("abc"){ }
Run Code Online (Sandbox Code Playgroud)
我知道它纯粹取决于语言的属性.但我想知道为什么java设置一个条件,在循环中只允许布尔值?
我相信你在谈论编译器错误而不是运行时
在java的情况下
while ( expression ) {
// expression must evaluate to boolean value true or false
// as far as I knwo "abc" is neither true or false when it comes to java hence error
}
Run Code Online (Sandbox Code Playgroud)
在C.
while ( expression ) {
// expression can be anything which finally gives a value of either 0 or a number
// "abc" in this case will evaluate to an address which is positive integer hence no error
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
166 次 |
| 最近记录: |