Sotirios-Efstathios Maneas

About Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a postgraduate student at the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. His main interests include distributed systems, web crawling, model checking, operating systems, programming languages and web applications.

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close