This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Order of evaluation with logical operators

 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:...so some sort of evaluation is needed to make sure that the type of rvalue is correct.


Dang! Now I'm mixing compile-time with runtime....

Don't worry. I've basically got it now (I think ). Thanks guys.

Winston
 
Sheriff
Posts: 7416
504
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote: This code snippet prints 51. Because it's evaluated as 6 (++i), 5 (--i) * 9 (--j) and finally 6 + 45.


This example is extremely good. Merits a cow. Actually two.

It simply reveals, that operators precedence does the grouping of operands, but does not define the evaluation order of whole expression. Really liked it.

 
author
Posts: 23853
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Winston Gutkowski wrote:...so some sort of evaluation is needed to make sure that the type of rvalue is correct.


Dang! Now I'm mixing compile-time with runtime....



Yeah, it *is* a pain. Discussions (of this type) regarding Precedence, Associativity, and Evaluation Order, should be in the context of the compiler (and compile time)... but "evaluation" is incredibly difficult to not use the context of runtime.

I do it too, I am looking out for it...

Henry
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Winston Gutkowski wrote:...so some sort of evaluation is needed to make sure that the type of rvalue is correct.


Dang! Now I'm mixing compile-time with runtime....


Glad to see you noticed that one yourself. That saves me a reply

And honestly I think you are overcomplicating this a little bit, while it is so easy and simple:
1/ at compile time the compiler executes a (static) code analysis to verify if you met all syntax rules (e.g. both operands of the operators && and || must evaluate to a boolean or Boolean value).
2/ at runtime the right operand might not be executed if and only if the result is already known after evaluating the left operand (e.g. false && ... will always evaluate to false; true || ... will always evaluate to true).

And with the ternary operator, the behavior is similar:
1/ the compiler will check if the condition evaluates to a boolean (or Boolean) value and also verifies if the true- and false-expressions are compatible with the type of the variable to which the result is assigned.
2/ at runtime the condition is evaluated (executed) and based on its result only the true- or the false-expression is evaluated (executed).
This is illustrated in this code snippetOutput: 10 10 10 (and no NullPointerException is thrown)

But these will not compile, because the types of the true- and false-expressions are not compatible with the variable type

Kind regards,
Roel
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:And honestly I think you are overcomplicating this a little bit, while it is so easy and simple:


For a self-professed "human compiler" maybe; but plainly not for the rest of us mortals.

In my case, I had it wrong for more than a dozen years; but it's never caused me any problems because I simply don't write expressions that rely on the "effects" of sub-expressions (beyond '.' chaining of course).

And I still say that the JLS only adds to the confusion, because its narrative is far from clear.

Winston
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:For a self-professed "human compiler" maybe; but plainly not for the rest of us mortals.


When you are active in this forum for a few years and having reviewed some OCA study guides, you'll automatically become a "human compiler"

Winston Gutkowski wrote:And I still say that the JLS only adds to the confusion, because its narrative is far from clear.


If you see which kind of language is used to write the JLS, I think adding confusion is its main purpose
 
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think some of the replies are a bit confusing so here is my take.

First of all this :

Can be rewritten as this:

It's only an assignment that returns the value assigned. Here is another simpler example :

int i;
System.out.println( (i = 13) ); // All we do is assigning 13 to the value i and returning it.
// Thus that can be rewritten as:
System.out.println(13);

With that out of the way you can see that this :

System.out.println( (false && true | true ); // Since bitwise operations have precedence this is always false ie => (false) && (true | true) => false && true
 
Liutauras Vilda
Sheriff
Posts: 7416
504
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cedric Bosch wrote:First of all this :

Can be rewritten as this:

I disagree.

Execute this example and you'll see:
Output:
While you're stating output should be:
 
Liutauras Vilda
Sheriff
Posts: 7416
504
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cedric Bosch wrote:System.out.println( (false && true | true ); // Since bitwise operations have precedence this is always false ie => (false) && (true | true) => false && true

That explanation is also incorrect, even though the output is false.
 
Liutauras Vilda
Sheriff
Posts: 7416
504
Mac OS X VI Editor BSD Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Expressions in Java being evaluated from left to right always.
&& operator is so called short-circuit. That means, no matter what is the operand on the right hand side - if left side can define that whole expression going to be false, that means, right operands not getting evaluated at all.

If expression were:
the expression behind the scenes would be treated as a visual example below.

Going to be:
Rather than:
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to read the whole thread because I'm confused right now. In the mean time can someone tell me what's a cow ? I see every one is saying "here have a cow".
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cedric Bosch wrote:With that out of the way you can see that this :

System.out.println( (false && true | true ); // Since bitwise operations have precedence this is always false ie => (false) && (true | true) => false && true


You are completely incorrect!

First of all, you can't rewriteasbecause both statements are not equivalent at all. These are two completely different statements.

Secondly your understanding of how the evaluation of the following statement occurs, is completely wrong as wellIn Java, expression are always evaluated from left to right (except for the assignment operator which is evaluated from right to left). That means that (a=false) is the first expression to be evaluated. It obviously evaluates to false. Because the left-hand side of the short-circuit && operator evaluates to false, the right-hand side ((b=true) | (c=true)) will not be evaluated at all.
And that is clearly illustrated in this code snippetOutput: res:false a:false b:false c:false

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cedric Bosch wrote:In the mean time can someone tell me what's a cow ? I see every one is saying "here have a cow".


Have a look at the Ranch Cows article to learn everything you need to know about cows You can compare it a bit with a Facebook thumbup on steroids
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:Secondly your understanding of how the evaluation of the following statement occurs, is completely wrong as well



Oooh, I see my error. Yes it does help and thanks for pointing me in the right direction, I was inverting the meanings of & and &&. So one operator let's it go through and two doesn't.

So this :



can be thought as this :



And this :



can be thought as thought as :



I hope that's correct ?

And my first statement would have been correct if there was only one & (I suppose) :



Output :

true
a: false
b: true
c: true
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cedric Bosch wrote:I hope that's correct ?

And my first statement would have been correct if (I suppose) :


Yes, both are correct!

So one little additional character can make a huge difference, as illustrated in these code snippetsAnd then invoke both methods with a null value and see what happens

Hope it helps!
Kind regards,
Roel
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will throw an error on the second one :p. NPE
I'd give you a cow if I could (the link you gave me say it's only one cow per day for rancher and I'm only rancher hand:)).
 
Liutauras Vilda
Sheriff
Posts: 7416
504
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Cedric
You just got your first cow for a nicely solved problem well done
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:
You just got your first cow for a nicely solved problem well done



Awesome, I'm part of the pack now !
 
Liar, liar, pants on fire! refreshing plug:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!