سؤال

This is quite confusing.
Question: Give the value of each of these expressions.

x && !x

Would this be true or false?

هل كانت مفيدة؟

المحلول

true && !true => false
false && !false => false

the only other option is

Boolean x = null;
x && !x => NullPointerException.

نصائح أخرى

I don't know if I understood your question, but I don't see how that expression would ever evaluate to true. If x was to be set to True the !x would evaluate to False, and vice versa. The logical AND operation will only evaluate to True if both parameters are True. Since this will never be the case, the expression will always be False.

The short-circuiting only occurs if the left side is False. Then there is no need to evaluate the right side. The expression will never short-circuit if the left side is True.

Always false except x is null

&& Called Logical AND operator. If both the operands are true, then the condition becomes true.

Why don't you test it out yourself?

I don't see how short-circuiting is applicable . To check if its true, both sides of the AND operator must be true and there are no shortcuts in the event that the entire exp is true.

In the theory may be some issue with multithreading, but only in the theory

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top