• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to be the compiler  RSS feed

 
Vishnu Khera
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vishnu Khera
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Vishnu Khera
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishnu Khera
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!