比较原始类型
我被问到
根据下面的 a、b 和 c 的定义,选择编译成功且计算结果为 true 的表达式。
int a = 1; 字符 b = 'a'; 布尔值 c = false;
所以我用了一个简单的
if (expression)
{System.out.println("True");}
else
{System.out.println("False");}
这样是吗?
c==a //false
!c || a //false
b >= a //true
c = a //false
a - b - 96 //false
a + b > 0 //true
c = true //true
a < b //true
这看起来还好吗?
I have been asked
Given the definitions of a, b and c below, select the expressions that compile successfully and evaluate to true.
int a = 1; char b = 'a'; boolean c = false;
So I used a simple
if (expression)
{System.out.println("True");}
else
{System.out.println("False");}
Is this right?
c==a //false
!c || a //false
b >= a //true
c = a //false
a - b - 96 //false
a + b > 0 //true
c = true //true
a < b //true
Does this look ok?
c==a
:无法编译,int
无法与boolean
进行比较。boolean ||不允许使用 int
b >= a
:编译,计算结果为true
c = a
:不编译,无法将int
值分配给boolean
a - b - 96
:编译,计算结果为 -192a + b > ; 0
:编译,计算结果为true
c = true
:编译,计算结果为true
(并赋值true
代码> 到c
)a
b
:编译,计算结果为true
c==a
: doesn't compile,int
can't be compared toboolean
.!c || a
: doesn't compile,boolean || int
isn't allowedb >= a
: compiles, evaluates totrue
c = a
: doesn't compile, can't assign anint
value to aboolean
a - b - 96
: compiles, evaluates to -192a + b > 0
: compiles, evaluates totrue
c = true
: compiles, evaluates totrue
(and assignstrue
toc
)a < b
: compiles, evaluates totrue