Page 1 of 1

About Question enthuware.ocajp.i.v8.2.948 :

Posted: Mon Jan 18, 2016 4:43 pm
by mtrikannad
Wonder why bitwise operators on booleans dont short circuit. For example for false & true - it is a certainty that when you come across false the result is going to be false ( Just like that for a && ). Why does Java still look at the other one. I can see why it would need to be done for a integer, it has to bit wise and each one of the bits.

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Mon Jan 18, 2016 9:45 pm
by admin
Yes, that is a logical argument. But I am afraid the correct reason can only be given by the language designers :)
Paul.

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Sun Mar 13, 2016 11:58 am
by claudiucls
Ok, the explanation says that 3 and 4 are correct but it shows that 1 and 3 for the correct answer? I attached a print screen of the result ? Something is wrong

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Sun Mar 13, 2016 8:37 pm
by admin
No, the explanation doesn't say that 3, 4 are correct. It says method1 is not called for 2 and 4.
Therefore it will print 1 and 3.

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Sun Apr 30, 2017 10:28 am
by Nijscoman
Probably, claudiucls was referencing to the line number, not to the output strings. It is a little bit confusing imho.

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Wed Sep 27, 2017 6:07 am
by Sergey
Why does this code...

Code: Select all

        boolean bool = false|true;
        System.out.println(bool);
print true?

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Wed Sep 27, 2017 6:26 am
by admin
That is how "or" works on boolean operands. false or true is true.

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Tue Mar 06, 2018 12:46 pm
by Meghana
But shouldn't line 1 evaluate to: "false&true = false" right? So, how does 1 become a part of the output?

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Tue Mar 06, 2018 10:18 pm
by admin
Because at line 1, it is calling method1("1"). The output is produced by the print statement in the method.

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Sat May 26, 2018 3:18 am
by mihhay
admin wrote:
Tue Mar 06, 2018 10:18 pm
Because at line 1, it is calling method1("1"). The output is produced by the print statement in the method.
I really don't understand why/how the output of " bool = (bool2 & method1("1")); //1 " can be given just by the method ...
The evaluation of "bool2" & "method1" shoud be done first, right ? This mean "false and true = false" , right ?
And after that we shoud assign to "bool" the result from the parenthesis

Can you please elaborate ?

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Mon May 28, 2018 8:11 pm
by admin
Can you be bit more clear about your querry? As the explanation says, the print stataement is in the call to method(1), so that output is coming from there irrespective of how the boolean expression is evaluated.

so yes, you should assign to "bool" the result from the parenthesis later but how will that change the output which is already printed?

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Fri Dec 21, 2018 6:41 pm
by juliusacar
For this part:

bool = (bool1 || method1("4")); //4

bool1 = true;

public static boolean method1(String str){
System.out.println(str);
return true;

Shouldn't it output as true as well since true || true = true?

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Fri Dec 21, 2018 10:20 pm
by admin
Please read the question and its explanation carefully. It is not the value of the expression bool1 || method1("4") that is being printed in the question.

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Thu Feb 21, 2019 7:50 am
by paulita
Hi,
i am writing regarding bool = (bool2 & method1("1")); //1 being true.
OperatorBitwise.JPG
OperatorBitwise.JPG (40.86 KiB) Viewed 8399 times
according to the snapshot from a book(see picture attached), the & operator is only true when both operands are true. Since bool2 is false and method1("1") is true results false.
Then i also don't understand why is true.
Thank you for your answer in advance!
Best,
Paula

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Thu Feb 21, 2019 9:21 am
by admin
>Since bool2 is false and method1("1") is true results false.
Yes, that is correct. The result of bool = (bool2 & method1("1")); will be false.

>Then i also don't understand why is true.
It is not true. Nowhere does it say that the result of the expression is true.

Please go through the code and the given explanation carefully and let me know if you still have any doubt.

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Mon Mar 09, 2020 11:04 am
by nerdchieftain
My textbook says that boolean type does not support bitwise operations.
Yet, we have `bool = (bool2 & method1("1"));`
It seems Java may be trying real hard to allow C++ style syntax here.

So is my book wrong, or is bool2 being implicitly cast to an integral type? If there were implicit casting, I could see them asking a question about that that gets tricky. Like does `true | 2` equal 0 or false? And if it's 0, is it a short or int or maybe even a byte?
I had thought boolean were stored as 8 bit bytes, but maybe that's a C++ thing and not a Java specification requirement.

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Mon Mar 09, 2020 11:27 am
by admin
Your textbook is correct. boolean type does not support bitwise operations. The code that you tried is not a bitwise operation either, although it does use an operator (&) that is used for bitwise operations!

& and |, when used with integral types perform bitwise operation but when used on boolean types, perform non-short circuiting logical operations.

Not sure which textbook you are using but this one by Hanumant Deshmukh explains this in detail (on Pg 123).

For other expressions that you have mentioned, what happened when you tried to compile/run them?

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Mon Mar 09, 2020 2:57 pm
by nerdchieftain
Yeah Oracle Java 11 compiler revealed that

for boolean a, boolean b, and int k
a & b returns a boolean
also,
a & k is invalid

there is no implicit conversion from other types to boolean

This does work (the right operand of & is a boolean)
a & k % 2 == 1

This is just bizarre and seems to defy good reasoning. I would have chosen to use a byte to implement a boolean. I am sure they had their reasons. I am using Hanumant Deshmukh text you linked. It is Kindle, so I don't have the exact page number, but I am pretty sure I was just not in the correct section. The author mentions 4 motivations behind the choices made when defining the Java language. Principle two is to reduce the propensity for bugs, and I think this in fact does the opposite. Or at least, leads to more compiler caught errors.

Well, now I am just complaining. Moving on.

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Mon Mar 09, 2020 7:55 pm
by admin
What do you find bizzare in this a & k % 2 == 1 ?

They may actually have used a byte internally to implement a boolean. It is just not reflected out to the application developer as that would amount to leaking an implementation detail. You identified reason 2 correctly for this. Using integers to implement conditionals (in c/c++) has indeed been proven to be a source of unnecessary confusion and a source of bugs.

On Kindle, | and & are explained in Section 6.1.1 Overview of Operators (Location 3283). (Just search for phrase "Non-short".)

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Sun Sep 26, 2021 5:20 pm
by Syams123
Can someone please help me understand. I am new to java just started learning

As per my understanding ;

boolean bool1 = true;
boolean bool2 = false;
boolean bool = false;
bool = (bool2 & method1("1")); //1 False & False --> Prints False
bool = (bool2 && method1("2")); //2 False && False --> Prints False
bool = (bool1 | method1("3")); //3 True | True --> Prints True
bool = (bool1 || method1("4")); //4 True || True --> Prints True
}

So answer should be 3 and 4 right?
and whats is the use of int variable here?


Thanks

Re: About Question enthuware.ocajp.i.v8.2.948 :

Posted: Sun Oct 10, 2021 1:23 am
by admin
1. Did you read the explanation ? It explains exactly why 4 is not right.
You may read more about short-circuit operators in Java.

2. There is no use of the int variable in the code. It is just there to throw you off.