Java
In these StemKB tutorials, I will guide you through the fundamentals of the Java programming language, starting from the very basics, and teach you how to write Java programs.
What is Java? Java is a programming language. It is available for free on any computer and operating system (Windows, Linux, etc.). There are no costs or licenses required to write a program.
In Java, programs are structured using "classes".
A class serves as a template for creating objects, which are instances of that class. In essence, objects are the "products" generated from a class.
Within a class, there are components known as "members." The primary members are fields (or properties) and methods:
- Fields
Fields are variables that store data and are part of the class or objects created from that class. They represent the "state" of an object or class. - Methods
Methods are operations or instructions that can be performed on fields to modify their state. Methods define the behavior of the class, such as assigning values to fields, performing calculations, or managing the program's control flow.
Each member of the class is enclosed within curly braces { } following the class name.
Here is a basic example of a program that prints the phrase "Hello, world!".
class MiaClasse {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
This program creates a class named `MiaClasse` with a `main` method, which acts as the entry point for the program.
The main method in Java is the starting point of program execution.
What is the main method? The main method is a special method that must be defined in every Java application to make it executable. When a Java class is executed, the Java runtime automatically looks for the main method and begins executing the code within it. If the main method is absent, the program cannot be executed properly. It is public, meaning it is accessible to everyone; static, meaning it does not require an object to be created; and void, meaning it does not return any values.
Following the name of the main method, there are parentheses () that can hold parameters.
Parameters are data that the method can receive from outside when it is called.
In the case of the main method, the only parameter is an array (a collection) of String objects named args.
What is args? It is an array, which is an ordered list of elements, with each element being a string of text. This array contains the "arguments" that the program can receive when it is launched from the command line. Arrays in Java are denoted by square brackets [], so String[] means "an array of strings".
The main method declaration is divided into two parts:
- The header
This consists of the method name followed by the parameters, and it is preceded by modifiers such as 'public', 'static', and 'void' that define its accessibility and return type. For the main() method, the header is "public static void main(String[] args)". - The body
This is the block of code enclosed within curly braces. In this example, it consists of a single statement "System.out.println("Hello, world!");" which outputs a string to the screen. Remember that each statement in Java must end with a semicolon ";".
In this example, when the program is executed, Java runs the main() method and prints "Hello, world!" on the screen.
Hello, world!
How to Write, Compile, and Run Java Code
First, write your program code using a text editor, which is a program where you can write code (for example, Windows Notepad or a specialized code editor).
class MiaClasse {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Next, save the code you have written to a file. In this example, the file is named "MiaClasse.java".
After saving the file, you need to compile the code. Compilation converts the source code (what you have written) into "bytecode," a form of "machine language" that Java can execute.
To compile, use the `javac` command followed by the filename in your computer’s terminal or command prompt.
javac MiaClasse.java
This command creates another file with the same name but in bytecode format.
Once compiled, you can run the program using the `java` command.
java MiaClasse
This command starts the execution of the `main` method in the "MiaClasse" class, which in our example will print "Hello, world!" to the screen.
Hello, world!
In summary, you write the code and save it in a source file, then compile it to convert it into a format the computer can understand, and finally, run it to see the result, which in this case is a simple text message.
Key Characteristics of the main Method
The main() method is the starting point of a class. When the program is executed, this method is the first to run.
In a Java program, the main() method has the following characteristics:
- public
The main method is declared as "public" to allow anyone, including the Java system, to execute it. - static
This means that the method belongs to the class itself rather than to a specific instance, allowing it to be executed without creating an object of the class. - void
This indicates that the main method does not return any value, so no data type is returned after execution.
Java’s Key Features
The Java language offers the following key features:
- It is a high-level language because it is closer to human language.
- It is an object-oriented language because it uses classes.
- It is a portable language because a Java program runs on a virtual machine, enabling it to execute on any operating system (Windows, Linux, etc.).
Which Editor to Use
If you are just starting with Java programming, I recommend using a free online editor. This way, you don’t need to install anything on your PC.
Once you are more familiar with programming, you can install the Java compiler on your computer and use any editor of your choice.
A professional open-source editor is Eclipse, but even Windows Notepad will suffice.