Java Basics

Java programming tutorial – Getting started with Java

In this tutorial we will discuss about the Java programming language and how to develop and execute our first application. Java is an object oriented programming language that aims for portability, high performance, robustness and security.

The Java Compiler is used to transform the Java source files to bytecode, which can be executed by the Java Runtime Environment (JRE). Moreover, the Java Development Kit (JDK) aims for providing a development environment for building applications, applets, and components using the Java programming language. The JDK includes tools useful for developing and testing programs written in the Java programming language and running on the Java platform. Finally, the JDK also contains a Java Runtime Environment (JRE).

Install the Java Runtime Environment

First of all, we must install the JRE in our local machine, in order to be able to execute Java applications. For more information on how to install Java in Windows, you can consult our tutorial here. For information on how to install Java in Linux, you can consult our tutorial here.

Furthermore, as already mentioned, the JRE is also included in the Java Development Kit. For more information on how to install the JDK in Windows, please consult our tutorial here.

Developing Java Applications

In order to develop and execute a Java application we need the Java Compiler to convert it into bytecode and then, let the JRE execute that bytecode. The Java Compiler is included in the JDK and can be used from the Windows command line, or from the terminal of Linux and Mac.

Once you have installed the Java Compiler in your local machine, you can start coding Java programs. The only thing you need is a simple text editor and the terminal (Linux or Mac) or the command line (Windows).

Moreover, a bunch of Java IDEs have been created, in order to facilitate and support the development and debugging of Java applications. IDEs provide many features, such as syntax highlighting and code completion. Also, the IDEs contain a compiler or interpreter that can be used to convert Java source files into bytecode, that can later be executed.

The most famous Java IDEs are Eclipse, Netbeans and IntelliJ IDEA.

The first Java program

The first Java program is very simple and just prints a String in the console. The program is called HelloWorld.java and is shown below:

HelloWorld.java:

class HelloWorld {

     public static void main(String[] args) {
          System.out.println(“Hello World”);
     }
}

Let’s explain the structure of this program. First of all, as we can see we have created a class called HelloWorld. The class is a fundamental component of Java and can be used to represent objects and their functionality. For more information about classes in Java, please refer to this link here.

Inside the HelloWorld class, we observe the main method. In Java, every application must contain a main method. The signature of the main method is the following:

public static void main(String[]);

The main method is used as the entry point of your application. Finally, inside the main method, we use the System class from the Java core library, in order to print a message to the standard output. Such files that contain Java code, are called source files.

Compile and Execute a Java Source File

In case you are familiar using an IDE, then you compile and execute Java programs, directly from the IDE. However, in case you are using a simple text editor, then you must undergo the following steps, in order to compile and execute a Java program:

  • Using the terminal (Linux or Mac) or the command line (Windows), navigate to the folder where you have stored your Java source file.
  • Execute the following command to compile your source file to bytecode:
    javac HelloWorld.java
    

    If you don’t have any errors in your source file, the HelloWorld.class file will be created. Otherwise, you must resolve all appearing errors and then, re-compile.

  • Finally, to execute the application, issue the following command:
    java HelloWorld
    

    A sample execution is shown below:

    Hello World!

More advanced Java Programs

The Java programming language contains a large number of libraries that can be used, in order for interesting and functional applications to be developed. These libraries contain classes that provide that functionality. Java classes are organized into packages, which can be imported by our application.

A very common package is the java.util package. This package contains the ArrayList class, which provides an implementation of a linked list. In order to use the ArrayList class, we must import the java.util package, before declaring our classes, as shown below:

import java.util.ArrayList;

An example that uses the ArrayList class is shown below:

ArrayListExample.java:

import java.util.ArrayList;

class ArrayListExample {

     public static void main(String[] args) {
          ArrayList<Integer> list = new ArrayList<Integer>();

          // Add some values to the list.
          list.add(1);
          list.add(23);
          list.add(456);

          for(Integer num: list)
               System.out.println(num);
     }
}

As we can observe, we declare and initialize an ArrayList of integers. Thus, this list may contain only integer values. Inserting a double value or a string is not permitted in this specific list and an error will be thrown. Then, we add some values to the list and finally, print these values in the standard output.

A sample execution is shown below:

1
23
456

 
This was a tutorial about the fundamental components of the Java programming language and how to develop and execute a simple Java program.

Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
pranit
6 years ago

This tutorial is very helpful as I have just started learning ,I am getting things in correct manner ,and by following steps that are mentioned in the article, I’m getting things very easily and definitions are also very clearly understood..

sam
5 years ago

Thanks a lot for this tutorial .
In Eclipse , which IDE can you choose when you’re interested in Mobile Application development . Is the latest IDE suggested good to download anyway ?

Back to top button