• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
It means our mission is in jeapordy! Quick, read this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic