• 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

Exceptions

 
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can anybody please explain the idea?
Thanks.

PS: The 27. question from http://home-1.tiscali.nl/~bmc88/java/q_a/index.html#qa
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answer is a), b), c).

a) and b) because the | always evaluates both operands so if s is null s.length() will throw NullPointerException

c) because if s == to null then (s != null) will evaluate to false and consequently s.length() will throw NullPointerException.

Not d) because if s == null then || operator will short cirquit and not evaluate the second operand and if s != null then s.length() will not throw an exception.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main point in this question is to understand the difference between the '|' operator and the '||' operator. Once you understand the difference, the solution to the problem is plain sailing.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
|| is a short circuit operator that is if the first operation comes to true it will not go to the second one so a,b,c will give exceptions
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
'|' is a binary bitwise or operator. Both the operands should be either boolean or integrals. Operands cannot be boolean & integers at the same time.
In the options given by u left operand is boolean while right operand is integer. This will give compiler error.
Same applies for the '||' operator.



Ramdas.
 
Why fit in when you were born to stand out? - Seuss. 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