• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What's the difference between & and &&?

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the difference between & and &&?

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
&& (and | vs || is the same) is the shortcut operator. As soon as you can determine the results, it stops. This is useful for things like

if (myObject != null && myObject.doIt())

if the object is null, we know the expression is false, so we don't bother calling myObject.doIt(), which is a good thing, since that would throw a null pointer exception.
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot for helping....

if any guy also needs that if speaks Portuguese check it out the link http://www.guj.com.br/posts/list/58646.java I found out too in detail...
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For more details, see JLS - 15.23 Conditional-And Operator &&.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing is that && and || have lower precedences than & ^ (exclusive-or) and |. The single-stroke operators & ^ and | are overloaded; they are applied to integer numbers and secondarily also applied to Boolean values. Boolean values include un-boxed java.lang.Boolean objects and boolean primitives.

Note the Java™ Tutorials refer to && as conditional and on one page, which is correct, and logical and, which may be incorrect, on another page.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:... Note the Java™ Tutorials refer to && as conditional and on one page, which is correct, and logical and, which may be incorrect, on another page.


You're right. The JLS defines a single '&' as "bitwise" or "logical" (JLS - 15.22). The double '&&' is "conditional" (JLS - 15.23).
 
reply
    Bookmark Topic Watch Topic
  • New Topic