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.
Table Of Contents
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:
- How to Create and Run Your First Java Program
- How to download Java 14 for Windows 10
- Download and Install Java Development Kit (JDK) 13
- Download and Install Java Development Kit (JDK) 11
- Download and Install Java Development Kit (JDK) 8
- How to update Java for Windows 10, macOS, and Android
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.
- Java for loop Example
- For Each Loop Java 8 Example
- Simple while loop Java Example
- Java Switch-Case Example
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.
- Handling Exceptions In Java
- Java Exception Handling Tutorial with Examples and Best Practices
- Try Catch Java Example
- java.lang.NullPointerException Example – How to handle Java Null Pointer Exception
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:
Java provides a number of methods that make the String manipulation easy and efficient, straight out of the box. The most important ones are:
- Java String format Example
- Java String Compare Example
- Java String split Example
- Java String Replace Example
- Java String replaceAll example
- Java String length Example
- Java String Contains Example
- Java String matches Example
- Java String Array Example
4.2 Set
A set is a data structure which holds unique values. In Java, the Set interface has multiple implementations which are widely used in programs as they offer quick lookups. The most widely used implementation of the Set interface is the HashSet class.
4.3 Map
A map is a data structure which holds key-value pairs. Similar to the Set, the Map interface also has multiple implementations and they offer quick lookups. The most widely used implementation of the Map interface is the HashMap class.
4.4 Queue
A queue is a data structure in which the elements are kept in order. The Queue interface has multiple implementations and the LinkedList class is the most used one.
4.5 Tree
A tree is a data structure which holds a collection of elements starting from the root, in which each element holds a value and a reference to the children elements. The tree implementations provided by Java are the TreeMap and TreeSet classes.
4.6 Enum
An Enum is a special data type that enables for a variable to belong to a set of predefined constants. The values defined inside an enum are constants and shall be typed in uppercase letters.
5. Collections
The Java Collections framework is a set of classes and interfaces that implement some of the most commonly used collection data structures that we saw in the previous section e.g. Set, Map, Queue, Tree. The java.util.Collections class has some very important methods:
- Java Collections Tutorial
- java.util.Collections Example
- Java Collections Sort Example
- Shuffle List elements example
- Reverse order of List example
- Replace all elements of List example
- Copy Collection to another Collection example
The following comparisons will help you decide on the best data structure depending on the use case.
6. Algorithms
It’s very important to learn how common algorithms like sorting, recursion etc are implemented in Java. Understanding and learning algorithms will make you a better developer regardless of the programming language you are using. Below we provide some of the most common algorithms:
- Quicksort algorithm in Java
- Mergesort algorithm in Java
- Java Recursion Example
- Factorial Program in Java
- Palindrome Program in Java
- Java Prime Numbers Example
- Binary search Java array example
7. Concurrency
Java has excellent support for multithreading programs in which multiple threads are executed simultaneously. Concurrency is one of the most difficult topic to understand and master in Java, so it requires a lot of reading and practice. To get started with concurrency read:
- Introduction to Threads and Concurrency
- Java Concurrency Essentials Tutorial
- You can download Java Concurrency Essentials
Then you should learn the fundamentals of concurrency such as deadlocks, monitors, the synchronized and volatile keywords:
- Concurrency Fundamentals: Deadlocks and Object Monitors
- Java Synchronized keyword Example
- How Volatile in Java works?
- Java Concurrency Essentials Tutorial
Java provides concurrent classes which will help you with the development of multithreading programs:
Finally performance and testing should be taken into consideration for concurrent applications:
8. Design Patterns
Another very important topic to learn as a Java developer is design patterns. In programming, a design pattern offers a solution to common problems in software design. There is a very big list of design patterns that can be transformed into code. Some of the most commonly used ones are:
Apart from the above, there are also other design patterns that you would want to learn if you want to become a master in design and architecture of Java program. These are the following:
- Visitor
- Iterator
- Decorator
- Interpreter
- Command
- Strategy
- State
- Template
- Memento
- Prototype
- Flyweight
- Chain of Responsibility
- Proxy
- Mediator
- Bridge
- Composite
- Adapter
9. Spring Framework
Java has a big open source community which has built many frameworks that ease the development of Java applications. The most famous one is the Spring framework. Spring is an open-source framework which consists of several modules created to address the complexity of an enterprise application development. To get started with Spring:
- Spring Framework Example
- Java Spring Tutorial
- Spring Framework Tutorial for Beginners with Examples
- You can download Spring Framework Cookbook
Below we take a look at the most important modules provided by Spring.
9.1 Spring AOP
Spring AOP is used to provide declarative enterprise services, especially as a replacement for EJB declarative services. It is also used to allow users to implement custom aspects, complementing their use of OOP with AOP.
9.2 Spring Security
Spring Security enables the developers to integrate security features easily and in a managed way. It is also integrated with the latest OAuth2 authorization framework which enables a third-party application to obtain limited access to an HTTP service, either on behalf of a resource owner by orchestrating an approval interaction between the resource owner and the HTTP service, or by allowing the third-party application to obtain access on its own behalf.
- Spring Security 4 Tutorial
- Spring Security OAuth2 Example
- Spring Security via Database Authentication Tutorial
9.3 Spring Transaction
Spring facilitates transaction management using annotations. IT provides an abstract layer on top of different transaction management APIs and it helps us to focus on the business problem, without having to know much about the underlying transaction management APIs.
- Spring Transaction Management Example with JDBC Example
- Understanding Transactional annotation in Spring
- How does Spring @Transactional Really Work?
9.4 Spring Data
Spring Data makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services. The benefits of using Spring Data is that it removes a lot of boiler-plate code and provides a cleaner and more readable implementation of DAO layer. Also, it helps make the code loosely coupled and as such switching between different JPA vendors is a matter of configuration.
- Spring data tutorial for beginners
- Spring Data JPA Tutorial
- You can download Spring Data Programming Cookbook
9.5 Spring MVC
Spring MVC provides Model-View-Controller architecture that eases the development of loosely coupled web applications. With a web application, many challenges also come into the picture as consequences. To be specific, some of these are state management, workflow and validations. The HTTP protocol’s stateless nature only make things more complex. The Spring web framework is designed to help us in these challenges.
9.6 Spring Integration
Spring Integration is a lightweight messaging solution that will add integration capabilities to your application. It is a lightweight messaging solution that will add integration capabilities to your Spring application. As a messaging strategy, it provides a way of sharing information quickly and with a high level of decoupling between involved components or applications.
9.7 Spring Boot
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications without the need of too much configuration. Spring Boot takes care of application infrastructure while you can focus on coding the actual business flesh. It makes reasonable assumptions of the dependencies and adds them accordingly. It also lets you customize the dependencies according to your requirement. Spring Boot has become very popular and it is used in many applications.
- What is Spring Boot
- Spring Boot Tutorial for beginners
- Spring Boot and JPA Example
- Spring Boot Configuration Tutorial
10. JDBC – JPA – Hibernate
Below is a list of libraries for interacting with Relational databases e.g. MySQL, Oracle etc.
10.1 JDBC
The purpose of JDBC is to make possible interaction with any database i.e. MySQL, Oracle etc in a generic way. This is to hide such details which are specific to the database vendor and expose a generic interface for client applications. As we saw in the previous section the Spring Framework also has support for JDBC.
- Jdbc Example For Beginners
- Spring JdbcTemplate Example
- Spring Transaction Management Example with JDBC Example
- You can download JDBC Tutorial
10.2 JPA
The Java Persistence API (JPA) is a vendor-independent specification for mapping Java objects to the tables of relational databases. Implementations of this specification allow application developers to abstract from the specific database product they are working with and allow them to implement CRUD (create, read, update and delete) operations such that the same code works on different database products. JPA has become the de-facto standard to write application code that interacts with databases.
- You can download JPA Minibook
- JPA Tutorial – The ULTIMATE Guide
10.3 Hibernate
Hibernate is a high-performance Object Relational Mapping (ORM) framework completely used in Java. Hibernate also provides query service along with persistence. This gives developers a way to map the object structures in Java classes to relational database tables. ORM framework eases to store the data from object instances into persistence data store and load that data back into the same object structure. ORM is actually a programming technique for converting data between relational databases and object oriented programming languages.
- Hibernate Tutorial For Beginners with Examples
- Hibernate Best Practices Tutorial
- You can download Hibernate Tutorial
11. Architecture
Choosing the best architecture in Software is essential. Sometimes multiple architectures and patterns can be combined in a single system, and fitting the perfect design into your solution can often feel like an art. The most common architectures are Monolith Multi Layered, SOA and Microservices.
11.1 Monolith Multi Layered
Multi Layered Architecture is an architectural model that propose the organization of the software components into different layers. Each of those layers is implemented as a physically separated container of software components.
11.2 SOA
SOA describes a set of patterns for creating loosely coupled, standards-based business-aligned services that, because of the separation of concerns between description, implementation, and binding, provide a new level of flexibility.
11.3 Microservices
Microservices allows you to take large application and decompose or break into easily manageable small components with narrowly defined responsibilities. The key point to embrace here is decomposing and unbundling the functionality.
- Microservice Architecture – A Quick Guide
- Microservices for Java Developers: Introduction
- Microservices for Java Developers: Microservices Communication
- Microservices for Java Developers: The Java / JVM Landscape
- Microservices for Java Developers: Monoglot or Polyglot?
- Microservices for Java Developers: Implementing microservices (synchronous, asynchronous, reactive, non-blocking)
- Microservices for Java Developers: Microservices and fallacies of the distributed computing
- Microservices for Java Developers: Managing Security and Secrets
- Microservices for Java Developers: Testing
- Microservices for Java Developers: Performance and Load Testing
- Microservices for Java Developers: Security Testing and Scanning
- Microservices Interview Questions and Answers
To select the best architecture check out the following comparisons:
12. Java 8+
The newest Java versions starting from Java 8 have new important features that Java developers should be aware of. Still the most used versions are Java 6-8.
12.1 Java 8
Java 8 is a revolutionary release as it includes a huge upgrade to the Java programming model and a coordinated evolution of the JVM. Java 8 supports functional programming via the lambda expression and Stream API and many other features.
- Java 8 Features Tutorial – The ULTIMATE Guide
- Java 8 Functional Programming Tutorial
- Java 8 Tutorials
- You can download Java 8 Features
12.2 Java 9
Java 9 was not as popular as Java 8, however, it has some new exciting features such as the Modules and the Java Shell tool (JShell) which is an interactive tool for learning the Java programming language and prototyping Java code.
12.3 Java 10-11
Java 10 and 11 are still not very popular has but they offer new features that will change the way we code and modularize our programs.
12.4 Latest versions of Java
- Java 11 New Features Tutorial
- How to download Java 12 for Windows
- The New Features in Java 13
- What is new in Java 14
- Java 15 New Features Tutorial
- Guide to Features Changes from Java 8 to Java 15
13. Desktop Java
Although Java is not very famous for developing desktop applications, it has a lot of technologies that can be used to create rich client applications and applets that are fast, secure, and portable. The best technologies are Swing, AWT and JavaFX.
13.1 Swing
The Swing API provides a comprehensive set of GUI components and services which enables the development of commercial-quality desktop and Internet/Intranet applications.
- Java Swing Tutorial for Beginner
- JAVA Swing Application Example
- How to Create a Gui in Java With Swing
13.2 AWT
The AWT (Abstract Window Toolkit) features the core foundation of the Java SE desktop libraries. It includes a robust event-handling model; graphics and imaging tools including shape, color, and font classes; layout managers for flexible window layouts; data transfer classes (including drag and drop) that allow cut and paste through the native platform clipboard.
13.4 JavaFX
JavaFX is a software platform for creating and delivering desktop applications, as well as rich Internet applications that can run across a wide variety of devices. JavaFX is intended to replace Swing as the standard GUI library for Java SE, but both will be included for the foreseeable future. JavaFX has support for desktop computers and web browsers on Microsoft Windows, Linux, and macOS.
14. Testing
Software testing has become more and more popular due to the increased size of the codebase of programs. It is of vital importance to write as many tests as possible and cover all the functionalities of your program. Testing can be divided into two main categories: unit testing and automation testing.
14.1 JUnit
Java has its own library for unit testing, the JUnit library. A unit can be a function, a class, a package, or a subsystem. So, the term unit testing refers to the practice of testing such small units of your code, so as to ensure that they work as expected. For more info about JUnit read the following:
- JUnit Tutorial for Unit Testing – The ULTIMATE Guide
- You can download JUnit Tutorial
14.2 Automation
With automation testing, the developer (or tester) write scripts which are used to automate the testing of the software end to end. Selenium and Cucumber are the most famous automation testing frameworks for Java. Check them out:
- Selenium Automation Testing Tutorial
- You can download Selenium Programming Cookbook
- JUnit Cucumber Example
15. Logging
Logging refers to the recording of activity. Logging is a common issue for development teams. Several frameworks ease and standardize the process of logging for the Java platform. The most commonly used logging frameworks for Java are Log4j and Logback. Logback is intended as a successor to the Log4j project due to the end of the support of the Log4j project. Apache Log4j2 is an upgrade to Log4j that provides significant improvements over Log4j and provides many of the improvements available in Logback while fixing some inherent problems in Logback’s architecture.
15.1 Logback
Logback is one of the most widely used logging frameworks in the Java community. It offers a faster implementation than Log4j, provides more options for configuration, and more flexibility in archiving old log files.
15.2 Log4j
Log4j2 is the updated version of the popular and influential Log4j library, which is a simple, flexible, and fast Java-based logging framework. It is thread-safe and supports internationalization.
16. Interview Questions
To prove your skills as a Java Developer in an interview you will be asked many questions that are related to Core Java, Multithreading, OOPs, Collections, Spring Framework, SQL. A summary of the most important questions that are asked in interviews:
- You can download Java Interview Questions
- 150 Java Interview Questions and Answers – The ULTIMATE List
- 100 Multithreading and Concurrency Interview Questions and Answers – The ULTIMATE List
- 40 Java Collections Interview Questions and Answers
- 100 Spring Interview Questions and Answers – The ULTIMATE List
- SQL Interview Questions and Answers – The ULTIMATE List
17. Summary
In this post, we took a look at the online resources to learn Java. We started with the installation of Java and an IDE and then we moved on to Core Java, Data Types and Java Collections. We saw the importance of learning algorithms, concurrency and design patterns. Then we covered the Spring Framework, JDBC, JPA and Hibernate. We took a look at architectural patterns, the newest Java versions, desktop Java, testing, logging and finally some very important Java Interview Questions.
18. More Tutorials to Learn Java Online
- Java Constructor Example
- Java List Example
- Java Stack Example
- Printf Java Example
- String CompareTo Java Example
- Java random number generator Example
- Convert int to string Java Example
- String to Int Java Example
- Polymorphism Java Example
- Java API Tutorial
- Online Java Compiler – What options are there
- How to Check Java version in Windows, Linux, MacOS
Last updated on Apr. 16th, 2021
Outstanding contribution. Thanks, you are saving lives.
Thank you Angelo, I’m glad that you found it helpful.