☕ Java

Introduction to Java: history, features and applications

What is it and why?

Imagine you are giving commands to a robot. You say: "Turn left, walk 10 steps, raise your hand." A programming language is the same way of giving commands, only not to a robot, but to a computer. Java is one of the most popular languages for this.

Let's draw an analogy. Imagine you are building a house from a LEGO set. You have bricks of different colors and sizes. Java is such a "construction set" for creating programs. It has ready-made parts (they are called classes and methods) from which you can assemble anything: from a "Snake" game to a note-taking app on your phone.

Now imagine you are learning a foreign language, like English. First you learn words (variables), then sentence structure rules (syntax), and then you can write a whole essay (program). Java is like "English" for computers. Only instead of letters, special words are used: public, class, static, void.

Why Java and not Python? Python is like a spoken language: simple and understandable. Java is like a literary language: more strict and formal. In Java you need to write more words, but the computer will understand exactly what you want. For example, in Python you can write print("Hello"), and that's it. In Java, you need to write a whole "wrapper" for this:

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

Looks more complicated? Yes. But such a program will work on any computer — whether on Windows, Mac, or Linux. This is called "write once, run anywhere". That's why Java is used in ATMs, in Minecraft games, in Android apps, and even in spacecraft!

Another important feature: Java is an object-oriented language. Sounds scary, but it actually just means you can create "blueprints" for things. For example, you can make a "Dog" blueprint, and then create specific dogs from it: Rex, Buddy, Max. Each will have its own name, age, fur color, but they will all be able to bark and wag their tail. In programming, this is very convenient.

Where is Java used? The list is huge:

  • Web applications — sites like Amazon, eBay, LinkedIn are written in Java.
  • Mobile applications — all Android apps (games, social networks, banks) are written in Java.
  • Games — the famous game Minecraft is written in Java.
  • Banking systems — ATMs, terminals, online banking.
  • Scientific programs — data processing, weather modeling.
  • Robots and smart devices — Java is used in the "Internet of Things" (refrigerators, kettles, light bulbs).

When you learn to program in Java, you will be able to create your own programs, games, and applications. And it's not as difficult as it seems. Let's look at some examples!

Code examples with explanation

We will analyze each example in detail. Don't be afraid if something is unclear — I will explain every line. First, remember the main rule: in Java, each command ends with a semicolon (;). It's like a period at the end of a sentence.

Example 1: "Hello, World!" (the simplest program)

// This is a comment. The computer ignores it, it is for people.
// Comments start with two forward slashes.

public class HelloWorld {          // Declare a class named HelloWorld
    public static void main(String[] args) {  // Main method, where the program starts
        // Print text to the screen
        System.out.println("Hello, World!");   // Print command
    }  // Close the method
}  // Close the class

Line-by-line breakdown:

  • public class HelloWorld — we create a "blueprint" (class) named HelloWorld. The class name must match the file name (the file should be called HelloWorld.java).
  • public static void main(String[] args) — this is the main door through which the program starts working. Without this line, the program won't run. Remember this magic phrase — it is always the same.
  • System.out.println("Hello, World!"); — a command that prints text to the console (a black window with white letters). Inside the quotes, we write what we want to say.
  • Curly braces {} — they show where a block of code starts and ends. Imagine it's a box: everything inside belongs to the class or method.

How to run it? You need to install Java on your computer (download JDK), save this code in a file HelloWorld.java, open the command line, type javac HelloWorld.java (compilation — translation into machine code), and then java HelloWorld (run). If everything is done correctly, you will see:

Hello, World!

Example 2: Math — adding numbers

public class MathExample {
    public static void main(String[] args) {
        // Declare variables — these are like boxes that store numbers
        int a = 5;           // int — integer. Put 5 into box 'a'
        int b = 3;           // Put 3 into box 'b'
        int sum = a + b;     // Add the contents of the boxes and put into a new box 'sum'
        
        // Print the result
        System.out.println("Sum: " + sum);  // The + sign concatenates text and number
        
        // You can do different operations
        int difference = a - b;  // Subtraction
        int product = a * b;     // Multiplication (asterisk, not cross sign!)