Welcome to Java Programming Language

Beknazar
5 min readFeb 19, 2021

There are two types of programming languages. The low-level programming languages and high-level.

The low-level languages are very close to instructions that are directly understandable by a machine. Sometimes, we refer to them as an assembly language or machine code.

In another hand, high-level programming languages have a syntax that is understandable by humans. We know, machines can only understand binary code (0 and 1) so in order to run our program, we will need to first translate it into binary code. The process of translating our source code written in the high-level language to byte code is called a compilation. A compilation is done by a special program — a compiler. After, a compiled code can be executed by a computer.

Compiler converts source code into machine code

Java is a high-level programming language. We will write source code with relatively understandable syntax, compile it, and then we will execute our compiled code.

Java source code

Java Programming Language

In order to start writing code in Java, we need to install Java Development Kit (JDK) on our computer.

JDK includes:

  1. Compiler. A Compiler for java will compile a source code into a java-byte code. Java-byte code is not quite machine code. Then, java-byte code will be executed by Java Virtual Machine(JVM).
  2. Java Runtime Environment (JRE). It contains JVM and core java libraries.
  3. Tools for java development(archiver, docs generator, and etc).
JDK

As we mentioned above, the java compiler converts source code into java-byte code. Java-byte code is not yet machine code, though, it is close to it. Then, JVM will convert java-byte code into machine code and execute it.

Why java has one more step and doesn’t convert directly to the machine code by a compiler?

Write once, run anywhere

Java is a platform-independent programming language. Let’s try to understand what does it mean and in which way it is platform-independent.

To start, different hardware and Operating Systems require different machine codes. If we will come back to the first schema where we have

SOURCE CODE -> COMPILER -> MACHINE CODE

We can see compiler generates machine code. It means for different machines, we need different compilers. Compilers are complex programs and expensive to build.

Now, let’s take a look at a schema for Java

SOURCE CODE -> COMPILER -> JAVA-BYTE CODE -> JVM -> MACHINE CODE

We can see here, the compiler generates java-byte code and Java Virtual Machine(JVM) takes java-byte code and generates machine code. In Java, there is only one compiler for any machine but different JVMs. JVMs are less complex programs to build than compilers. This approach lets java adopt new environments faster.

In my opinion, we cannot say Java is %100 platform-independent. We will still need to install different JRE for different machines to execute our java program. For example, for Mac, you will need a different one than for Windows.

First Program

Java First Program
  • Java file name should have the exact same name as a public class name.
  • Java files have .java extension.
  • javac <pathToJavaFileName>.java will compile java source code and produce a file with .class extension with java-byte code.
  • java <pathToJavaFileName> will run the compiled file.

Breaking down the HelloWorld program

Breaking down the HelloWorld program
  • class is a blueprint for objects but for now, it’s useful to think that we just need a class to write a java program.
  • the main method is a special method that actually runs our program. Everything in the main method will be executed when we run our program from top to bottom.
  • java file can have only one public class, but many nonpublic classes.

All about print statements

  • System.out.println(); will print in a new line(actually, it will add a new line after printing).
  • System.out.print(); will print everything in one line.
  • We can use \ to escape special characters in java.
  • \n — new line.
  • \t — tab space.

--

--