← Todos os escritos

Java Basics

Java is a popular, versatile programming language used in many applications, from web development to mobile apps. It is known for its portability, security, and robustness.

Setting Up the Environment

Before you begin, make sure Java is configured on your computer. Download the Java Development Kit (JDK) from Oracle’s official website and follow the installation instructions for your operating system.

Your First Java Program

Let’s start with a simple example: the classic “Hello, World!” program. Open your code editor, create a file with the .java extension, and add this code:

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

This basic Java program prints “Hello, World!” to the console. To run it, compile the code into Java bytecode, then execute it. Open a terminal, go to the directory that contains the .java file, and run these commands:

javac HelloWorld.java
java HelloWorld

After you run the commands, you should see “Hello, World!” in the console.

Variables and Data Types

Java is a strongly typed programming language. This means each variable must have a specific data type. Here are some examples of variable declarations in Java:

int idade = 25;
double altura = 1.75;
char genero = 'M';
boolean estudante = true;
String nome = "João";

This example declares variables for a person’s age, height, gender, student status, and name. It uses several data types: int for integers, double for floating-point numbers, char for individual characters, boolean for true or false values, and String for sequences of characters.

Control Structures

Java provides several control structures for conditional and repeated actions. Here are examples of how to use them:

Conditionals: if and else

int idade = 18;

if (idade >= 18) {
    System.out.println("Você é maior de idade.");
} else {
    System.out.println("Você é menor de idade.");
}

Loops: for and while

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

int i = 0;
while (i < 10) {
    System.out.println(i);
    i++;
}

These examples use an if statement to check whether the age is at least 18 and display an appropriate message. They also use for and while loops to print a count from 0 to 9.

Functions and Methods

In Java, functions are called methods. They are blocks of code that you can run when needed. Here is an example of a simple method:

public class Calculadora {
    public static int somar(int a, int b) {
        return a + b;
    }
}

// Chamando o método somar
int resultado = Calculadora.somar(5, 3);
System.out.println("Resultado da soma: " + resultado);

This example creates a Calculadora class with a static method named somar. It accepts two arguments, a and b, and returns their sum. The code then calls somar with the values 5 and 3, stores the result in a variable, and prints it to the console.

Conclusion

This short article introduced Java and covered basic concepts such as environment setup, control structures, variables, data types, functions, and methods. Java is a powerful, versatile language. I hope this gives you a basic understanding to start learning it.

References