Java and curly brackets

Paulo de Oliveira
2 min readApr 27, 2024

--

Consider the Java code above.

We have the main method, two local variables a and b, and a for-loop.

One very important aspect of Java is the notion of domain or scope. For example, in our case, if you think that a and b will have the same result, you are wrong.

The absence of curly brackets around the for-loop body will make the for-loop encompass only the next line. So, a will be incremented 3 times because i < 3; b will be incremented only once because it will be ignored by the for-loop.

If we want b to be equal to a, we have to use curly brackets to specify the scope of the for loop as shown below:

It’s because of these kinds of things that I love Java so much: in order to simply work, there is a necessity for logical and semantic explicitness for each context through appropriate syntax for each moment.

But I don’t want to get so philosophical…

--

--

Paulo de Oliveira

Ph.D., Software Developer, proficient in Java, Dataweave, Apex, SQL, and Python, interested in the Foundations of Constructive Mathematics and Buddhism.