Java Programming Tutorial
A Fast-Moving Guide to Java Programming for Experienced Developers

Interested in training from the author of these tutorials? See the upcoming Java 7 and Java 8 programming courses in Maryland (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 an extensive series of tutorials on Java programming from coreservlets' lead instructor Marty Hall. 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. Click on a section below to expand its content. The relatively few parts on IDE development and deployment use Eclipse, but of course none of the actual code is Eclipse-specific. These tutorials 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

These fast-moving tutorials are derived from Marty Hall's world-renowned live JSF 2, PrimeFaces, Hadoop, Ajax, 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. Update: based on student feedback, we have added more simple/warmup exercises to each section below (and added the corresponding code to the downloadable solution sets).

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.

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, Android development, Hadoop, GWT, Ajax, 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 aimed at developers who have moderate to strong experince in some other programming language, but who are new to Java. These tutorials definitely move too fast for first-time programmers.

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.

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 is covered.

Java 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, and no real syntax is yet covered.

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.

Basic Object-Oriented Programming in Java

This is the first of two 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 the concepts of OOP from other languages, but don't know the Java syntax. So, if you have never seen object-oriented programming at all before, this section probably moves too quickly for you.

Object-Oriented Programming in Java: More Capabilities

This is the second of two sections that cover OOP in Java. It discusses overloading, the use of private instance variables teamed with accessor (getter/setter) methods, JavaDoc, and inheritance. It also very briefly summarizes some more advanced OOP concepts.

Applets and Basic Graphics

This section discusses applets and basic drawing operations. But, it also serves as an excuse to introduce three general concepts that are used throughout Java programming: import statements, lifecycle methods, the @Override annotation, and try/catch blocks.

More Java Syntax and Utilities

This section discusses lists (like arrays except that they stretch as you add entries), maps (associative arrays -- things that match a key to a value), generic types, printf, varargs, and the performance of String vs. StringBuilder. This is the most "computer-sciency" of any of the tutorial sections, but the performance issues are important for real-life Java programming.

Basic File IO with the NIO Package

This section discusses covers simple ways to read and write files. Uses the Java 7 nio package as well as Java-7-specific try/catch blocks, so these examples work in Java 7 and 8, but are not portable to Java 6 and earlier. Also gives a lot more details on try/catch blocks, which were introduced in the last lecture. In particular, it covers finally blocks, multicatch, and try with resources.

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 in the tutorial on lambda expressions and streams in Java 8.

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 you can move there 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 Programming

This section discusses how (and why) to use Java's multithreaded programming capabilities to perform concurrent processing. It also explains how to deal with race conditions, the bane of the multithreaded developer.

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 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.

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.

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.

Lambdas and Streams in Java 8

Because Java 8 introduces such major changes, we made a separate Java 8 tutorial covering lambda expressions and streams. 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.

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.

Introduction to HTML Frames

Again, 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.

  • This tutorial section in PDF.
  • Topics covered:
    • Advantages and disadvantages of frames
    • FRAME template
    • Defining rows and cols in a FRAMESET
    • Common FRAME and FRAMESET attributes
    • Nested frames
    • Targeting a document to a named FRAME cell

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 servlets and JSP (including five times at JavaOne), and author of six popular J2EE books from Prentice Hall and Sun Microsystems Press. 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 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 Symantec, VeriSign, the NY Stock Exchange (SIAC), State Farm Insurance, General Motors, Hewlett Packard, Lexmark International, TransCanada Pipelines, Raytheon, Learjet, Sikorsky, Los Alamos National Labs, Genomica, the U.S. Navy, General Dynamics, Banco Popular, 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 7 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 public courses are co-sponsored by Johns Hopkins Engineering for Professionals and will be 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 10% cheaper than the canned courses from the big training vendors. And his courses include a textbook, a bound student notebook, exercises, exercise solutions, and all necessary software (for onsite courses). 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.

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

Java

JSF (JavaServer Faces)

Servlets & JSP
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA

Struts