[Logo]
Forums Register Login
Trying to be the compiler

Howdy ranchers!

Trying my best to 'be the compiler' (and sitting in a meditation posture)

The code in discussion is:



I have had a bit of Java (-0.0==0.0) is true but a little more surprised with the above output. Correct me if I'm wrong Parentheses, ( ), can be used to override precedence and associativity.

If we look at the statement (1) '=' has lowest precedence. The expression on the right hand side would be looked into first. I feel the compiler ( ) has to evaluate RHS to a boolean value, so that the program runs!!! Okay no problem, but what about 'precedence rules' - why look at '||' first when we have 'j+=2' sitting there in parentheses. And yes I do understand to the left of '||' its true this means mr. compiler never looks at what's on the right
(2 likes)
The boolean expression on the right hand side is not evaluated, because the logical OR operator || implies short-circuit evaluation. The expression on the left hand side of the OR operator evaluates to 'true', so evaluating the expression on the right hand side of the OR operator is deemed unnecessary (parentheses or not) and is subsequently skipped. Try changing the logical OR operator to |, and see how that impacts the result.
(2 likes)
 
Vishnu Khera wrote:I have had a bit of Java (-0.0==0.0) is true but a little more surprised with the above output. Correct me if I'm wrong Parentheses, ( ), can be used to override precedence and associativity.

Correct.

If we look at the statement (1) '=' has lowest precedence. The expression on the right hand side would be looked into first. I feel the compiler ( ) has to evaluate RHS to a boolean value, so that the program runs!!! Okay no problem, but what about 'precedence rules' - why look at '||' first when we have 'j+=2' sitting there in parentheses. And yes I do understand to the left of '||' its true this means mr. compiler never looks at what's on the right

And in that last sentence you've just answered your own question. If the compiler sees:
true || whatever
it doesn't matter a darn what the 'whatever' is, or how many brackets it contains, because it will never get evaluated.

HIH

Winston
(2 likes)
 
Vishnu Khera wrote:
I have had a bit of Java (-0.0==0.0) is true but a little more surprised with the above output. Correct me if I'm wrong Parentheses, ( ), can be used to override precedence and associativity.

If we look at the statement (1) '=' has lowest precedence. The expression on the right hand side would be looked into first. I feel the compiler ( ) has to evaluate RHS to a boolean value, so that the program runs!!! Okay no problem, but what about 'precedence rules' - why look at '||' first when we have 'j+=2' sitting there in parentheses. And yes I do understand to the left of '||' its true this means mr. compiler never looks at what's on the right


Keep in mind that precedence, associativity, and order of evaluation are three different things. Just because you, actually most people, do the expression from the inner most parens out, doesn't mean that Java will do it that way.

In Java, evaluation order is done from left to right....

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7

Henry
Jelle, Winston, Henry thank you so much for your answers

Henry thank you for your answer. The explanation below is satisfactory

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7

15.7.2. Evaluate Operands before Operation

The Java programming language guarantees that every operand of an operator (except the conditional operators &&, ||, and ? : ) appears to be fully evaluated before any part of the operation itself is performed.

Java document says ......... <boolean expression> || whatever

Thank you Winston I understand 'whatever' would never be evaluated
(1 like)
 
Vishnu Khera wrote:

Trying my best to 'be the compiler' (and sitting in a meditation posture)


Henry Wong wrote:
Keep in mind that precedence, associativity, and order of evaluation are three different things. Just because you, actually most people, do the expression from the inner most parens out, doesn't mean that Java will do it that way.

In Java, evaluation order is done from left to right....

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7

Henry


Do not try and bend the evaluation order. That's impossible. Instead.. only try to realize the truth
What truth?
That there is no evaluation order

Yess Jayesh!!! I'm a fan toooo...

(Oracle's place)
Priestess: Hello, Neo. You're right on time.... Make yourself at home, Morpheus. Neo, come with me.... These are the other potentials, you can wait here.

Spoon boy: Do not try and bend the spoon. That's impossible. Instead only try to realize the truth.
Neo: What truth?
Spoon boy: There is no spoon.
Neo: There is no spoon?
Spoon boy: Then you'll see that it is not the spoon that bends, it is only yourself.

Priestess: The Oracle will see you now.

Oracle: I know you're Neo. Be right with you.
Neo: You're the Oracle?
Oracle: Bingo. Not quite what you were expecting, right? Almost done. Smell good, don't they?
Neo: Yeah.
(1 like)
 
Jayesh A Lalwani wrote:Do not try and bend the evaluation order. That's impossible. Instead.. only try to realize the truth
What truth?
That there is no evaluation order

Actually, in this case, there is; as Vishnu has conclusively proved.

Winston
(1 like)
 
Jayesh A Lalwani wrote:Instead.. only try to realize the truth
What truth?
That there is no evaluation order

Ah, you're a functional programmer, then?
(1 like)
 
Winston Gutkowski wrote:
Jayesh A Lalwani wrote:Do not try and bend the evaluation order. That's impossible. Instead.. only try to realize the truth
What truth?
That there is no evaluation order

Actually, in this case, there is; as Vishnu has conclusively proved.

Winston


What I meant to say is that the rules of Java are what Java has defined the rules to be, just like in The Matrix the Machine defined it's own rules. Once you understand the rules you can do stuff that works differently than how you expect them to behave. The way boolean expressions are evaluated are the equivalent of spoon bending in the movie. They break convention.
 
What I meant to say is that the rules of Java are what Java has defined the rules to be, just like in The Matrix the Machine defined it's own rules. Once you understand the rules you can do stuff that works differently than how you expect them to behave. The way boolean expressions are evaluated are the equivalent of spoon bending in the movie. They break convention.


Well said Jayesh, well said All for a good cause
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 17, 2017 16:20:31.