Page 1 of 1
About Question enthuware.ocajp.i.v7.2.909 :
Posted: Sun Jul 08, 2018 5:51 am
by st.lisker
Hey. These expressions are meaningless. Why is this compiled? And what does it mean? Thanks in advance.
boolean b = false&true;
boolean c = true&&false;
boolean d = true|false;
boolean e = false||true;
System.out.println(""+b+c+d+e); //falsefalsetruetrue
Re: About Question enthuware.ocajp.i.v7.2.909 :
Posted: Sun Jul 08, 2018 7:48 am
by admin
Why do you think they are meaningless? These expressions are probably the simplest examples of how |, &, ||, and && work. If you haven't read how these operators work, you might want to check out the operators section of whichever book you are following. Or you can try this:
http://cs.techruto.com/2017/06/logical- ... -java.html
Re: About Question enthuware.ocajp.i.v7.2.909 :
Posted: Sun Jul 08, 2018 4:58 pm
by st.lisker
Thanks for the answer. I understand how the operators |, ||, &, && works. I don't understand what it means to be true-or-false. It must be either true or false. But not " boolean b = ( true-or-false ) ".. I do not see the point in this expression...
Re: About Question enthuware.ocajp.i.v7.2.909 :
Posted: Sun Jul 08, 2018 7:47 pm
by admin
You are assigning the value of the expression true|false to the variable b and the point is to test whether you know how these operators work. Every operand of | must be a boolean, therefore, it will always be the same kind of situation in every expression involving | (or other logical operator). whether you use a boolean variable or a boolean literal.
For example, if x and y are two boolean variables with values true and false, b = x | y will also resolve to b = true|false, which is same as the expression given in the question.
Re: About Question enthuware.ocajp.i.v7.2.909 :
Posted: Mon Jul 09, 2018 6:12 am
by st.lisker
For example, if x and y are two boolean variables with values true and false, b = x | y will also resolve to b = true|false,
So, in this case, b is a true or false?

Re: About Question enthuware.ocajp.i.v7.2.909 :
Posted: Mon Jul 09, 2018 6:49 am
by admin
Well, you need to try it out. Just do System.out.println(b); But this is such a simple expression that you should know the answer without printing it out, especially when you've written above that you understand how these operators work. If you don't, please go through the link I gave above.
Come to think about it, writing the answer here would have taken me just one word to type

Re: About Question enthuware.ocajp.i.v7.2.909 :
Posted: Mon May 25, 2020 11:48 am
by Sieusc
Hi, I'm having trouble understanding this type of declaration like st.lisker.
I've read the (awesome) book by Hanumath Deshmukh, and know about the short circuiting operators. i.e. the difference between && and & and || and |. I love the support that you guys give on this forum and I aplaud you for your knowledge and provided software, but I think the question that st.lisker is asking is pretty clear

.
Code: Select all
boolean b = true|false; // value true
boolean c = true||false; // value true
What the heck is this? How is this evaluated, in what order? I got the short circuiting theory covered, but regardless of the opthis is just something I've not seen before. I'm totally new to programming and was proud to (barely, lol) pass Test 1 in Enthuware after having done a couple of tests, but this is something I've never seen before (and as such, might be a very logical thing to others, but not to me/us.
Thanks in advance
UPDATE: Okay while I'm writing this I think I stumbled upon the answer. To clarify my question and provide full information, I also tried to compile
Code: Select all
boolean d = true&&false // false
boolean e = true&false // false
// I only included both the short and non-short circuiting operators for sake of completeness
So I guess that at the time of declaring the boolean variable, it also gives the result of the evaluation of the expression at the same time. I think what got me confused is the fact that other sorts of expressions are made by operator precedence, but this evaluation is done "all at once".
Re: About Question enthuware.ocajp.i.v7.2.909 :
Posted: Tue May 26, 2020 12:38 am
by admin
Since = has the lowest precedence our of all operators, true&&false will be evaluated first and its result i.e. false will be then be assigned to the variable d. That's pretty much it.
If you have trouble understanding the expression true&&false or true||false, you may want to read a bit about
boolean algebra and
truth tables (not required for the exam).
Re: About Question enthuware.ocajp.i.v7.2.909 :
Posted: Tue May 26, 2020 1:56 am
by Sieusc
Thank you, sounds very logical now. I will check them out