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

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

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »

This question says that the '&' operand can have integral as well as boolean operands. What integral means in this case?

Thanks

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

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

Post by admin »

integral means numeric... 2 & 3. In this case, it will do a bit wise and. This should help.

Matheu'

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

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

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

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

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

gliesian
Posts: 9
Joined: Sun Oct 28, 2012 8:17 pm
Contact:

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

Post 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

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

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

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

Robyn

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

Post 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

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

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

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

Robyn

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

Post by Robyn »

Oh right - yeah that makes sense. Thanks

shareef.hiasat
Posts: 20
Joined: Thu Dec 19, 2013 8:22 am
Contact:

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

Post 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

dmcinnis1
Posts: 16
Joined: Wed Feb 25, 2015 8:52 pm
Contact:

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

Post 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

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

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

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

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests