It's typed this way because a lot of people come to Java from the C or C++ worlds and as a result still have bad habits ingrained into them by C's and C++'s sharp edges.
In C there are two problems that interact poorly: assignments are expressions and boolean tests can be any of a broad set of types. Thus is is possible in C to have:
...
int a;
a = 5;
if (a = 6)
{
printf("This should not happen!\n");
}
...
The problem, of course, is that you've assigned 6
to a
instead of checking of a
was equal to 6
. As a result of a lot of brain damage caused by tracking down these kinds of subtle bugs, a coding convention was adopted that put the rvalues on the left side of the comparison so if you accidentally assigned instead of compared the compiler would have a conniption and puke all over your screen:
...
int a;
a = 5;
if (6 = a) /* THIS CANNOT COMPILE */
{
printf("This should not happen!\n");
}
...
Fast forward a few decades and a programming language later. Java looks an awful lot like C or C++ at first glance so people assume that it works the same way. It doesn't. While assignment is STILL an expression in Java, boolean checks must be of type boolean
. The expression a = 6
will have an integer type and thus the compiler will puke. You can therefore, in Java, cheerfully use the coding approach that more closely approximates the way you'd talk, but people have difficulty grasping this and instead import their C and C++ habits.