About Question enthuware.ocajp.i.v8.2.948 :
Moderator: admin
-
- Posts: 11
- Joined: Mon Jan 18, 2016 4:35 pm
- Contact:
About Question enthuware.ocajp.i.v8.2.948 :
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.
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
Yes, that is a logical argument. But I am afraid the correct reason can only be given by the language designers
Paul.
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Wed Mar 09, 2016 9:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
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
- Attachments
-
- Wrong correct answer
- Q23.JPG (132.38 KiB) Viewed 13123 times
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
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.
Therefore it will print 1 and 3.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sun Apr 30, 2017 9:37 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
Probably, claudiucls was referencing to the line number, not to the output strings. It is a little bit confusing imho.
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
Why does this code...
print true?
Code: Select all
boolean bool = false|true;
System.out.println(bool);
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
That is how "or" works on boolean operands. false or true is true.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Sun Feb 11, 2018 3:13 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
But shouldn't line 1 evaluate to: "false&true = false" right? So, how does 1 become a part of the output?
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
Because at line 1, it is calling method1("1"). The output is produced by the print statement in the method.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Wed May 09, 2018 12:48 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
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 ?
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
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?
so yes, you should assign to "bool" the result from the parenthesis later but how will that change the output which is already printed?
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Dec 21, 2018 4:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
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?
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?
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Thu Feb 21, 2019 7:37 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
Hi,
i am writing regarding bool = (bool2 & method1("1")); //1 being true. 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
i am writing regarding bool = (bool2 & method1("1")); //1 being true. 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
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
>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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Sun Mar 08, 2020 3:55 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
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.
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.
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
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?
& 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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Sun Mar 08, 2020 3:55 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
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.
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.
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
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".)
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".)
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Sun Sep 26, 2021 5:12 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
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
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
-
- Site Admin
- Posts: 10170
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.948 :
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.
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 3 guests