This tutorial provides you with a simple and cleaner approach to writing your first Java program. It explains every single line of code to bring clarity to you.
Introduction
Before you get on to writing your first Java program, make sure you know what will you get after learning Java. So, here are some reasons why you should learn Java.
Feature | Description |
---|---|
Java is popular. | It is a programming language that is used by many big companies, such as Google, Amazon, and Netflix. |
Java is versatile. | You can use it to create many different types of applications, such as web applications, mobile applications, and desktop applications. |
Java is portable. | The code written in Java can run on many different platforms, such as Windows, Mac, and Linux. |
Java is secure. | It has built-in features to prevent security vulnerabilities. |
Java is object-oriented. | Being object-oriented makes it a good language to learn for beginners. |
In short, Java is a powerful and versatile programming language that is a good choice for beginners and experienced developers alike. Here are some additional benefits of learning Java:
Benefits | A Short Description |
---|---|
High salary potential | Java developers are in high demand and command high salaries. |
Job security | Java is a mature language with a large community of developers, so there is a lot of job security in the Java field. |
Learning curve | Java is a relatively easy language to learn, so you can start developing applications quickly. |
Open source | Java is an open-source language, so there are many free resources available to help you learn. |
If you are interested in learning to code, Java is a great language to start with. It is a powerful, versatile, and in-demand language that can open up many opportunities for you.
Learn to Write Your First Java Program
The tutorial has the following sections to help you learn quickly.
Pre-requisites
JDK
Before you paddle up to write your first Java program, make sure you have the Java SE development kit installed on the system.
You can confirm it in many ways:
1) On Windows, open the “Run” window (Press Win+R), and type “appwiz.cpl
” to launch the “Programs and Features” dialog. Search for the Java SE Development Kit. Or you can directly go to the “C:\Program Files” or “C:\Program Files (x86)\” directory, and check if it has some “jdkX.X.X_XX” folder inside.
2) On Linux or Mac OS, open the terminal (CTRL+T), and run the following commands to check Java availability.
# check if java is installed whereis java # check java location which java
In case, you don’t find Java on your system, then download and install the latest version from the formal Oracle site. Once you have everything set up, then check if the JAVA_HOME variable exists or not.
Open the console window, and issue one of the following commands as per the OS you are using:
# On Windows echo JAVA_HOME = %JAVA_HOME% # On Linux or Mac OS echo JAVA_HOME = $JAVA_HOME
By the way, if you don’t have the above variable set, then don’t worry and add one yourself. Adding JAVA_HOME to your environment is not only a good practice but useful too.
a) To do it on Windows, open the “Edit environment variables” dialog and set up the JAVA_HOME pointing to the Java installation directory.
b) To do it on Linux or Mac OS, update the user profile “~/.profile” by adding an entry of JAVA_HOME.
Text Editor or IDE
You can either pick a text editor (like Notepad or Notepad++ on Windows and Vi/Vim on Linux/Mac OS) or an IDE (like Eclipse or NetBeans or CodeBlocks) to write Java code.
It’s an ideal practice to begin using an IDE (Integrated Development Environment) so that you can avoid the headache of manually compiling the code and rectifying errors highlighted by the interpreter.
Don’t miss to read the Java Coding Guidelines.
Objective
Since time, newbie programmers have traditionally used the “Hello World!” as their first programming exercise.
In this tutorial also, we’ll be using it to illustrate the basics of Java programming syntax and code flow.
Write your first Java program
First, launch either a text editor or the IDE that you have chosen. Create a file named “MyFirstProgram.java” and save it. Next, here are some thumb rules for you to remember.
1. Every Java program has a main class, and its name must match the name of your Java file (i.e., MyFirstProgram in our case). Also, you need to use “public” access modifier before it.
// Class name should match the name of the Java file public class MyFirstProgram {
2. As per standard naming conventions, Java classes should start with a capitalized character, and follow the camel case format (in which the first letter of each word is capitalized).
// The class name remains in CamelCase format public class << MyFirstProgram >> {
3. The rules suggest against underscores (_) and dollar signs ($).
Also, note that every main class has an entry point routine called “main.” You place the main business logic in this function.
class MyFirstProgram { public static void main(String args[]) {
Now, check out the following lines of code and type them into the “MyFirstProgram.java” file.
// MyFirstProgram.java public class MyFirstProgram { public static void main(String args[]) { System.out.println("Hello World!"); } }
After seeing the above code, you might have observed a few keywords used there. Let’s understand their meaning and purpose.
Public: It is an access modifier indicating that you can call the main from anywhere, even from outside the program.
Static: It is a keyword that means the function can exist and run by itself (without creating an object).
Void: Another keyword, it means that the function doesn’t return any value.
Build and compile your code
Open a terminal window and change the current directory to where your Java file is available:
cd /path_to_file/
Once done, the next line to write is:
# Build and compile java program javac MyFirstProgram.java
Sometimes, you get the error that the system didn’t find the “javac” command or it’s an inoperable program or batch file. In such a case, check if JDK exists and its environment variable points to the correct path.
Once successful, the compiler will at that point produce a bytecode file called MyFirstProgram.class. Java Virtual Machine (JVM) interprets and executes this file.
C:\>dir MyFirstProgram.* Volume in drive C has no label. Volume Serial Number is 9050-B24D Directory of C:\ 03/23/2019 12:27 AM 434 MyFirstProgram.class 03/23/2019 12:27 AM 156 MyFirstProgram.java 2 File(s) 590 bytes 0 Dir(s) 13,673,246,720 bytes free
The Java programming dialect compiler (javac) pursues source files written in the Java programming dialect and turns them into bytecode. Alternatively, the compiler can likewise process comments found in the source utilizing the Pluggable Annotation Processing API.
Execute the Java program
For the final step, use the following command:
# Run the java program java MyFirstProgram
If you see the output, then you have successfully coded your very first Java program!
C:\>java MyFirstProgram Hello World!
Conclusion
If the insight into the program was too much for once, don’t worry about it.
As you make progress with your Java skills, you will start realizing the need for every keyword or access modifier used in your very first program.
For the time being, take a moment to appreciate your first-ever Java code.