• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

instanceof

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is difference betwee & and &&
| and || can you tell some details.
 
Stephen O'Kane
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
&& 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
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic