BEST SITE FOR WEB DEVELOPERS
Java programming language. Lessons for beginners from hyperskill

Ua

Write, compile, and run


Theory

Even if you are not an experienced programmer, you have probably heard something about Java. It is not only a programming language but also a widely used software platform. In this topic, you will get some essential ideas about the Java platform and learn how to write and run your programs in Java environment.

§1. Writing a program

As a developer, the first thing you do while creating a program is writing the source code in a plain text file and saving it with an extension corresponding to the programming language you've chosen (.java for the Java language, .kt for Kotlin, and so on). A single program consists of one or more such files, which contain instructions specifying what the program does. The source code must follow the syntax rules of the respective language and be easy to read and understand.


§2. Compilation

After the code is written, you need to make the computer run the program. As computers don't understand the source code, it needs to be translated into a computer-comprehensible format. That's where a special program called a compiler comes in handy. The code obtained after compilation is called native code or low-level code. Each computer platform uses different low-level commands, just like people around the world speak different languages. It creates an additional challenge to use a program on different devices.

In the world of Java, a compiler (the javac tool for Java or the kotlinc tool for Kotlin) translates source code into an intermediate representation known as Java bytecode stored in files with a .class extension. Computers can't read bytecode without translation, but a system called the Java Virtual Machine (JVM) can execute it.


§3. Running a program

The Java Virtual Machine (JVM) is an application that represents a virtual computer according to the JVM specification document. It executes the compiled Java bytecode and translates it into low-level commands, which the computer understands. Each platform has its own version of the JVM, but since all JVMs match the same specification, your program will behave identically on different devices.

One of the main concepts of the Java Platform is "write once, run anywhere". It means that a program can run on various devices as long as they have a JVM installed. This concept is also frequently called platform independence or portability.

It's important to remember: the code input into the JVM is platform-independent, while the output code is platform-dependent.


Running a program

A platform-independent program in the world of Java

If the JVM is installed on the computer, you can run a compiled JVM program using the java tool. It will open a file with the .class extension to launch the program from this file. The tool is the same for all JVM languages.

The picture below briefly summarizes the work cycle of a JVM program.

JVM program

§4. JVM languages

The Java Platform allows using more than one programming language to create programs. This is achieved by the design of the JVM: it doesn't know anything about any particular programming language. It only understands Java bytecode. If the tools for a programming language can generate bytecode, programs written in this language can be executed on the JVM. Such languages are often called JVM languages. They include Java itself, Kotlin, Scala, Groovy, Clojure, and others. So, to create programs in the world of Java, you can choose the most convenient language of your choice.

Nowadays, you can find tools to generate Java bytecode for almost any programming language, which means that there's hardly any language that is not a JVM language.

JVM languages

Different programming languages can be used to write programs in the world of Java


§5. Conclusion

  • Developers write program source code in text files with appropriate extensions.
  • Compilers translate source code into Java bytecode stored in .class files.
  • The Java Virtual Machine executes bytecode, giving low-level commands to the computer.
  • All JVMs are defined by the JVM specification, thus providing platform independence for programs.

You can also view Lesson on hyperskill.org.


Practical tasks and answers

Tasks and answer options are given. The correct option is highlighted in blue color.

№1. MyHelloWorldApplication.class

Question: Given a file MyHelloWorldApplication with the extension .class. What does this file contain?

Select one option from the list:

  • Machine code
  • Java source code
  • JavaScript
  • Common Intermediate Language
  • Bytecode ✔

№2. A system that executes Java programs

Question: Enter the abbreviation for the system that executes Java programs. Hint: the abbreviation consists of three letters.

Enter a short text: JVM ✔

Explanation. The abbreviation for the system that executes Java programs is JVM (Java Virtual Machine).


№3. Document

Question: In the world of Java, there is a document called "The JVM specification". What does it describe?

Select one option from the list:

  • How to make a programming language a JVM language: a set of syntax rules and compilation that any programming language must support to be a JVM language.
  • The Java programming language, including syntax, execution, and tools.
  • A list of JVM languages to make everyone familiar with all possible options.
  • An abstract computer and how it should work to make programs portable. ✔

Explanation. "The JVM specification" describes an abstract computer and how it should work to make programs portable.


№4. Bytecode

Question: Which tool generates bytecode?

Select one option from the list:

  • JVM language
  • JVM
  • Compiler ✔
  • JVM specification

Explanation. The Compiler is the tool that generates bytecode.


№5. JVM languages

Question: Which of the following programming languages can run on the JVM?

Select one or more options from the list:

  • Kotlin ✔
  • Scala ✔
  • Groovy ✔
  • Java ✔

Explanation. All these programming languages can run on JVM


№6. Platform independence

Question: Select a correct statement about platform independence in the Java world:

Select one option from the list:

  • Programs are directly dependent on the computer's hardware and the operating system.
  • Programs need to have a special syntax to reach platform independence.
  • Programs are executed on the Java Virtual Machine if the operating system is not Linux.
  • Programs are executed on the Java Virtual Machine to reach platform independence. ✔

Explanation. The correct statement about platform independence in the Java world is: Programs are executed on the Java Virtual Machine to reach platform independence.


№7. Statements

Question: Select all correct statements.

Select one or more options from the list:

  • The JVM understands bytecode. ✔
  • A computer understands source code.
  • To run a compiled Java program, a JVM must be installed on the computer. ✔
  • The JVM provides platform independence for Java programs. ✔

№8. What is the source code translated into?

Question: A compiler of a JVM language, for example, of Kotlin, translates source code into ...

Select one option from the list:

  • User code
  • Bitcode
  • Bytecode ✔
  • Bitcoins
  • Machine code

Explanation. A compiler of a JVM language, for example, of Kotlin, translates source code into Bytecode.


№9. Generating bytecode

Question: As you know, the world of Java uses bytecode as a foundation stone. From the options below, which are valid ways to create bytecode?

Select one or more options from the list:

  • Implement a program in Java and use JVM to generate bytecode
  • Write code in a JVM language and use its compiler to generate bytecode ✔
  • Run your program on at least three different computers and combine their outputs to receive bytecode ("write once, run anywhere" concept)

Explanation. The following are valid ways to create bytecode: Implement a program in Java and use JVM to generate bytecode; Write code in a JVM language and use its compiler to generate bytecode (ChatGPT response).

But the hyperskill site accepts only one correct option (highlighted in blue). Source code (.java) -> Compiler -> bytecode -> JVM -> Machine code. JVM is not a compiler. There is only one correct option.


№10. Purpose of bytecode

Question: What is bytecode needed for?

Select one option from the list:

  • It helps humans write and understand programs
  • It's more compact than the original code of the program, so programmers use it to read the program faster
  • It protects the source code of programs
  • It makes programs written in a JVM language platform-independent ✔
  • It allows storing a program as a set of bytes on a computer

Explanation. Bytecode makes programs written in a JVM language platform-independent.