Java from the Scratch – Part I – Say Hello

Share

What is Java? Well everyone know that Java is a general-purpose computer programming language. But what is so special about Java? Well it’s object-oriented programming (OOP) language, it’s concurrent and also it is designed to let developers to write once, run anywhere (WORA) means developers could run compiled code could run on any platform.

But in reality, there is a virtual machine (JVM) which could run java compiled code (Byte Code). If a developer could install this JVM in any platform or the JVM is already installed on any platform, compiled java code could be run on the platform. If there is platform without JVM, any java compiled code couldn’t be run on that platform.

Of course Java is a one of the most popular programming language in use. Since Java is working on a virtual machine, Java is unable to access lower level facilities of a computer. Java is originally developed by James Gosling at Sun Microsystems. Currently Java is owned by Oracle Corporation.

Source: Java (programming language)

So lets install java development kit because we’re going to develop java programs. Since java is running on many platforms, here I’m going to tell you how to install inside Windows Platform (Windows 7, 8, 8.1, 10) and Linux platform (Ubuntu, Linux Mint, Arch Linux).
First of all, we have to download jdk installation package. So google for jdk or goto this link to download the package.

Then go to Java Download, accept the license agreement and download the relevant installation package for your platform. (Arch Linux, Ubuntu, Linux Mint users doesn’t need to download it.) If you’re using Windows users should download *.exe package as for their platforms.

Install inside Windows platform

Read this topic if you have a Windows operating system such as Windows 7, 8, 8.1, 10 etc. When you completed downloading the installation package, double click on it and run the setup. Click next again and again so you will get following steps respectively. There’s nothing to configure in installing so don’t worry, just hit on next button.

Figure 1
Figure 1
Figure02
Figure 2
Figure03
Figure 3
Figure04
Figure 4
Figure05
Figure 5

Finally close the installation wizard.

Figure06
Figure 6

Installing steps are easy. Now you have to add java path to windows environmental variables’ path, so the system could identify java commands. To add path follow following steps.

Add java path to windows environmental variables’ path

First go to Computer (This PC, My Computer).

Figure 7
Figure 7

Now go to System Properties in the Computer Tab. (You could go to system properties by right clicking on Computer Icon also. This method may be useful if you’re using Windows 7 ).

Figure 8
Figure 8

From the system properties window, go to Advanced System settings.

Figure 9
Figure 9

In advanced system settings go to Environment Variables.

Figure 10
Figure 10

Now find Path variable from system variables list and click on Edit.

Figure 11
Figure 11

Now copy the path of your java bin folder. Usually it will be C:\Program Files\Java\jdk<version>\bin

Figure 12
Figure 12

Now add a semicolon (;) at the end of the variable value text box and paste the path of the java bin folder.

Figure 13
Figure 13

Now clock on the OK button of all opened windows and close all other windows. And press WinKey + R to get Run window and type cmd in it and press Enter to get command prompt. Type javac in the command prompt and press enter.

Figure 14
Figure 14
Figure 15
Figure 15

If you have entered the path correctly, you will get an output like following.

Figure 16
Figure 16

Install inside Linux platform

Linux users should follow following steps to install jdk on your systems. Here I’ll give methods to install in distributions which uses apt-get (Ubuntu, Linux Mint etc.) and pacman (Arch Linux etc.).

Install in Ubuntu, Linux Mint etc. (with apt-get)

Use following commands to install java development kit.
$ sudo add-apt-repository ppa:webupd8team/java -y
$ sudo apt-get update
Figure 17
Figure 17

It may take little time to update packages database. So wait until it gets finished. When finished enter following command.

$ sudo apt-get install oracle-java8-installer

To continue, type y and press enter.

Figure 18
Figure 18

Then you will be getting a message like following. Use enter button to press OK in it.

Figure 19
Figure 19

Then select Yes using arrow keys and press enter to accept license agreement.

Figure 20
Figure 20

Then jdk will be downloaded and installed.

Figure 21
Figure 21

Finally install following package using following command.

$ sudo apt-get install oracle-java8-set-default

Then you’ll be ready to go.

Install in Arch Linux (with pacman)

Use following command to install jdk inside Arch Linux.

$ sudo pacman -S jdk8-openjdk
Figure 22
Figure 22

Type y and enter to continue installation.
Finally after any installation, type javac in the terminal and if you got a similar output to following figure, you have correctly installed java.

Figure 23
Figure 23

Now you’re ready to develop your first java program. So let’s say Hello, World..! In the following method, I’m going to tell you how to print Hello, Wolrd..! in the command prompt or terminal.

First of all, open a text editor (Notepad, Gedit etc.).

First we have to create a java class. What is a java class? I’ll answer for that question in a next part. For now you just need to know the basic way of coding. So first of all you need to create a class. Following is the way of creating basic java class. You have to type keyword class and the class name (When adding class name, there shouldn’t be any spaces)

class ClassName{

}

Now you’re going to create your first java class. Let take our class name as Hello. Type following lines in a text editor.

class Hello{

}

Now you have to save your file. You must use extension .java when you’re saving. Also normally we use class name as the file name when we save the file. So save the file as Hello.java. Remember the path which you’re saving the document.

Figure 24
Figure 24

In windows, you need to change save as type to All Files before type the name.

Figure 25
Figure 25

Then you could save the file.

Figure 26
Figure 26

Now we have to create the main method of the program. Main method is the first running code in the whole document. So when you’re coding a java program, you must add a main method to your program, otherwise the program will not work.

Main method must be included inside a class. Java main method is coded as follows,

public static void main(String[] args){

}

So let add main method to our code.

class Hello{
          public static void main(String[] args){
     }
}
Figure 27
Figure 27

Now we have to enter following code which prints a string line.

System.out.println(“<text to print in terminal>”);

We need to print Hello, world..! So our code will be,

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

Now you have coded your first java program. Now we need to compile it and run it. Before that you need to get into the folder where you have saved your java source code. Here I tell you some important commands to go there in Linux and Windows. And then let’s compile the program.

    • Windows:

To change the drive, just type drive letter and a colon mark and press enter.

Figure 29
Figure 29

To go back type cd.. and press enter.

Figure 30
Figure 30

To go forward, type cd <directory name> and press enter.

Figure 31
Figure 31

I have saved my java source code in C:\Users\Rashintha\Documents\Java directory. So lets go to the directory using above commands.

Figure 32
Figure 32
    • Linux:

To go back type cd .. and press enter.

Figure 33
Figure 33

To go forward, type cd <directory name> and press enter.

Figure 34
Figure 34

I have saved my java source code in /home/rashintha/Documents directory. So lets go to the directory using above commands.

Figure 35
Figure 35

Now let’s compile and run our program. Use javac command to compile the code.

javac FileName.java

Eg: javac Hello.java

Figure 36
Figure 36

When you compile the code, it will be giving no output if your code have no syntax errors. If there is any error, it will be shown in the terminal.
Now let’s run the program using java command. When we’re running the program, we need to use class name. Only when compiling, we have to use file name. We use same name as the file name and class name so we could minimize problems.

java ClassName
Eg: java Hello

Figure 37
Figure 37
Figure 38
Figure 38

If you did everything correctly, you will be getting an output like this.

Try yourself:

Try to get following outputs using the java lesson you have learnt.

    • Q1:
First Line
Second Line
    • Q2:
*
**
* * *
* * * *

If you have any suggestions to improve this article, please forward them to students.blog@sci.cmb.ac.lk.

 
Tagged : / /