Basic Java Syntax

The Java programming language is widely used due to its portability, efficiency and security. It is an object-oriented language, which means that the focus is on creating objects that contain both data and methods. Before we dive into more advanced concepts, it is essential to understand the basic syntax of the language. This guide will cover the main elements that form the basis of Java programming.

Structure of a Java Program

Every Java program is composed of at least one class and one main method, known as main. This method is the entry point of the program and is where execution begins.


public class MyFirstProgram {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}
    

The public keyword makes the class accessible to other classes, and static allows the method to be called without needing to instantiate the class. The String[] args array is used to receive arguments from the command line.

Variables and Data Types

Variables are containers for storing data. In Java, each variable must have a specified data type, which defines the size and memory layout of the variable. Data types in Java are divided into two categories: primitives and references.

Primitive Types

  • int: For integers. Example: int age = 30;
  • double: For decimal numbers. Example: double salary = 4500.50;
  • boolean: For true or false values. Example: boolean estaRaining = true;
  • char: For single characters. Example: char letter = 'A';
  • byte: Integer type of very small size. Example: byte age = 30;
  • short: Small-sized integer type. Example: short year = 2021;
  • long: Large-sized integer type. Example: long populationEarth = 7800000000L;
  • float: For single precision decimal numbers. Example: float weight = 65.0f;

Types of Reference

Reference types include classes, interfaces, and arrays. When you create an object of a class, you are creating a reference type. For example:


String message = "Welcome to the course!";
    

The String type is a class that represents sequences of characters and is an example of a reference type.

Operators

Operators are special symbols that perform specific operations on one, two, or three operands and return a result. Operators in Java can be classified into different types:

Arithmetic Operators

Mainly used to perform mathematical operations.

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • % (module)

Comparison Operators

Used to compare two values.

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)

Logical Operators

Used to perform logical operations (AND, OR and NOT).

  • && (logical AND)
  • || (logical OR)
  • ! (NOT logical)

Flow Control

To control the flow of program execution, we use decision structures and loops. Decision structures include if, else, and switch. Loops include for, while, and do-while.

Decision Structures


if (condition) {
    // Block of code to be executed if the condition is true
} else {
    // Block of code to be executed if the condition is false
}
    

switch (expression) {
    case value1:
        // Block of code to be executed if expression == value1break;
    case value2:
        // Block of code to be executed if expression == value2
        break;
    default:
        // Block of code to be executed if expression is not equal to any of the cases above
}
    

Loops

Loops are used to execute a block of code multiple times.


for (initialization; condition; increment) {
    // Block of code to be executed on each repetition
}
    

while (condition) {
    // Block of code to be executed while the condition is true
}
    

of {
    // Block of code to be executed at least once and then as long as the condition is true
} while (condition);
    

Arrays

Arrays are containers that store a fixed number of values ​​of the same type. The syntax for declaring an array in Java is:


type[] ArrayName = new type[size];
    

For example, an array of integers can be declared as:


int[] numbers = new int[5];
    

The elements of an array are accessed through indices, which start at 0.

Conclusion

Java's basic syntax covers the structure of a program, data types, operators, flow control, and arrays. Understanding these concepts is fundamental for anyone who wants to learn Java. As you become more familiar with the language, you can explore more advanced topics such as inheritance, polymorphism, interfaces, and exceptions. Continuous practice and building real projects are the best ways to deepen your Java knowledge and skills.

Now answer the exercise about the content:

_What is the purpose of the `main` method in a Java program?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Basic Java Syntax: Primitive Data Types

Next page of the Free Ebook:

5Basic Java Syntax: Primitive Data Types

6 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text