The virtual machine and the first command - 1

"Hello, Amigo! My name is Rishi. I'm going to help you learn Java."

"I'm all ears!"

"A program is a set (list) of commands. First, you execute the first command, then the second, then the third and so on. Once all the commands are executed, the program is finished."

"What kinds of commands are there?"

"Commands depend on what is executing them. On the types of commands the actor knows (and understands)."

"You can give a command to a dog: 'Sit!', 'Bark!'; to a cat: 'Shoo!'; to a human: 'Freeze, or I’ll shoot!'; or to a robot: 'Work! Work, roboscum!'"

"What else?" Amigo was finally starting to have fun.

"Programs written in Java are executed by the Java virtual machine (JVM). The JVM is a special program that knows how to execute programs written in Java."

"The list of its commands is quite extensive. For example, this command could be used to display 'Robots are friends to humans' on the screen."

Here's a super simple command:
System.out.println("Robots are friends to humans");
The virtual machine and the first command - 3

"O_O"

"Rather than starting with commands, we’ll begin with a couple of simple principles."

"Knowing a couple of principles can replace knowledge of many facts."

"Here’s the first principle."

"In the Java programming language, each command is written on its own line. A semicolon must be placed at the end of a command."

"Suppose we want to display 'Humans and robots are friends forever' on the screen three times. This is how it would look:"

The program is made up of three commands:
System.out.println("Humans and robots are friends forever");
System.out.println("Humans and robots are friends forever");
System.out.println("Humans and robots are friends forever");

"How about a small task then?"

"The second principle."

"A program can’t consist of nothing but commands."

"Imagine a room in an apartment. A room can’t exist on its own. It’s part of some apartment. An apartment also can’t exist on its own. It’s part of some building."

"On the other hand, we can say that the building is divided into apartments and an apartment is divided into rooms."

"Everything is clear so far."

"A command is like a room. In the Java programming language, a command can't exist on its own. It's part of a function (in Java, 'functions' are also called 'methods'). A method is part of a class. In other words, a class is divided into methods and methods are divided into commands."

"So a class is an apartment building, a function/method is an apartment, and a command is a room. Did I get that right?"

"Yes, that's absolutely correct."

Amigo looked at Rishi in awe. This human was explaining to him the basics of programming using the divine Java language! And he, Amigo, just understood (had guessed all by himself!) that programs consist of classes, classes consist of methods, and methods consist of commands!

Amigo still didn't know why he needed it, but he was certain that this knowledge would make him the most powerful robot on the planet.

Meanwhile, Rishi went on:

"Programs in Java consist of classes. There might be tens of thousands of classes. A minimal program is one class. For each class, a separate file is created. The name of the file matches the name of the class."

"Suppose you decide to create a class that describes a home. You’ll need to create a Home class that will be saved in the file Home.java."

"If you want to describe a cat in the program, then you’ll have to create a file Cat.java and declare the Cat class in it, etc."

"The files contain code (text) written in the Java programing language. Usually a class’s code consists of the 'class name' and 'class body'. The class body is written in curly brackets. This is how the Home class (file Home.java) should look:"

public class Home
{
    

Class body



}

"I understand so far."

"Great. Let’s go on then. The class body may contain variables (also known as data) and methods ('functions')."

public class Home
{
    Variable A


    Variable Z


    
Method 1



    
Method N


}

"Would you please give me an example?"

"An example? Of course!"

public class Home
{
    int a;
    int b;

    public static void main(String[] args)
    {
        System.out.print("1");
    }

    public static double pi()
    {
        return 3.14;
    }
}

"Are int a and int b variables, and main and pi methods?"

"Yep."

"Can classes exist without variables?"

"Yes."

"And without methods?"

"Yes. But a minimal program must contain at least one class that must include at least one method/function to get the program running. This method must be named 'main'. A minimal program looks like this:"

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

"I can see the Home class here. I can see the 'main' method, but where are the commands?"

"A minimal program doesn't have any commands. That’s why it's called 'minimal'."

"I see."

"The class that starts the program can have any name, but the 'main' method used to start the program must always look the same:"

public class Home
{
   //Unchangeable part
   public static void main(String[] args) 
   {
       

Code for the method


  
   }
}

"I think I understand everything. At least, it seems so right now."

"Brilliant. Let’s go further and write a few lines of code, then