About Question enthuware.ocajp.i.v7.2.909 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
st.lisker
Posts: 22
Joined: Sat Jun 30, 2018 6:11 am
Contact:

About Question enthuware.ocajp.i.v7.2.909 :

Post 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

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.909 :

Post 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
If you like our products and services, please help us by posting your review here.

st.lisker
Posts: 22
Joined: Sat Jun 30, 2018 6:11 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.909 :

Post 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...

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.909 :

Post 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.
If you like our products and services, please help us by posting your review here.

st.lisker
Posts: 22
Joined: Sat Jun 30, 2018 6:11 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.909 :

Post 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? :roll:

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.909 :

Post 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 :D
If you like our products and services, please help us by posting your review here.

Sieusc
Posts: 21
Joined: Mon Mar 02, 2020 3:38 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.909 :

Post 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 :P.

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 :joy:

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".

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.909 :

Post 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).
If you like our products and services, please help us by posting your review here.

Sieusc
Posts: 21
Joined: Mon Mar 02, 2020 3:38 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.909 :

Post by Sieusc »

Thank you, sounds very logical now. I will check them out

Post Reply

Who is online

Users browsing this forum: No registered users and 52 guests