Like any human language, Java provides a way to express concepts. If successful, this medium of expression will be significantly easier and more flexible than the alternatives as problems grow larger and more complex.
You can’t look at Java as just a collection of features – some of the features make no sense in isolation. You can use the sum of the parts only if you are thinking about design, not simply coding. And to understand Java in this way, you must understand the problems with it and with programming in general. This tutorial discusses programming problems, why they are problems, and the approach Java has taken to solve them.
Prerequisites:
This tutorial assumes that you have some programming familiarity. Of course, the book will be easier for the C programmers and more so for the C++ programmers. I’ll be introducing the concepts of object-oriented programming (OOP) and Java’s basic control mechanisms.
Chapters:
Here is a brief description of the chapters contained in the tutorial.
Chapter 1: Introduction to Objects
This chapter is an overview of what object-oriented programming is all about, including the answer to the basic question “What’s an object?”, interface vs. implementation, abstraction and encapsulation, messages and functions, inheritance and composition, and the all-important polymorphism. You’ll also get an overview of issues of object creation such as constructors, where the objects live, where to put them once they’re created, and the magical garbage collector that cleans up the objects that are no longer needed.
Chapter 2: Everything is an Object
This chapter moves you to the point where you can write your first Java program, so it must give an overview of the essentials, including the concept of a reference to an object; how to create an object; an introduction to primitive types and arrays; scoping and the way objects are destroyed by the garbage collector; how everything in Java is a new data type (class) and how to create your own classes; functions, arguments, and return values; name visibility and using components from other libraries; the static keyword; and comments and embedded documentation.
Chapter 3: Controlling Program Flow
This chapter begins with all of the operators that come to Java from C and C++. In addition, you’ll discover common operator pitfalls, casting, promotion, and precedence. This is followed by the basic control-flow and selection operations that you get with virtually any programming language: choice with if-else; looping with for and while; quitting a loop with break and continue as well as Java’s labeled break and labeled continue (which account for the “missing goto” in Java); and selection using switch. Although much of this material has common threads with C and C++ code, there are some differences.
Chapter 4: Initialization & Cleanup
This chapter begins by introducing the constructor, which guarantees proper initialization. The definition of the constructor leads into the concept of function overloading (since you might want several constructors). This is followed by a discussion of the process of cleanup, which is not always as simple as it seems. Normally, you just drop an object when you’re done with it and the garbage collector eventually comes along and releases the memory. This portion explores the garbage collector and some of its idiosyncrasies. The chapter concludes with a closer look at how things are initialized: automatic member initialization, specifying member initialization, the order of initialization, static initialization and array initialization.
Chapter 5: Hiding the Implementation
This chapter covers the way that code is packaged together, and why some parts of a library are exposed while other parts are hidden. It begins by looking at the package and import keywords, which perform file-level packaging and allow you to build libraries of classes. It then examines subject of directory paths and file names. The remainder of the chapter looks at the public, private, and protected keywords, the concept of “friendly” access, and what the different levels of access control mean when used in various contexts.
Chapter 6: Reusing Classes
The concept of inheritance is standard in virtually all OOP languages. It’s a way to take an existing class and add to its functionality (as well as change it, the subject of Chapter 7). Inheritance is often a way to reuse code by leaving the “base class” the same, and just patching things here and there to produce what you want. However, inheritance isn’t the only way to make new classes from existing ones. You can also embed an object inside your new class with composition. In this chapter you’ll learn about these two ways to reuse code in Java, and how to apply them.
Chapter 7: Polymorphism
On your own, you might take nine months to discover and understand polymorphism, a cornerstone of OOP. Through small, simple examples you’ll see how to create a family of types with inheritance and manipulate objects in that family through their common base class. Java’s polymorphism allows you to treat all objects in this family generically, which means the bulk of your code doesn’t rely on specific type information. This makes your programs extensible, so building programs and code maintenance is easier and cheaper.
Chapter 8: Interfaces & Inner Classes
Java provides a third way to set up a reuse relationship, through the interface, which is a pure abstraction of the interface of an object. The interface is more than just an abstract class taken to the extreme, since it allows you to perform a variation on C++’s “multiple inheritance,” by creating a class that can be upcast to more than one base type. At first, inner classes look like a simple code hiding mechanism: you place classes inside other classes. You’ll learn, however, that the inner class does more than that—it knows about and can communicate with the surrounding class—and that the kind of code you can write with inner classes is more elegant and clear, although it is a new concept to most and takes some time to become comfortable with design using inner classes.
Chapter 9: Holding your Objects
It’s a fairly simple program that has only a fixed quantity of objects with known lifetimes. In general, your programs will always be creating new objects at a variety of times that will be known only while the program is running. In addition, you won’t know until run-time the quantity or even the exact type of the objects you need. To solve the general programming problem, you need to create any number of objects, anytime, anywhere. This chapter explores in depth the container library that Java 2 supplies to hold objects while you’re working with them: the simple arrays and more sophisticated containers (data structures) such as ArrayList and HashMap
Chapter 10: Error Handling with Exceptions
The basic philosophy of Java is that badly-formed code will not be run. As much as possible, the compiler catches problems, but sometimes the problems—either programmer error or a natural error condition that occurs as part of the normal execution of the program—can be detected and dealt with only at run-time. Java has exception handling to deal with any problems that arise while the program is running. This chapter examines how the keywords try, catch, throw, throws, and finally work in Java; when you should throw exceptions and what to do when you catch them.
Chapter 11: The Java I/O System
Theoretically, you can divide any program into three parts: input, process, and output. This implies that I/O (input/output) is an important part of the equation. In this chapter you’ll learn about the different classes that Java provides for reading and writing files, blocks of memory, and the console. The distinction between “old” I/O and “new” Java I/O will be shown
Chapter 12: Run-Time Type Identification
Java run-time type identification (RTTI) lets you find the exact type of an object when you have a reference to only the base type. Normally, you’ll want to intentionally ignore the exact type of an object and let Java’s dynamic binding mechanism (polymorphism) implement the correct behavior for that type. But occasionally it is very helpful to know the exact type of an object for which you have only a base reference. Often this information allows you to perform a special-case operation more efficiently. This chapter explains what RTTI is for, how to use it, and how to get rid of it when it doesn’t belong there.
Chapter 13: Creating Windows and Applets
Java comes with the “Swing” GUI library, which is a set of classes that handle windowing in a portable fashion. These windowed programs can either be applets or stand-alone applications. This chapter is an introduction to Swing and the creation of World Wide Web applets. The important “JavaBeans” technology is introduced.
Chapter 14: Multiple Threads
Java provides a built-in facility to support multiple concurrent subtasks, called threads, running within a single program. Although these can be used anywhere, threads are most apparent when trying to create a responsive user interface so, for example, a user isn’t prevented from pressing a button or entering data while some processing is going on. This chapter looks at the syntax and semantics of multithreading in Java.
Chapter 15: Distributed Computing
All the Java features and libraries seem to really come together when you start writing programs to work across networks. This chapter explores communication across networks and the Internet, and the classes that Java provides to make this easier. It introduces the very important concepts of Servlets and JSPs (for server-side programming), along with Java DataBase Connectivity (JDBC), and Remote Method Invocation (RMI). Finally, there’s an introduction to the new technologies of JINI, JavaSpaces, and Enterprise JavaBeans (EJBs).
January 5, 2007 at 10:43 am
[...] (more…) Posted in Array [...]
April 4, 2007 at 6:50 am
Nice stuff.. I am learning java as well ..
My site – Bangladesh Technology
Portal
October 8, 2007 at 5:33 am
I really liked ur post, thanx for sharing. Keep writing. I discovered a good site for bloggers check out this http://www.blogadda.com, you can submit your blog there, you can get more auidence.
October 14, 2008 at 6:10 pm
Hi,
My name is Sandi and I’m working in association with Thumbplay.com, a leading online platform of mobile entertainment content that offers ringtones, wallpapers, games and text-based services through a membership-based, community experience.
I feel the information on your site you provide is very useful. I would love to know if you could link to our ringtone resource.
Best regards,
Sandi