• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Expression evaluation

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone please explain me the order of evaluation the following expression uses to execute? I guess it would be easier just to put the extra parenthesis in place.

int i=1;
int j = 2;
if (i==1 || j==2){
System.out.println("true");
}

Thanks!
 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi -

A quick Google search for "Java operator precedence" took me straight to Sun's official documentation:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html

You'll note in the table that "equality" (==, !=) has higher precedence than "logical OR". So the expression will be evaluated:

(i == 1) || (j == 2 )
<= THIS WILL, OF COURSE, EVALUATE TO "TRUE"

One thing the Java page *doesn't* mention is that "&&" and "||" are "short circuit" operators - that is, the second expression will *not* be evaluated if the first one is true. This can often be a very important distinction.
Hope that helps .. PSM
[ February 11, 2005: Message edited by: Paul Santa Maria ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Filipe Pomar:
I guess it would be easier just to put the extra parenthesis in place.



Yes, I'd probably do exactly that.
 
Philip Pomario
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys!
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Santa Maria:

...
One thing the Java page *doesn't* mention is that "&&" and "||" are "short circuit" operators - that is, the second expression will *not* be evaluated if the first one is true. This can often be a very important distinction.
Hope that helps .. PSM

[ February 11, 2005: Message edited by: Paul Santa Maria ]


This statement is only true of the || operator. For the && operator, if the first operand evaluates to false, then the second expression is not evaluated.
 
Philip Pomario
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just passed the SCJP1.4 exam and wanted to post this appreciation note to thank everyone who helped me understand Java a little better. Without your help this personal achievement wouldn't be possible.
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic