← Back to course

Free trial: Java для начинающих

Complete a couple of real lessons from this course — no signup required.

01

Lesson 1: Variables, Data Types & Operators

Java is a simple, object-oriented, and highly efficient programming language. One of its main goals is to make learning to code easier and faster. To write a working Java program, it's enough to remember the core keywords, and your code will run correctly.

In this lesson we'll cover the basic structure of a Java program, variables, data types, reading input from the console, and the main math operators.

Your first Java program

Let's look at the first simple example of a Java program:

Main.java
public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a + b);
    }
}
Output:30
JAVA
Output

When this code runs, we create two numbers: a and b. Variable a equals 10, and variable b equals 20. Then we print their sum:

a + b = 10 + 20 = 30

The line System.out.println(a + b); prints the result to the console.

Structure of a Java program

If we break down the program above, we see it starts with the keywords: public class Main.

This means we're creating a class called Main. The class name must match the source file's name exactly.

If the file is called Main.java, the class must be called Main. If the file is called Hello.java, the class must be called Hello.

For example, if you named the file Hello.java, the code would look like this:

Hello.java
public class Hello {
    public static void main(String[] args) {
        ...
    }
}

Next we see curly braces { }. They open and close a block of code. In Java, it's very important to make sure every opening brace is closed.

Inside the class we create the program's main method: public static void main(String[] args). This is exactly the method where a Java program starts running.

Basic structure:

public class Main {
    public static void main(String[] args) {

        // The program's logic goes here

    }
}

We'll cover methods a bit later. For now, the main thing to remember is: every Java program starts running from the main method.

Printing text to the console

To print data to the console, we use the command System.out.println().

Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello BITLAB");
    }
}
Output:Hello BITLAB
JAVA
Output

Note that we end every statement with a semicolon: ;. It means the statement is complete.

02

Lesson 2: Conditional Statements

We use conditions all the time in everyday life. For example, we wake up and start having breakfast. If the kettle has boiled, we turn it off. If not yet, we wait until it does. We start getting dressed and head to work. If we're running late, we order a taxi; otherwise, we walk there in about 25 minutes. If we're hungry after work, we go have dinner. If there's no food at home, we order some. You could keep writing out the algorithm of our life's conditions forever like this.

In short, we always act and make decisions depending on the situation — that is, on input data. Programming works exactly the same way: you can build conditional actions based on variables and their values.

if statements

For example, I enter a number. If that number is greater than 10, I print the text "YES":

Main.java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.println("Insert number:");
        int a = in.nextInt();

        if (a > 10) {
            System.out.println("YES");
        }

        System.out.println("Program ended");
    }
}
Input:20
Output:YES Program ended
Input:5
Output:Program ended
JAVA
Output

How does this code work?

First, we create a whole number int a, whose value we read from the console. Then we set up a condition check using the regular comparison operator >, meaning "greater than".

The keyword if means "if". If a > 10, the program enters the block of code inside the curly braces. If the condition is false, the program skips that block and keeps going.

As you've noticed, checking a condition always produces one of two results: true, or false.

Conditions with multiple checks

You can also combine several comparisons using logical operators. For example, we enter a number, and if that number is between 10 and 20, the condition is true. Logically, a number between 10 and 20 must be less than 20 and, at the same time, greater than 10. If either of those conditions is false, the whole condition fails.

Main.java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.println("Insert number:");
        int a = in.nextInt();

        if (a > 10 && a < 20) {
            System.out.println("YES");
        }

        System.out.println("Program ended");
    }
}
JAVA
Output

As you can see in the example, we use the double ampersand — &&. This symbol means logical "AND".

If I enter 15, it falls between 10 and 20, since 15 is less than 20 and also greater than 10. If I enter 25 instead, the condition "greater than 10" gives true, but "less than 20" gives false, since 25 isn't less than 20. So if I enter 25, I skip the condition's block.

Here is a table of the logical operators and what they do:

Operator Description Example
&& Logical AND 10 > 5 && 20 <= 40 is TRUE
|| Logical OR 5 == 3 || 30 < 20 is FALSE
! Logical NOT 5 != 5 is FALSE
03

Lesson 3: Loops: while & for

In programming, you very often run into situations where the same action needs to happen several times. For example: printing the same text many times, processing several numbers, going through a list of students, repeating an action for as long as a condition stays true.

Java uses loops for tasks like these. A loop means you don't write the same code over and over — you describe the repetition once.

Why do we need loops?

Picture this: you want to run a process that repeats periodically up to a certain point. For example, printing "Hello BITLAB" 10 times.

Without loops, it would look like this:

Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello BITLAB");
        System.out.println("Hello BITLAB");
        System.out.println("Hello BITLAB");
        System.out.println("Hello BITLAB");
        System.out.println("Hello BITLAB");
        System.out.println("Hello BITLAB");
        System.out.println("Hello BITLAB");
        System.out.println("Hello BITLAB");
        System.out.println("Hello BITLAB");
        System.out.println("Hello BITLAB");
    }
}
Output:Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB
JAVA
Output

This code works, but it's inconvenient. Now imagine we wanted to print that same text 100 times. A bit much, right?

In that case you'd have to write System.out.println("Hello BITLAB"); 100 times. And if we ever wanted to change the text, we'd have to change it in a lot of places.

The core idea behind loops: write the repeating action once, and tell Java how many times, or until when, to repeat it.

With loops, everything changes. The code becomes shorter, clearer, and easier to modify.

The while loop

We can shorten our "print Hello BITLAB 10 times" task like this:

Main.java
public class Main {
    public static void main(String[] args) {
        int i = 0;

        while (i < 10) {
            System.out.println("Hello BITLAB");
            i = i + 1;
        }
    }
}
Output:Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB Hello BITLAB
JAVA
Output

The idea is that a while loop checks a condition just like a regular if.

If the condition is true, the block of code runs. If it's false, the block is skipped. The key difference with while is that after the block runs, it goes right back to checking the condition again.

A while loop reads as: while the condition is true — run this block of code.

So this while loop keeps running for as long as the condition i < 10 stays true.

How while works, step by step

Let's break the loop down. In our example there's a variable i. It's there to count the number of repetitions.

  • Create the variable int i = 0;
  • Check the condition i < 10
  • If it's true, print "Hello BITLAB"
  • Increase i by 1
  • Go back to checking the condition again

At each step, the value of i increases:

i = 0 → i = 1 → i = 2 → i = 3 → ... → i = 10

Once i reaches 10, the condition i < 10 becomes false, because 10 is no longer less than 10. The loop then ends.

04

Lesson 4: Arrays

In programming, you very often need to store not one value, but several values of the same type at once. For example, at BITLAB ACADEMY we might have a group of students, and each student has an exam score.

If there are few students, you could create several separate variables. But if there are many students, that approach quickly becomes inconvenient. Java uses arrays to solve this kind of problem.

The problem without an array

Let's look at a case where you want to store the exam results for 5 students in a group and print their sum and average. Normally you could create 5 variables of type double in your program and work with them.

Main.java
public class Main {
    public static void main(String[] args) {
        double sc1 = 100;
        double sc2 = 90;
        double sc3 = 95;
        double sc4 = 80;
        double sc5 = 75;

        double total = sc1 + sc2 + sc3 + sc4 + sc5;
        double average = total / 5;

        System.out.println("Total score : " + total);
        System.out.println("Average score : " + average);
    }
}
Output:Total score : 440.0 Average score : 88.0
JAVA
Output

As you can see from the example, we created 5 double variables for each student's score, and calculated the sum and the average.

But this is very inefficient, because this kind of approach is static. If someone suddenly tells me I need to add 5 more students, it would be silly to keep creating separate variables for them.

And what if the group has not 5 students, but 100 students? Creating variables sc1, sc2, sc3, and so on by hand would be very inconvenient.

To solve this kind of problem, we need to store the students' scores in a single structure. And that's where arrays come to mind.

What is an array?

An array is a data type, or data structure, in the form of a set of components — that is, array elements — placed one right after another in memory.

Access to individual array elements happens through indexing, meaning a reference to the array along with the number, or index, of the needed element.

In Java, an array is convenient for working with a large amount of same-type data. For example, if all the values are student grades, we can store them in a single array of type double.

In short, if a variable is a one-seat motorcycle, then an array is a bus, where you can fit a bunch of variables — that is, passengers.

Going back to our example, let's imagine we created an array with our students' scores:

double scores[] = {100, 90, 95, 80, 75};

Now all 5 results live not in separate variables, but inside one array called scores.

What does an array look like inside?

Every array element has its own number, called an index. The most important rule: in Java, array indices start at 0.

  • scores[0] = 100
  • scores[1] = 90
  • scores[2] = 95
  • scores[3] = 80
  • scores[4] = 75

So if the array has 5 elements, the indices aren't from 1 to 5, but from 0 to 4.

The first element of the array is scores[0]. The last element of a 5-element array is scores[4].

If you try to access scores[5], the program will throw an error, because there's no element with that index in the array.