Understanding Java

The if statement is often translated as “if”, but “if and only if” is what it really means.

To be able to use a language you need to actually understand it. This may seem obvious, but books on Java usually don’t have much on understanding the language. When you learn French you will want to be able to translate from and to French. 

Java isn’t English

A book should explain to you the meaning of different statements, such as if, switch, while etc. But they usually do not translate those to English. However, I have seen many blogs that try to do just that when they explain simple statements. I like the idea, but the translations are usually not precise enough.

In this post I use the if statement as an example and I explain how to properly translate such code from Java to English.

Example Code is misleading

It’s nearly impossible to find a perfect example, because if it’s realistic it probably is also too complex. And if it’s very simple then it’s not realistic and a beginner doesn’t know the context of such a simple code snippet.

The Example

So here’s the Java code we will translate to English:

if (weatherForecast.isRaining()) { takeUmbrella();}

English isn’t Java

Someone who’s just learning Java will see that this is somewhat similar to English and translate it as such:

If it is raining, I take the umbrella with me.

That’s not completely wrong and it seems so right because that’s what you would actually say. Any other human will understand exactly what you mean. But when you write Java code you do not only communicate with other programmers who might read your code. It will be compiled to byte code and later to machine code, so the CPU understands it. And the CPU doesn’t know what rain and umbrellas are.

False Friends and Biconditionals


The most important mistake here is the translation of “if”. It doesn’t mean the same in English. It could be translated to “only if” or “if and only if” as it is often used in logic and related fields. The JVM only executes the code if the statement is true. It will not execute the code if the forecast reports any other weather.

Words that look or sound similar, but have different meanings are called false friends. That’s why “iff” (two f’s), or a symbol, such as ↔ or ⇔, is used, to make the distinction clear.

The Translation must be complete

A statement isn’t the same as a logical operator. But there is a connection and we can use “if and only if” for a better translation. Here are two translations that are incomplete on their own, but complete as a logical conjunction:

  • The JVM must execute the method if it is raining.
  • The JVM must execute the method only if it is raining.

Both are correct translations, but only when put together they are complete. The first one only states that the JVM can not skip the method when it is raining. It could still execute it when it’s sunny. The second one only states that it mustn’t execute the method when it’s not raining. It could still skip it when it’s raining. Together we get the correct translation:

  • The JVM must execute the method if and only if it is raining.

Another translation would be “Only on the condition that…”, “Only in the case of…”.

Bad Example

Programming in a language is mostly about giving orders. There is also meta programming and some other things that are more declarative, but mostly you see code like in the example. However, this example isn’t good programming. What if it isn’t raining now, but will rain later? What if you already have an umbrella? Is there only one umbrella? Or do I need to find one first and which one would I take if there are more than one? What if there is no umbrella available? The method takeUmbrella(), might take care of that, but then the name would be somewhat misleading.

Proper Translation

I would alter the example a bit:

if (weatherForecast.isRaining(LocalDate.now()) 
    && !umbrellas.isEmpty() 
    && !me.isUmbrellaEquipped()) {

Here’s how I would translate it:

If and only if it is raining today, there are umbrellas available and I do not already have one on me, I take the first available umbrella.

Other Mistranslations

  • Assignment operator (=): Often translated in a way that suggests things that do not actually happen. It doesn’t alter the old value, or the object that was referenced before the assignment. It doesn’t copy any objects.
  • Variables: Often translated suggesting that the variable is the value or object. But it only holds a value (and that value can be a reference). The variable is just a name used to identify a field, a parameter, or a local value.
  • Parameters:  Translations often suggest that object typed expressions are passed by reference. But they are passed as a copy of the value, which is a reference (see here and here).
  • And (&&) / Or (||): These are actually conditional operators. if (a || b) should be translated as “iff a, or iff not a then iff b“. Sounds strange, but that’s what it really means. The JVM will only evaluate b if a was false.
  • final: I suggest not using the term constant when talking about Java (see here).
  • char: A char isn’t exactly a “character”. Some code points need two chars (see here).


There’s no point in making the example so simple that it doesn’t look like real code. If you want to learn programming, you need realistic examples and you need a book that properly explains what the keywords mean. In my example it still has to be explained that the variables weatherForecast, umbrellas and me need to be declared and initialized. Another alternative would be to have only one umbrella and implement it as a singleton. So you can say “the umbrella” as in the first translation. In both cases I assume that a person (me) can only have up to one umbrella.

Leave a Reply

Your email address will not be published. Required fields are marked *