• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

evaluation order of expressions

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Every operand of an operator appears to be fully evaluated before any part of the operation itself is performed.
Therefore, shouldn�t the compiler fully evaluate x = y and report this error instead?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every operand of an operator appears to be fully evaluated before any part of the operation itself is performed.
That requirement governs evaluation order during execution.
Therefore, shouldn�t the compiler fully evaluate x = y and report this error instead?
At compile time, the compiler is still understanding what the expression does. If there is more than one error in an expression (or in code in general) then the compiler may report as many or as few errors as it feels like, in any order, as long as it reports at least one error. With multiple errors we'd like to see a nice complete list all at once, but that's often hard for the compiler to do - one error influences interpretation of the rest of the code, and the other errors may be only loosely related to what the programmer really meant because the first error got the compiler off on a different understanding entirely. In general, the compiler seems to report errors in the order they appear in the source, meaning leftmost before rightmost. I know when I'm looking at compiler errors, I often just fix the very first one and recompile; it takes too much time to figure out what the later messages are talking about, and they're often just descended from the initial error anyway. I'd rather fix one and then let the compiler tell me if the others are still real errors or not. If the compiler reports leftmost errors first, with some consistency, it make it easier for me to find the (first) problem.
In this case, the compiler isn't actually evaluating the expressions; it's first checking to see if the expressions are even valid. It may do that in any order.
[ July 31, 2003: Message edited by: Jim Yingst ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jim for the nice detailed explanation.
[ July 31, 2003: Message edited by: Marlene Miller ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried as below ..still compiler error is there ...can any body explain...
class Test {
public static void main(String[] args)
{
double x = 343;
double y = 343;
boolean b = (x = y);
}
}
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi tanmay, you probably mean
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tanmay,
boolean b = (x = y); This is an extract from the Sierra&Bates mock exam. They are testing whether you notice that (x = y) really should be (x == y).
When I worked the problem, I did not notice this. Instead, I noticed that a value of type double is being assigned to a variable of type long.
So there are two errors in this example.
 
Our first order of business must be this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic