4

I was intrigued that Scott Ambler in his book, Java Coding Standards, says, and I quote :

2.5.2 Place Constants on the Left Side of Comparisons

So he recommends to use

if ( 1 == something ) {…}    
if ( 0 = x ) { …}

instead of

if ( something == 1 ) {…}    
if ( x = 0 ) { …}  

OMG !!!

And he motivate this by saying that :

"Although they are both equivalent, at least on first inspection, the code on the left compiles and the code on the right does not."

As I'm aware (when I started programming Java, Java 14. was already in use), both of conditions will throw compiler error.

Starting from Ambler statement, I tried to search if Java syntax if ( x = 0 ); was ever compilable.

Can you help me out with this? I searched back different versions of JSR's and I did not find any change that could indicate that that piece of code was compiling on other java versions.

I compiled with a Jre7 compiler using target and source 1.2 and still raises compiler error. Unfortunately I don't have a Java 1.1 compiler: 9

My question is:

if(x = 0); Was compilable with older versions of Java compilers?

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Grandanat
  • 181
  • 2
  • 12
  • 2
    I'm confsed : Are you looking for this ?[Is there any difference between (null == var) and (var == null](http://stackoverflow.com/questions/18956526/is-there-any-difference-between-null-var-and-var-null/18956582#18956582) – Suresh Atta Oct 16 '13 at 07:41
  • 3
    in C it would be correct but not in java – Marco Forberg Oct 16 '13 at 07:44
  • 1
    No, i don't think that was ever valid in Java - Java had a clear distinction between `boolean` and integral types from the very beginning. So this is just 13 year old crap (happens when someone mentally copy and pastes from his C experience without adjusting for the new language) – Gyro Gearless Oct 16 '13 at 08:19
  • Yes, that's true, and more over, it not allows conversion from non-boolean to boolean or otherwise. But i wanted to find an explanation for Ambler statement, since is anyway, a heavy name in software engineering. It looks that he overlook this aspect. Anyway, i was negatively surprised by how many answers were out of scope. I would give u -1 to all if i could (except @GyroGearless) – Grandanat Oct 16 '13 at 11:01
  • 'if (x = true)' is valid but 'if (true = x)' is not. – sachin garg Dec 30 '13 at 10:54

2 Answers2

5

it is not compilable. if (x=true) however still is if x is boolean.

popfalushi
  • 1,332
  • 9
  • 15
5

This condition if ( x = 0 ) { …} if ( 0 = x ) { …} will never compile.It is because if accepts boolean type but x=0 are assignment operators

secondly 0=x is not right.0=x means that you are storing the value of x in 0 which can never be possible.

In this condition if ( something == 1 ) {…} instead of if ( 1 == something ) {…} is valid one and will work well with the present comparison with integers but for comparison between strings instead of == better use .equals() Please see this links to know difference between == and .eqauls() link1 link2

Community
  • 1
  • 1
SpringLearner
  • 13,738
  • 20
  • 78
  • 116