Page 1 of 1

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

Posted: Tue Mar 20, 2012 9:43 am
by ETS User
This question says that the '&' operand can have integral as well as boolean operands. What integral means in this case?

Thanks

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

Posted: Tue Mar 20, 2012 10:55 am
by admin
integral means numeric... 2 & 3. In this case, it will do a bit wise and. This should help.

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

Posted: Tue Mar 20, 2012 10:58 am
by Matheu'
admin wrote:integral means numeric... 2 & 3. In this case, it will do a bit wise and. This should help.
Thanks for the reply. These bitwise operators are in the exam too?

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

Posted: Tue Mar 20, 2012 11:00 am
by admin
You don't need to learn exactly how they work for the purpose of the exam. But you should know that they exist and what is the difference between && and &.

But outside the exam, every programmer should know how bit wise operations work.

HTH,
Paul.

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

Posted: Sun Nov 04, 2012 12:27 pm
by gliesian
Here are two of the proposed options:

--> & can have integral as well as boolean operands.
--> && can have integer as well as boolean operands.

Shouldn't you be consistent with the use of integral and/or integer?

Thanks,
Robert

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

Posted: Sun Nov 04, 2012 12:56 pm
by admin
gliesian wrote:Here are two of the proposed options:

--> & can have integral as well as boolean operands.
--> && can have integer as well as boolean operands.

Shouldn't you be consistent with the use of integral and/or integer?

Thanks,
Robert
No, both are different options. One means integral (which includes integer) and other means just integer.

HTH,
Paul.

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

Posted: Sun Mar 03, 2013 7:34 am
by Robyn
In this example, the boolean if works fine, but the integer if's do not compile

public final static void main(String[] args) {
int i = 5, x = 9;
boolean b = true, c = false;
if(6 & 8){ System.out.println("This does not compile"); }
if(i & x) { System.out.println("This does not compile either"); }
if(c & b){ System.out.println("This works fine"); }
}

How then is this used for integral types?

Thanks

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

Posted: Sun Mar 03, 2013 7:48 am
by admin
When you use & for integers, the result is an integer. So the expression is valid as such but not you can't use it for an if condition because an if condition expects a boolean.
You can do: int value = i & x; or value = 6 & 8;

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

Posted: Sat Mar 09, 2013 11:59 am
by Robyn
Oh right - yeah that makes sense. Thanks

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

Posted: Wed Jan 01, 2014 4:04 pm
by shareef.hiasat
admin wrote:When you use & for integers, the result is an integer. So the expression is valid as such but not you can't use it for an if condition because an if condition expects a boolean.
You can do: int value = i & x; or value = 6 & 8;
Thanks this is what i wanted :P

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

Posted: Tue Mar 10, 2015 4:10 pm
by dmcinnis1
Hi,

I am finding conflicting information about Booleans being allowed as operands for the & operator in Oracle Java books and online. In both my Java SE 7 and Java SE 8 books (both by ORACLE) that the & operator works on integral types only. In the book "Java A Beginner's Guide Sixth Edition", page 166 (Java SE 8) is the following (little found in the Java SE 7 book):

"The bitwise operators can be used on values of type long, int, short, char, or byte. Bitwise operators cannot be used on boolean, float, or double, or class types. They are called the bitwise operators because they are used to test, set, or shift the individual bits that make up a value."

Booleans are represented by true and false in Java. Can we assume that that these values are represented by 1 and 0, respectively as in C++, etc.? Exactly how is Java representing these Boolean values in memory, and how many bits are in these representation? 1, 8, 16 bits?

Thanks

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

Posted: Tue Mar 10, 2015 6:11 pm
by admin
In java, boolean is an independent type. It is not represented by 0 or 1. Its actual size is not specified in jls and is not important either.