Java

Best Way to Learn Java Programming Online

1. Introduction

Java is one of the most widely used programming languages. According to a recent report by Github, Java was ranked as the 2nd most used programming language after JavaScript. There is a very big list of topics that someone has to learn to master Java. The good thing is that you can find lots of content online. In this post, we will categorize all those topics and provide references to articles and code examples that will guide you through the process of learning Java.

Before we start, take a look on What is Java used for.

2. Prerequisites

2.1 Installing Java

The first step before starting writing your first programs is to install Java and especially the JDK (Java Development Kit) which consists of the libraries needed to write programs and the Java compiler, known as JRE. A step-by-step guide of how to install Java and run your first program via the JDK commands can be found:

2.2 Installing an IDE

An IDE is an essential tool as it helps you with the development and compilation of Java programs. The IDEs are bundled with a set of plugins that can make your life easier. The most widely used IDEs are:

If you want to learn more about how to download, install and use those IDEs then read the following tutorials:

In the following section we move on to the Java basics.

3. Basics – Core Java

After installing Java and your favorite IDE, you are ready to learn about the basics of Java also known as Core Java. Before that, you should first understand how a program starts through the main method:

Then, you can create your first program:

3.1 Java Packages

A Java project consists of Classes that are grouped within packages. It is similar to the concept of folders (packages) and files (classes). A nicely organised project will help developers maintain, understand and read code easily.

3.2 Java Variables

Let’s see now what variables are supported by Java are how to declare them. The variable is the basic unit of storage in a Java program. A variable is defined by the combination of an identifier, a type, and an optional initializer. In addition, all variables have a scope, which defines their visibility, and a lifetime.

See below an example of declaring and initialising Java variables:

int a, b, c; // declares three ints, a, b, and c.

int d = 3, e, f = 5; // declares three more ints, initializing // d and f.

byte z = 22; // initializes z.

double pi = 3.14159; // declares an approximation of pi.

char x = ‘x’; // the variable x has the value ‘x’.

boolean d = false;   // boolean value initialized with value false;

3.4 Java Primitive Data Types

Primitive data types are the most basic data types. Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean. In the previous example, we saw how to declare them and initialize them. Let’s see in more details those:

3.5 Java Operators

Operators are used to perform operations on variables and values. In short, the most basic operators and their usage in Java are:

  • Use the Additive operator to add variables.
  • Use the Subtraction operator to subtract variables.
  • Use the Multiplication operator to multiply variables.
  • Use the Division operator to divide variables.
  • Use the Modulo operator to get the remainder of the division of variables.

To learn more about those operators see the following articles:

3.6 Java if-else

The if-else statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true. It has a very simple syntax and more info can be found:

See below a very basic example of the if-else statement:

boolean b = false;
 
if (b) {
    System.out.println("Variable value is true");
}
else {
    System.out.println("Variable value is NOT true");
}

3.7 Java Loops

If you need to execute a block of code many times, then you will definitely have to use a mechanism named as loop. Java provides three looping mechanisms, which are the for, while loops and switch statements.

Let’s see below an example of a for loop which is the most widely used:

public static void main(String args[]) {
    String[] cities = { "Athens", "Thessaloniki", "Chania", "Patra", "Larissa" };
 
    for (String cityname : cities) {
        System.out.println(cityname);
    }
 
}

3.8 Java Arrays

Arrays are used in the majority of Java programs. You need to understand how to initialise and iterate arrays without accessing the array for an index that is out of the array bounds.

Find below an example of the initialisation and iteration of an array:

public static void main(String args[]) {
 
    // declare a string array with initial size
    String[] schoolbag = new String[4];
 
    // add elements to the array    
    schoolbag[0] = "Books";
    schoolbag[1] = "Pens";
    schoolbag[2] = "Pencils";
    schoolbag[3] = "Notebooks";
       
    // this will cause ArrayIndexOutOfBoundsException
    // schoolbag[4] = "Notebooks";
}

3.9 Java Exceptions

No program has been written that it is flawless. For that Java supports the handling of errors through exceptions. Exceptions are thrown either by the developers or by Java itself.

4. Data Types

The Java data types are implementations of very important data structures in programming. Below we list the most important ones and examples of their methods.

4.1 String

The String class is probably one of the most used types in Java programs. A String is actually a sequence of characters. As a matter of fact, a String Object is backed by a char array. A String and can be initialized in two ways:

String str= "Hello World";
String str = new String("Hello World");

For more info about the String class see:

Lefteris Karageorgiou

Lefteris is a Lead Software Engineer at ZuluTrade and has been responsible for re-architecting the backend of the main website from a monolith to event-driven microservices using Java, Spring Boot/Cloud, RabbitMQ, Redis. He has extensive work experience for over 10 years in Software Development, working mainly in the FinTech and Sports Betting industries. Prior to joining ZuluTrade, Lefteris worked as a Senior Java Developer at Inspired Gaming Group in London, building enterprise sports betting applications for William Hills and Paddy Power. He enjoys working with large-scalable, real-time and high-volume systems deployed into AWS and wants to combine his passion for technology and traveling by attending software conferences all over the world.
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
Angelo Kenel
Angelo Kenel
6 years ago

Outstanding contribution. Thanks, you are saving lives.

Back to top button