Understanding coding structures
When we write code in any language, we know that it must be organized in very specific ways. You are familiar with this concept from whichever language or languages you already know, so all we must do is examine how they are coded in Java. We begin with code blocks.
Code blocks
Every language has a structure for organizing the lines of code you write, and this is commonly called a block. The Python language uses indenting to define a block, and Pascal uses the begin
and end
keywords. Java uses opening ({
) and closing (}
) braces, as do C, C++, C#, and JavaScript.
In Java, all classes and methods must have an opening and closing brace. Blocks may be nested, as we will see when we examine iteration and decisions later in this section. Blocks also serve another purpose when it comes to variables. This is called the variable’s scope. Let’s look at this in practice in an example:
public class Blocks { 2 3 ...