Welcome to Java programming let’s dive deep into the Java programming world and see its use cases and real-world applications
Before directly jumping into Java let’s first understand why computers need a programming language. We humans generally communicate with others using languages such as English, Spanish, Hindi, etc. But when we try to communicate with a computer using our human language it won’t understand what we want to say. Computers only understand machine language. Machine language is nothing but just 0 and 1, so to communicate with computers many great people created many languages such as Java, python, c, etc.
What does a program exactly mean?
A program is nothing but a set of instructions given to a computer to be executed. Those sets of instructions can be anything for example adding two numbers or finding a prime number. In a simpler way, a program is a task given to the computer to perform. These instructions can be given in any language such as Java, Python, C, etc. But here we will learn only about Java so I will tell only Java from now.
High-Level Language VS Low-Level Language?
High-Level Language:
- High-Level Language is human-readable language also known as user-friendly or programmer-friendly language.
- Debugging is easy in a high-level language.
- A compiler will compile the high-level language into machine-level language.
- Mostly all languages are high-level languages such as Java, Python, C++, C#, etc.
Low-Level Language:
- Low-Level Languages are not human-readable languages and they are not user-friendly or programmer-friendly language.
- Machines can understand low-level languages very easily.
- Debugging is very difficult in a low-level language.
- Languages such as assembly language are examples of Low-Level languages.
Before learning the Java syntax and writing our first Hello World program let’s understand a few important features of Java.
What is Java?
Java is platform-independent means when we write Java code and compile the java code it converts it into Java byte code this java byte code can run on different machines. Java is known as WORA means write once and run anywhere. Java is a high-level language, It has many features such as Object-Oriented, Robust, Multithreading, security, Interpreted, high performance, and many more.
What is a Java Virtual Machine?
A Java program cannot directly run on the machine. If you want to run the Java program we have to use a program called complier. The job of the compiler is to compile the Java source code into Java byte code. This Java byte code can run on different machines which has JVM installed in it.
“JVM will convert the byte code into machine code”
As I said before java is platform-independent but JVM is platform-dependent means JVM depends on OS(Operating system).
The Flow of Java code:
Firstly we need to write the Java source code and make sure we do not have any syntax errors in Java code. After writing Java code we need to compile the Java code using a compiler. This compiler will compile the Java code and give us Java byte code this java byte code is platform-independent means it can run on any device. Now with the help of JVM, this Java byte code will run on different machines which has JVM present in it.
Features of Java
- Simple to use: Java is easy to learn and quite simple to use. If you have a background of C or C++ programming knowledge the syntax will look similar.
- Object-Oriented: In Java, we mostly use objects, classes, and OOP concepts such as inheritance, polymorphism, encapsulation, and abstraction. But is not 100% Object-Oriented language due to primitive data types.
- Platform Independence: Java is platform Independent as we write code once and run anywhere using the help of JVM(Java Virtual Machine).
- Java APIs: Java is known for its rich API we have many APIs in Java such as JPA(Java Persistence API), Java 3D, and many more.
- Multithreading: Java uses Multithreading to run multiple threads at the same time. it will save a lot of time.
Java Development Kit (JDK) vs Java Runtime Environment (JRE) VS JAVA VIRTUAL MACHINE (JVM)
JDK: JDK is also known as Java development kit. It is a software development environment that is used to develop software applications. JDK contains JRE (Java runtime environment) and other development tools such as compiler, interpreter, and document generator. If you want to develop a Java program then download JDK.
JRE: JRE is also known as Java runtime environment. It is used to run and execute the Java program in our system. JDK contains JVM(Java virtual machine) and other class library files. If you want to just run the Java program but not develop then download JRE in your system.
JVM: JVM is also known as Java virtual machine. It provides platform independence to Java. JVM is used to convert the byte code into machine code.
Setting Up the Development Environment
Let us create our development environment by installing all the required software.
- First, we need to install JDK in our system i recommend installing the latest version of JDK from the official website of Oracle or at least installing JDK version 8 also known as Java 1.8
- After downloading JDK you need to keep the Java bin path in the environment variables as JAVA_HOME.
- Next, we need to install any IDE for creating Java programs. I personally use IntelliJ IDEA but you can download other IDE such as Eclipse or NetBeans. IDE is not a must for creating Java programs and running them. we can write Java code in Notepad and run it manually but these IDE’s provide many features such as debugging project management and many more which will save us time.
- Now we have installed all the required software for writing our Java programs.
Now it is time to write our First Hello World program
Here is our first Hello World program which will display Hello World! in the console.
Let us break down the code:
- public: Public is an access modifier which means that our class is accessible for all other class files.
- class: Class is a keyword that is used to create a class. We generally write class followed by class name.
- static: The main method has to be static so that JVM will load the class into memory and call the main method without creating an object of the class.
- void: Void is a return type. All Java methods will have some return type, void means return nothing.
- main: main is the method name, when a java program starts then JVM will always try to find the main method
- string[] args: Java main method will take arguments from command line arguments these arguments are generally passed at runtime.
Output of the following code :
Don’t worry much about these right now, we will look deeply into them in the future.