aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "instanceof" Watch "instanceof" New topic
Author

instanceof

Mark Henryson
Ranch Hand

Joined: Jul 11, 2005
Posts: 200
Can anyone explain this program, especially the instanceof thing in the program.



a. 1 2
b. 3 4
c. 1 3 4
d. 3
e. Compilation error.

The answer is 3 (d). Please explain how???
Stephen O'Kane
Greenhorn

Joined: Aug 17, 2005
Posts: 26
the first if statement is false, as al is an object of class D, which is not an instance of C. The part after && is not evaluated

the second if statment is false, the first part gives a value of first, but since & is used, the second part of the if condition is evaluated, which means a3 is an object of class C

the third if is true, as the first part (before |) is true, but the second part is also evaluated (a2 = new D()) again because it is a single |.

the fourth if is false, as a2 is not an instance of C (from third if construct), and class D does noot extend C

Sok
sameer inamdar
Ranch Hand

Joined: Jul 04, 2005
Posts: 40
what is difference betwee & and &&
| and || can you tell some details.


Dream!!! Set Vision !! Transform Vision to Mission by Actions on Deadlines!.
Stephen O'Kane
Greenhorn

Joined: Aug 17, 2005
Posts: 26
&& and || are known as short circuit expressions. using the example above

if((a1 = new D()) instanceof C && (a2 = new B()) instanceof A)

if the first part before && evaluates to false, then the second is not executed. whereas for

if(a2 instanceof B & (a3 = new C()) instanceof A)

it doesn't matter what the first part evaluates to, the second part must also be evaluated, and then the result of both expressions are used with &

the same goes for | and ||, except that if the first part of a || expression must be true so that the second part is not evaluated.

Sok
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
The operator && evaluates the first operand, if it is false does not evaluate the second operand. On the other hand & evaluates both operands whether the first one is false or not.

The operator || evaluates the first operand, if it is true does not evaluate the second operand. On the other hand | evaluates both operands whether the first one is true or not.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: instanceof
 
Similar Threads
Exam Question: I can't explain this downcasting
Dan Chisholm's Array Question
Please explain Garbage Collection
Multidimensional Arrays
instanceof confusion