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.
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 Programming Language
In order to start writing code in Java, we need to install Java Development Kit (JDK) on our computer.
JDK includes:
- 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).
- Java Runtime Environment (JRE). It contains JVM and core java libraries.
- Tools for java development(archiver, docs generator, and etc).
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 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
- 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.
Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming
OS, File, and File System
Working with terminal
Welcome to Java Programming Language
Variables and Primitives in Java
Convert String to numeric data type
Input from the terminal in Java
Methods with Java
Java Math Operators and special operators
Conditional branching in Java
Switch statement in Java
Ternary operator in Java
Enum in Java
String class and its methods in Java
Loops in Java
Access modifiers in Java
Static keyword in Java
The final keyword in Java
Class and Object in Java
Object-Oriented Programming in Java
OOP: Encapsulation in Java
OOP: Inheritance in Java
OOP: Abstraction in Java
OOP: Polymorphism in Java
The method Overriding vs Overloading in Java
Array in Java
Data Structures with Java
Collection framework in Java
ArrayList in Java
Set in Java
Map in Java
Date and Time in Java
Exception in Java
How to work with files in Java
Design Patterns
Generics in Java
Multithreading in java
Annotations in Java
Reflection in Java
Reflection & Annotations - The Powerful Combination
Run terminal commands from Java
Lambda in Java
Unit Testing in Java
Big O Notation for coding interviews
Top Java coding interview questions for SDET