Learning Modern Java: A Tutorial Using Java 8
A Fast-Moving Guide to Java Programming for Experienced Developers

Interested in training from the author of these tutorials? See the upcoming Maryland courses on Java 8 programming (for those new to Java) and Java 8 lambdas and streams (for those that know earlier Java versions). Co-sponsored by Johns Hopkins Engineering for Professionals. Or, contact info@coreservlets.com for info on customized courses onsite at your location. See the Java training page for various options for the onsite courses.

Following is a series of tutorials on Java programming from coreservlets' lead instructor Marty Hall. Click on a section below to expand its content. Since each section includes exercises and exercise solutions, this can also be viewed as a self-paced Java training course. All the slides, source code, exercises, and exercise solutions are free for unrestricted use. Although these tutorials do not assume any previous exposure to Java, they do assume that you already know some high-level programming language; they move too fast for first-time programmers. To arrange a Java-related course at your organization based on these materials, contact info@coreservlets.com or see the Java training page.

Overview of the Java Programming Tutorial Series

This tutorial uses the latest version of Java (Java 8) throughout, covers modern syntax and approaches, and focuses primarily on those parts of Java that apply to all application areas: server-side apps, phone apps, and desktop apps. As a result, it only briefly skims desktop graphics, but spends a lot of time on more general topics like object-oriented programming, data structures like Lists and Maps, generic types, lambda expressions, streams, concurrent and parallel programming, file I/O, network programming, and so forth. The relatively few parts on IDE development and deployment use Eclipse, but of course none of the actual code is specific to Eclipse.

These fast-moving tutorials are derived from Marty Hall's world-renowned live JSF 2, PrimeFaces, Hadoop, Ajax, JavaScript, jQuery, Android, and Java EE training courses, and have been tested by Marty in live training courses in the US, Canada, Australia, Cambodia, Puerto Rico, Japan, Mexico, India, Norway, and the Philippines. Click on a topic below to get the detailed tutorial, download the source code, or try out exercises on the topic. The training materials home page has tutorials on many more advanced topics related to Java and Web programming, but the core Java language is the foundation for all of the topics.

The key to learning is practicing, so each section contains exercises for you to try on your own. The exercise solutions are also included, but you will almost certainly learn better if you try some or all of the exercises on your own before peeking at our solutions. The later sections have exercises only for the main target audience of these tutorials: those with at least moderate previous experience in other languages. But, some of the earlier sections also have easier or slower moving exercises for those that have little previous programming experience.

Because Java 8 introduces such major changes, there is also a separate tutorial on lambdas and streams in Java 8. That tutorial, unlike this one, is aimed at people that already know Java, and it only covers the new Java 8 features. For general Java programming, the tutorial here is the one you want, all sections have been updated to use Java 8, and all the Java-8-specific topics are also included in identical form in this tutorial.

To arrange a Java-related course at your organization based on these materials, contact info@coreservlets.com or see the Java training page. These courses can be customized to use any combination of the materials on the J2EE tutorials site (Java 7, Java 8, JSF 2, PrimeFaces, Spring MVC, Android development, Hadoop, GWT, Ajax, JavaScript, jQuery, Ext/JS, Spring, Hibernate, RESTful Web Services, servlets, JSP, etc.), and new materials can be added for specific client needs. To learn more details about the instructor, the curriculum for the live training courses, or the public course schedule, please see http://courses.coreservlets.com.

If you find these free tutorials helpful, we would appreciate it if you would link to us. Send corrections or feedback on any tutorial to hall@coreservlets.com.

Intended Audience

These tutorials are primarily aimed at developers who have moderate to strong experince in some other programming language, but who are new to Java. These tutorials move too fast for first-time programmers. If you already know earlier Java versions and only want to learn the new Java 8 features, you can read the tutorial on lambdas and streams in Java 8. That tutorial is a subset of this one that focuses only on the new Java 8 capabilities.

Similarly, the the onsite Java training courses are aimed at organizations whose developers have experience with C++, C#, Objective C, or another high-level language, and want to get started with Java as quickly as possible. There is also a shorter version of these training courses that focus on the new Java 8 features and are intended for developers that know earlier Java versions. If either type of course interests you, please email hall@coreservlets.com to inquire about arranging a customized version at your organization.

Introduction to and Overview of Java

This section gives a big-picture view of what the Java language and various Java packages are all about. No syntax or code examples are covered. If you are familiar with what Java is all about, skip this section entirely and move on to the real programming topics.

Java 8 with Eclipse: Setup and Getting Started

This section shows how to install Java and the Eclipse development environment. It also walks you through running some pre-existing applications and making Eclipse Java projects. Although a few very simple examples are shown, they are not explained in any detail. Real syntax is explained starting in the following section.

Basic Java Syntax

This section gives a whirlwind intro to very basic Java syntax: loops, if statements, use of "main", strings, arrays, indenting, and so forth. Remember that this tutorial series is primarily intended for developers that have used another high-level language, so this section almost certainly moves too quickly for those without previous programming experience.

Object-Oriented Programming in Java: Basics

This is the first of three sections that cover OOP in Java. This section covers instance variables, methods, and constructors. These tutorial sections are primarily aimed at developers who are already familiar with at least one other language, but don't know Java syntax. So, if you have never seen object-oriented programming at all before, this section might move too quickly for you.

Object-Oriented Programming in Java: More Capabilities

This is the second of three sections that cover OOP in Java. It discusses overloading, the use of private instance variables teamed with accessor (getter/setter) methods, JavaDoc, inheritance, packages, and the toString method.

Object-Oriented Programming in Java: Advanced Capabilities

This is the last of three sections that cover OOP in Java. It discusses abstract classes, interfaces, @Override, visibility modifiers, and enums in detail. Interfaces are discussed from the perspective of Java 8, where they can have concrete (default) and static methods as well as abstract methods. This section also briefly discusses JavaDoc options and the classpath.

Simple Java Graphics

This section discusses simple popup windows and basic drawing operations. More importantly, it also serves as an excuse to introduce or illustrate general concepts that are used throughout Java programming: import statements, inheritance in action, lifecycle methods, the @Override annotation, and try/catch blocks.

Lists, Maps, and Sets

This section discusses lists (ordered collections somewhat similar to arrays except that they stretch as you add entries), maps (associative arrays -- things that match a key to a value), and sets (unordered collections of elements where you can later simply ask if the element exists or not).

Generic Types, printf, and Miscellaneous Utilities

This section explains how to support generic types in your own methods and classes. It also discusses printf, varargs, and the performance of String vs. StringBuilder.

Unit Testing with JUnit

This section gives a very fast introduction to using the JUnit framework to test your Java programs.

Asynchronous Event Handling

This section looks at asynchronous event handling: e.g., responding to button clicks. It uses that as an excuse to examine four options that will be seen again with GUI widget event handling, array sorting, and multithreaded programming: separate classes, interfaces, named inner classes, and anonymous inner classes. It also gives a brief preview of Java 8 lambdas, which are covered detail later.

Concurrent Programming with Java Threads

This section discusses how (and why) to use Java's multithreaded programming capabilities to perform concurrent processing. It also compares and contrasts concurrent programming with Java threads (this topic) with parallel programming using the Java fork/join framework (the next topic). This section also explains how to deal with race conditions, the bane of the multithreaded developer.

Parallel Programming with the Fork/Join Framework

This section discusses how (and why) to use Java's fork/join framework to perform parallel processing. It also compares and contrasts concurrent programming with Java threads (the previous topic) with parallel programming using the Java fork/join framework (this topic).

Important: the majority of fork/join applications can be done in Java 8 using parallel streams with a fraction of the effort. In Java 8, only very advanced developers need to use fork/join explicitly. So, if you are using Java 7: read on. If you are using Java 8: skip this section and concentrate on parallel streams instead. But neither fork/join nor parallel streams obviate the need to learn concurrent programming with threads, as was covered in the previous lecture.

Topics Specific to Java 8

Originally, the Java 8 topics were available only separately as part of the Java 8 lambdas and streams tutorial. However, the final version of Java SE 8 was released in March 2014, Java 8 is now mainstream, and the formerly separate topics are now integrated into this general Java tutorial. The Java 8 tutorial is still being maintained for those already familiar with earlier Java versions, but the exact same topics are reproduced in identical form below.

Although Java 8 is now a standard part of Java, it is by far the most significant upgrade to the Java programming language since at least 2004, probably since Java's inception, and the features are particularly useful for those doing enterprise and server-side Java, or those dealing with large data sets. In general, Java 8's high-level goals were to make more flexible code, to better use multiple cores, and to more easily deal with large data sets. In specific, there are four main reasons that it is important to know Java 8:

  1. More flexible and reusable code (thanks to lambdas). Lambda expressions in Java 8 are a way of representing "functions", and their judicious use can make your code significantly more adapatable and reusable.
  2. Convenience (thanks to high-level Stream methods). Streams are wrappers around collections (or other data sources) that use lambda expressions pervasively. They support many convenient and high-performance operations that use lambdas, including "map", "reduce", "filter", and "forEach". These methods make many types of code much simpler to write, as compared to the comparatively clunky and low-level Collection methods.
  3. Faster and more memory efficient code (thanks to lazy evaluation and automatic parallelization). Streams support lazy evaluation, so if you map firstName over Employees, filter ones that start with "P", then choose the first, it really only maps and filters until the first match. Streams can also be parallel, so that the operations are automatically done in parallel, without any explicit multithreading code.
  4. Adapt to the times. Others will be using lambdas and streams, since they are standard parts of Java SE as of 3/2014. So, you have to learn the Java 8 features simply to be able to use and modify others’ code. Besides, once you get a taste of their power, you will want to use the new features frequently.

Java 8: Installation, Setup, and Review of Foundational Topics

Java 8 Lambda Expressions Part 1

Java 8 Lambda Expressions Part 2

Java 8 Lambda Expressions Part 3

Static & Default Methods in Java 8 Interfaces

Java 8 Lambda Expressions Part 4

Java 8 Streams Part 1

Java 8 Streams Part 2

Java 8 Streams Part 3

Lambda-Related Methods Directly in Lists and Maps

File I/O in Java 8 Part 1: Treating Files as Streams of Strings

File I/O in Java 8 Part 2: Using Lambdas and Generic Types to Make File-Reading Code More Flexible, Reusable, and Testable

Network Programming - Clients

This is the first of three sections on network programming in Java. In this section we cover the basics of creating client sockets to connect to existing servers, and we also take an aside to summarize the basic ways of formatting data to send over the socket (i.e., calling printf) and reading the results (i.e., using StringTokenizer and String.split on the result of readLine). The second networking section covers servers and multithreaded networking apps; the third section covers the use of ObjectInputStream and ObjectOutputStream to send complex data structures across the network.

Network Programming - Servers

This is the second of three sections on network programming in Java. The first section covers socket basics and clients. This section covers servers and multithreaded servers. The third section covers the use of ObjectInputStream and ObjectOutputStream to send complex data structures across the network.

Network Programming and Object Serialization

This is the last of three sections on network programming in Java. The first section covers socket basics and clients. The second section covers servers and multithreaded servers. This third section covers the use of ObjectInputStream and ObjectOutputStream to send complex data structures across the network.

Desktop Graphics in Java

Originally, the coreservlets.com Java tutorial series and associated training courses had several topics on windows and drawing for desktop Java applications. Over time, however, we saw that most tutorial users and developers in the training courses were applying Java to either server-side apps (the most common application area) or to Android apps (the second-most common application area). Desktop graphics in Java apply to neither of these application areas, so we stopped updating the graphics topics. These older graphics topics are included here for your reference, but please note that they have not been updated for several years, and are no longer being actively maintained.

Applets and Basic Graphics

This section discusses applets and basic drawing operations. This section is old, and it repeats most of what is covered in the earlier section on simple graphics (because simple graphics was presented in the context of applets in older versions of this tutorial). Skip this section completely unless you care specifically about browser-based applications.

AWT Components: Simple GUIs

This section introduces simple windows and GUI controls in Java. It is mostly a precursor to the later sections on Swing, so that you can move to Swing gradually by learning some of the basic concepts before diving into the larger and more complex (but better!) library.

Layout Managers

This section shows how to use layout managers to control the size and position of components in a window. It also discusses the design strategy of using nested windows.

Java 2D Drawing

This section discusses the rich 2D drawing library that is part of Java SE.

Basic Swing

This section looks at how the Swing library improves over the rudimentary AWT library. It assumes that you are familiar with the techniques discussed in the AWT section. This is a whirlwind intro to give you the feel of Swing; detailed coverage would require about 10 sections, but for that, please see http://docs.oracle.com/javase/tutorial/uiswing/.

Advanced Swing and MVC: Custom Data Models and Cell Renderers

This section shows how you can separate the underlying data from the actual graphical view. This MVC approach usually applies to GUI elements such as JList, JTree, and JTable that display complex sets of data.

Multithreaded Graphics and Animation

This section looks at techniques that let drawings change and persist over time. Double buffering and animation are the most important approaches in this category. Note: This section depends on the later section on multi-threaded programming. It comes here (earlier) because it is part of the general desktop-graphics category that is no longer being maintained and updated.

Out-of-Date Topics

Included here are some older topics that are no longer being updated. They are included here for your reference, but please note that they have not been updated for several years, and are no longer being actively maintained.

XML Parsing and DOM

This section covers the standard DOM API for parsing XML files and extracting data. This is higher level than the builtin SAX API, but is still pretty low level by today's standards. Note: This section is a bit out of date, and has not been updated in several years. It is no longer part of the usual courses that I teach.

Accessing Databases with JDBC

This section gives an introduction to the Java DataBase Connectivity (JDBC) library, the relatively simple but low-level database library that is built into Java SE.

Java Web Start and other Java Deployment Options

This section looks at making launchable JAR files and deploying Java apps via Web Start. Note: this section has not been updated in several years, and is undoubtedly out of date.

JNI: Invoking OS-Specific Programs and Native Methods

This section looks at using the Java Native Interface to call C functions. Note: this section has not been updated in several years, and is undoubtedly out of date.

XHTML and HTML5: A Crash Course

This gives a brief intro to XHTML with a smattering of HTML 5 and a super-fast summary of CSS selector syntax. Again, the HTML sections are not specific to Java, but basic knowledge of HTML is useful for practically everyone: in particular, both servlets and JSP and Ajax require pretty extensive knowledge of HTML of some sort, and JSF 2 is built entirely around XHTML. Also see the separate tutorial on HTML5 input elements.

HTML 4: A Crash Course

The HTML sections are not specific to Java, but basic knowledge of HTML is useful for practically everyone, and the tutorials on JSF 2 and PrimeFaces, servlets and JSP, and Ajax and jQuery require pretty extensive knowledge. Please note that the sections on HTML 4 are old: they not been updated in several years. Still, they might be useful for beginners.

Source Code

Source code for all examples in this tutorial as well as the exercise solutions can be downloaded from the Java sample code repository. Code is free for completely unrestricted use. Each section above also has links to the code used in that section.

Customized Onsite Training

Looking for short hands-on training courses on Java taught at your company? The courses are taught by Marty Hall, an experienced instructor, developer, speaker, and well-known Java EE author, and the creator of this tutorial series. For more information, see the Java training course page or contact Marty to arrange a course at your location. Courses are available in any country, and have been given in N. America, Central America, Europe, Asia, and Australia. Some course options:

  • Java Programming: A Crash Course for Server-Side Developers. A fast-moving course on Java for developers that are experienced with C#, C++, or another high-level language. The version for those that will be doing server-side Java omits most topics on desktop graphics. Can be taught with or without Java 8 (lambda expressions and streams).
  • Java Programming: A Crash Course. Same as above but adds topics on desktop graphics.
  • Java 8: Lambdas, Streams, and More. The general Java courses above can include Java 8, but this version is for those that already know Java and only want to learn the new Java 8 features.

Distinctive features of Marty's training:

  • Experienced instructor who can answer tough questions and show best practices.
    Marty Hall and James Gosling @ Marty's car

    Marty & Java inventor James Gosling at Marty's car.

    With the big training vendors, you don't even know the instructor's name before the class begins, much less their credentials. Marty is an experienced real-world developer, award-winning instructor, conference speaker on Java EE topics (including five times at JavaOne and twice as keynote speaker at the Great Indian Developer Summit), and author of six popular J2EE books from Prentice Hall. Core Servlets & JavaServer Pages is the most popular servlet and JSP book internationally, with translations in Bulgarian, Chinese (both traditional and simplified script), Czech, French, German, Hebrew, Japanese, Korean, Macedonian, Polish, Portuguese, Russian and Spanish. Marty is also the co-author (with David Geary and Cay Horstmann) of the upcoming JSF 2.2 edition of Core JSF, the leading JSF text worldwide. Marty also teaches in the Johns Hopkins University part-time graduate program in Computer Science, where he directs the Java and Web-related concentration area. Click here for more details.
  • Well-tested courses. Marty has taught J2EE training courses in Canada, Australia, Japan, Puerto Rico, Mexico, India, Cambodia, Norway, the Philippines, and dozens of US venues. Clients for on-site Java courses include Google, Symantec, Verisign, the NY Stock Exchange (SIAC), the Federal Reserve Bank, State Farm Insurance, General Motors, Hewlett Packard, Motorola, Telenor Norway, Lexmark International, TransCanada Pipelines, Raytheon, Learjet, Sikorsky, the Aerospace Corporation, Informatica India, Los Alamos National Labs, Genomica, the U.S. Navy, General Dynamics, Banco Popular de Puerto Rico, Titan Corporation, the University of Michigan, Context Integration, the University of Utah Hospital, ESI, SAIC, NSA, CIA, and many others. Click here for a longer list. No using your developers as guinea pigs!
  • Flexible schedules. Courses are available in any city worldwide, and have been taught in dozens of US venues and 9 other countries. However, Maryland-area companies have extra flexibility in the course schedules, since Marty is local to Maryland. In addition to the full-day courses offered in all locations, Maryland companies can book the courses in the late afternoons or early evenings. Even weekends are possible! For example, Marty has done about a dozen Maryland courses that met once or twice a week from 4:30-6:30 pm (or similar) so that the students did not lose billable hours.
  • Course materials developed by the instructor. Marty personally developed all of his own materials, much of it based on his best-selling books. They thoroughly cover the basics, introduce advanced topics, include many meaty code examples, and show plenty of live running applications. They also discuss best practices, design strategies, and efficiency issues. Most of the big training vendors hire someone to create the course materials, then bring in some inexperienced flunky to regurgitate them to the class.
  • Public (open-enrollment) training courses
    JHU/EP Logo Onsite courses are cheaper and more convenient for organizations that have medium to large sized groups of developers. But, in order to serve organizations with only one or two developers that need training, coreservlets.com periodically offers public versions of some of the most popular courses. The next public offerings will be on Java programming, new features in Java 8, Android programming, Web application development with JSF2, PrimeFaces, Big-Data Apps with Hadoop, Rich Internet Applications with Ajax (and jQuery), GWT programming, Hadoop, Java EE programming with Spring, Java persistence with Hibernate and JPA, and Web Services (RESTful and SOAP-based). The courses are co-sponsored by Johns Hopkins Engineering for Professionals and are held at JHU/Dorsey in Elkridge Maryland (5 minutes from BWI airport). Click here for details.
  • A variety of complementary course offerings.
    Promos for Marty Hall at GIDS conference in Bangalore

    Ads for Marty at GIDS conference in India

    Marty personally teaches the following courses (click for detailed course descriptions): In addition, Marty reviews/edits courses taught by colleagues. These courses are developed by experts who work daily with the technologies, and the materials are thoroughly reviewed by Marty. Course topics include:
  • Customizable content. Want a class on JSF 2 or Spring or Hadoop, but need some review of basic Java first? No problem. Want to move extra fast because your developers are experienced? Piece of cake. Need to combine some basic JSF topics with thorough coverage of PrimeFaces? Just say the word. Want an Ajax and jQuery course, but first need a crash course on JavaScript syntax, first-class functions, and the DOM? Simply let us know. Want training on GWT, but need a review of basic server-side Java techniques first? Tell us what you need. Every onsite course is fully customized to the needs and backgrounds of your developers.
  • Competitive prices. Marty's public training courses are typically at least 20% cheaper than the canned courses from the big training vendors. And his courses include a textbook, a bound student notebook, meals and snacks, exercises, and exercise solutions. Onsite courses at your location are even cheaper, since there are no venue rental costs. Plus, your developers don't have to pay travel costs and the content and pace is customizable.

Email for details.

PowerPoint Files for University Faculty

The PDF files in this tutorial contain the complete text of the original PowerPoint files, so if your goal is learning this technology, just stick with this tutorial. However, as a service to instructors teaching full-semester courses at accredited universities, coreservlets.com will release the original PowerPoint files for free. Please see the instructor materials page for details.

More Information


JSF (JavaServer Faces)

Servlets & JSP
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA