How to write your first program in Java


Java is a programming language and has become very popular in the last decade or so. If you are just starting to learn Java then obviously you would have few unanswered questions and few things you might not know at all. So, let’s begin!

Why Java ?

Java is a language based on the OOPs concept (Object Oriented Programming; more on this later) and is one of the most used and demanded programming language. It’s also very beginner-friendly and has huge community support if you get stuck midway through your code. In fact, programming languages like C and Java are essential for selection criteria for campus placements in many colleges.

Getting Started

The first step is to be able to write your first Java program is to download the JDK (Java Development Kit) through Oracle’s website

Next step is to install the file you just download and then check whether it has properly been installed, this can be done using this command on terminal (MacOS, Linux)

java -version

If you get a screen like above then Java is properly installed

On Windows, you can use the following command –

where java

The above command should return the path of Java.exe which signifies that Java is properly installed.

Text Editors and IDE

The next step is to get something on which we can write and edit our Java program which can later be compiled and run, for this we can either get a simple text editor which doesn’t offer any extra features compared to IDE which has a host of features like syntax correcting, auto-completion and auto-indentation

Sublime Text :

SublmiText

Eclipse :

The very popular and my favourite Sublime Text is a free text editor you can go for but it won’t offer much features when compare to IDE like Eclipse which is very popular for Java. Netbeans is also a very popular alternative to Eclipse and offer many features to aid you while you program.

If you are starting out for very first time, it would be better if you use a text editor instead of an IDE as it will help you to remember to basic codes which are very essential for you to learn.

Hello World in Java!

So we are now all set to write our first java program, we will keep it simple and take the most basic program in almost every programming language i.e. to print “Hello World” to our console/terminal.

Follow these steps in order to run your java program :

  • Open your preferred text editor and type the program as shown or copy the code from below. The java program always begin with the keyword class followed by classname.
  • After writing the above program just save the file as ‘HelloWorld.java’ (which is nothing but <classname>.java and every java file must be saved using the classname which precedes your ‘class’ keyword in your program).
  • After saving your file go to your terminal and locate the file using cd command (if you have file on Desktop use command as ‘cd /Desktop’).
  • Now you firstly need to compile the file which can be done by executing the below command:
    javac HelloWorld.java

  • Now the final step is run your java file that can be done using:
    java HelloWorld

 

Here you can see we have printed ‘Hello World’ on console/terminal. If you have any trouble writing you can copy the code below and try running it! In my next post I will be explaining each and every line of our HelloWorld program to help you understand what’s actually happening.  Stay tuned :)

The Code:

class HelloWorld
{
    public static void main(String [] args){
        System.out.println("Hello World");
    }
}

Leave a Comment