After we built the "Hello World" program earlier, this time we'll dissect that code to see how Java code is structured.
Make no mistake. Writing a program has a structure, the way writing a thesis does. And that structure has to be right so the adviser computer doesn't reject it.
Alright, straight in.
About Java Code
Generally, Java code files are marked by the .java extension.
Java code has to be written according to the rules of the Java language. Certain rules and syntax must be followed so a program can compile and run.
If there's a mistake in the syntax you type, the Java compiler throws an error automatically. An error means your program can't run ๐.
That's why you need to know the rules of Java code before typing any. Let's start with the structure.
Java Program Structure
Before going further, it helps to look again at the "Hello World" code we already built.
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World!");
}
}
Now we'll dissect that code piece by piece, following the structure of Java code.
1. Main Class Declaration
In Java, everything is an object. An object is usually marked with the class keyword.
In the "Hello World" example above, we create a main class declaration named HelloWorld
public class HelloWorld {
...
}
All code inside the curly braces ({...}) is called the class body. This class body can later hold variable declarations and functions.
In OOP terms, a variable declared inside a
classis called an attribute, while functions inside aclassare called methods. We'll cover this further in the OOP with Java section.
The rule is that a single .java file may only contain one main class. That main class is marked with the public keyword, and its name must match the name of the file you create.
For example, since we created a main class named HelloWorld, the file we create must carry the same name, HelloWorld.java.
2. Main Function Declaration
After the main class comes the main function, or main method.
โ Here we're talking about function as a noun, not a verb. If you run into the sentence "the function that functions to ...", that's normal, please don't get confused hehe..
A function is a program block holding the logic you want. That function can later be called to do something.
In the "Hello World" example above, the main function is this block:
public static void main(String args[]) {
...
}
Like a class declaration, a function block starts with an opening curly brace ({) and ends with a closing curly brace (}). All the code between those braces is the function body holding the logic you want.
The main method in Java is marked with the main identifier. That name is absolute, because Java uses it as the starting point of your code.
For now there's no need to know what public static void and String args[] mean, we'll cover them in later pieces.
3. Main Function Body
The last part of the main section of Java code is the function body.
As mentioned earlier, the function body is where you write the logic you want.
In this example, since we want to print the text "Hello World" on the console, we write the matching logic:
System.out.println("Hello World");
That code is an output command, which tells the computer to write "Hello World" on the console.
Inside this main function body is where you write your code. From input/output, variable declarations, control structures, and plenty more. We'll cover all of it in later pieces.
Java Code Writing Rules
Now that you know how Java code is structured, let's talk about its syntax rules.
These syntax rules are like the rules for writing text in a language. There are rules for capital letters, punctuation, and spelling that have to be met.
In Java, the general writing rules are:
- Naming (identifier) โ An identifier is the name we give a variable, function, class, and the like. Identifiers can use uppercase and lowercase letters (
A-Z,a-z), digits (0-9), and the$and_characters. They can't contain spaces, and can't start with a digit. - Semicolons (
;) โ In Java, a semicolon ends one command line. That includes variable declarations, function calls, and other statements. - Curly braces (
{...}) โ Curly braces mark a program block. A program block is one or more commands grouped together. You'll usually see these braces when creating functions and classes, and when calling control structures. - Periods (
.) โ In Java, a period accesses an attribute and method of an object. In the hello world example above, we use the period to access theoutattribute of theSystemobject, then access theprintlnmethod of theoutobject. Beyond that, the period also replaces the comma in decimal numbers โ so0.9, not0,9.
There are actually many more rules and punctuation marks you can use, like question marks, commas, and colons. We'll cover all of them later.
Next Up
Always remember this structure, because these are the rules you have to know to write Java code.
This is only the basic structure; later there'll be plenty of additional structures that help a lot when writing code. We'll cover them at other times.
Next we'll look at comments in the Java language.
Happy coding ๐