Java has always been and still is a universal programming language. Every day we come across such aspirants who are struggling with understanding the boilerplate Java code. Let’s put a pin in that & redirect our focus on the fundamental Java syntax of your very first code.
Here in this blog, you will get to learn the process of writing, compiling, and running your very first Java program.
“Hello World!” Java Program
I guess you already checked out the Java code of your first program which is written right below this paragraph. The objective is to print the following output on the console i.e. “Hello World!”
Well, it may look gibberish but give it time, you will get used to it!
class Hello
{
public static void main(String[] args)
{
System.out.println ("Hello World program");
}
};
Let’s translate the above code into normally understandable language. The terminologies used in the code along with their meaning and purpose, are as follows:
- class: Every programming language has its own set of reserved words, meaning thereby, their functionalities are pre-defined. The word “class” is one of those and is used to declare classes in your program.
- public: Another reserved keyword and it denotes that a certain class or a function is publicly visible to the rest of the modules.
- static: Whenever our purpose is to execute a function, without having to create any object of its class, we simply add the word “static” right before it.
- void: You know the literal meaning of the word “void”, right? Exactly, the same meaning can be used here. This is another keyword of Java language and it means that the following question won’t be returning anything.
- main(): Calling it one of the most crucial methods in Java, won’t be wrong at all. Whenever you try running any program, the very first function that gets executed by the compiler is the main () function. So, make sure to include every bit of logic here.
- String [] args: As you may have noticed, there are square brackets followed by the word “string”. This is how we denote an array. It means that we have an array whose parameters are of the string data-type.
- System.out.println:Want to print the output over the console? Simply add this command and hit “enter”!
We don’t expect you to have understood every bit of what’s written above straight away, but consistency will get you there.
Things You Need to Run Your First Java Program
Programming languages come with their own environment. Java programming language is no exception. For Java, there is Java SE Development Kit (JDK).
Java SE Development Kit
A complete software package that needs to be on your machine before you start exploring the pool of programming. After downloading the JDK, you will witness that it further has the Java Runtime Environment (JRE). The fact about JRE is, it’s the main package for Java.
It actually enables your computer to execute Java programs. The Java Virtual Machine (JVM) acts as a translator in translating the bytecode into machine-understandable code.
Also, you should have knowledge about the following tools in Java:
- javac.exe: acts as a compiler that converts java code.
- java.exe: acts as a virtual machine and comes in handy for executing programs.
Text Editor
Don’t burden your machine with high-end software packages because you will end up slowing down the processing speed of your system. At this stage, all you need is a simple text editor which comes pre-installed in all systems.
Compiling & Running Your First Java Program
Here in this portion, the step-by-step procedure of compiling and running your first Java program is listed.
Write Source Code
Although copying and pasting the code seems like an easy job, I don’t recommend following this practice because it will kill your habit of scripting the code on your own.
Your job for today is to use the following code which will print a string on the console:
class Hello
{
public static void main(String[] args)
{
System.out.println ("Hello World program");
}
};Save the Code File
This is where you should pay a little more attention. Save the code file as “HelloWorld.java”. The .java extension tells your system that “I am the java file”.
Open Command Prompt
Well, who doesn’t know about the command prompt? The same screen with the black background that we once perceived as “the hacking screen”. Well, now we know that it’s basically the command prompt or command console where we simply type commands, and upon hitting enter, it actually executes those commands for us.
Here’s how you can open up the command prompt:
- Hit the combination keys “CTRL+R”. A dialogue box will appear.
- In the input field of that dialogue box, type “cmd” and hit “enter”.
Bingo! There the screen of the command prompt shows up.
Type Commands & Hit Enter
Once the console opens up, the first step is to take your system where your “HelloWorld.java” file is located.
After navigating to that folder, write the following command: javac HelloWorld.java
Hitting the enter key will trigger the operation of code compilation. Also, you will get to see another file of the .class extension that’ll show up in the same folder where your main file is located. Just so you know, your code’s compiled version is ready.
Run Your First Program
If you have followed all of the prerequisites before this step successfully, it means you are on the right track and all that’s left is running the program.
On the same screen of the command prompt, write the following command and press enter: java HelloWorld
As a result, you will have the following string printed out over the console: Hello World!
Wrapping Up
Bravo! It seems that you have drafted the code, compiled it, and ran your very first Java program and that too, without any error. Remember, it’s a kick-start blog for you and now you are on your way to learn Java to follow an interesting career path. Keep on exploring this programming language and share your experiences in the community for the rest of the freshies to follow.