File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's the difference between & and &&?

 
André Asantos
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the difference between & and &&?

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For more details, see JLS - 15.23 Conditional-And Operator &&.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic