Earlier we briefly discussed that Java is a platform which is software-only platform and it runs on other hardware-based platforms. Java provides a reliable platform upon which many services and applications are built.
At this point, you are probably tired of all the talking but no coding and eager to write some java code in your text editor and run your first Java Application to see some java in action. You are fully prepared to start your journey as a software developer and can not wait any longer to be rich. But we would have to ask you to hold your horse for a little bit longer. We have to go through and understand several for you to fully understand what you’re actually doing.
Whether you are familiar with some other programming languages and hence know about concepts like compilations, what an executable file is, or a complete novice in the world of programming, please go through the following. And keep in mind that Java works in a way that differs from most other programming languages like C or C++.
You have to follow several steps to create a basic Java Application. If you want to create some other complex application such as Enterprise Application or Mobile Application, the creation process is more complex but at its core, you will find these simple steps. Yes, you heard it right! These few simple steps! So now let us focus on the following topics and try to make sense of them.
Install Visual Studio Code on Windows
Yes! You guessed it right! You have to write some java code in a text editor. You can use any text editor in this matter. We will be using Visual Studio Code for this course to write our java source code.
1. Go to: Visual Studio Code to download Visual Studio Code.
2. Choose your platform (the Operating System running in your machine). We are using Windows 10 as our Operating System so we will be choosing Windows. You can also download your OS specific version from the list. You will be actually downloading the installer (VSCodeUserSetup-{version}.exe).
3. Wait a few seconds before the automatic downloading of the installer begins. If the downloading doesn't start automatically there is some option to download it manually. You will find it at the top of the page.
4. Once the installer is downloaded, run the installer (VSCodeUserSetup-{version}.exe). This will only take a minute.
By default, VS Code is installed under C:\Users\{Username}\AppData\Local\Programs\Microsoft VS Code.
5. Run it and start writing source code.
Write Java Code in Visual Studio Code
Type the following Java Code in the text editor (please don’t copy and paste . Type it by hand so that the words are registered in your brain and you get a little bit of muscle memory.)
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Save it anywhere in your Computer as a .java file.
You have to remember two things while saving the file. First, the file name must be "HelloWorld". Second, the file extension must be ".java". Don't worry about the details yet.
For now all you have to do is “Type the Java Code and Save it as "HelloWorld.java" in your machine”. You have to keep in mind that the java source code you have written is saved in plain text files.
You will have a file like this on your machine.
Compile the Java Source Code.
Compiling Java source code is the process of converting human-readable code into machine-readable code that can be executed by a computer. The compiler takes the source code written by the programmer and generates an executable file, which can be run on a Java Virtual Machine. This process helps identify syntax errors and other issues in the code before it is executed. The compiled code is also optimized for performance, making it faster and more efficient to run.
What is Compiling Source Code and Why is it Necessary?
The code written in the text editor and saved in the HelloWorld.java file is in a human-readable format. We, humans, are comfortable with text-based formats. But computers are not. They are comfortable with numbers. Actually, all they understand is 0 and 1. They are called bits(Binary format). We will get into that in a later article. Right now the key point to take is that computers do not understand texts. They only understand numbers. But in our source code file, we wrote the instructions in text format that we humans understand and are comfortable with. So the Computers can not execute the instructions as they can not make sense of them. We need to transform the source code into another format, which can be understood and executed by the computer. This transformation is called Compiling.
In simple words, compiling source code means transforming human readable text based format to computer executable format.
To do this we need a special tool called a compiler. Some languages do not have a compiler. They transform the format in some other way. Java does have a compiler. It is called javac. Every compiler is specific to a language. You can not compile a java source code using a compiler that is used to compile C or C++ source code.
The compiler produces a file after compiling the source code. This file is called a binary file or an executable file. Whereas you can read a source code and understand it, binary or executable files are not meant to be read and understood by a human person. Only your computer can make sense of it.
This code contains special binary codes called byte codes. This is a technical term that you may have already come across. The precise description of what this byte code is beyond the scope of this article.
You may face failure when compiling; your code has to be correct for the compiler to produce an executable version of it. Once the compiling is successful and you get the binary/executable file that you need, you can execute this binary file. In other words, you can run your application.
These two steps: compilation and execution(running your application) require two very specific pieces of software. You do not need to be perplexed. They come with your Java Development Kit, also known as the JDK. You will see how to download the JDK for free and install it on your machine in the next tutorial.