About the Author
Budi Kurniawan is a senior developer at Brainy Software and the author of How Tomcat Works, Servlet and JSP: A Tutorial, Struts 2 Design and Programming, and others. He has written software that is licensed by major corporations worldwide.
Introduction
Welcome to Java: A Beginners Tutorial, Fourth Edition .
This book covers the most important Java programming topics that you need to master in order to learn other technologies yourself. By fully understanding all the chapters and doing the exercises youll be able to perform an intermediate Java programmers daily tasks quite well.
This book offers all the three subjects that a professional Java programmer must be proficient in:
- Java programming language
- Object-oriented programming (OOP) with Java
- J ava core libraries
What makes structuring an effective Java course difficult is the fact that the three subjects are interdependent. On the one hand, Java is an OOP language, so its syntax is easier to learn if you already know OOP. On the other hand, OOP features such as inheritance, polymorphism and data encapsulation are best taught when accompanied by real-world examples. Unfortunately, understanding real-world Java programs requires knowledge of the Java core libraries.
Because of such interdependence, the three main topics are not grouped into three isolated parts. Instead, chapters discussing a major topic and chapters teaching another are interwoven. For example, before explaining polymorphism, this book makes sure that you are familiar with certain Java classes so that real-world examples can be given. In addition, because a language feature such as generics cannot be explained effectively without the comprehension of a certain set of classes, it is covered after the discussion of the supporting classes.
There are also cases whereby a topic can be found in two or more places. For instance, the for statement is a basic language feature that should be discussed in an early chapter. However, for can also be used to iterate over an array or an object collection. Therefore, for is first presented in Chapter 3, Statements and then revisited in Chapter 6, Arrays and Chapter 14, The Collections Framework.
The rest of this introduction presents a high-level overview of Java, an introduction to OOP and a brief description of each chapter.
Java, the Language and the Technology
Java is not only an object-oriented programming language, it is also a set of technologies that make software development more rapid and resulting applications more robust and secure. For years Java has been the technology of choice because of the benefits it offers:
- platform independence
- ease of use
- comprehensive libraries that speed up application development
- security
- scalability
- extensive industry support
Sun Microsystems introduced Java in 1995 and Javaeven though it had been a general-purpose language right from the startwas soon well known as the language for writing applets, small programs that run inside web browsers and add interactivity to static websites. The growth of the Internet had much to contribute to the early success of Java.
Having said that, applets were not the only factor that made Java shine. The other most appealing feature of Java was its platform-independence promise, hence the slogan Write Once, Run Anywhere. What this means is the very same program you write will run on Windows, Unix, Mac, Linux, and other operating systems. This was something no other programming language could do. At that time, C and C++ were the two most commonly used languages for developing serious applications. Java seemed to have stolen their thunder since its first birthday.
That was Java version 1.0.
In 1997, Java 1.1 was released, adding significant features such as a better event model, Java Beans, and internationalization to the original.
Java 1.2 was launched in December 1998. Three days after it was released, the version number was changed to 2, marking the beginning of a huge marketing campaign that started in 1999 to sell Java as the next generation technology. Java 2 was sold in four flavors: the Standard Edition (J2SE ), the Enterprise Edition (J2EE ), the Micro Edition (J2ME ), and Java Card (that never adopted 2 in its brand name).
The next version released in 2000 was 1.3, hence J2SE 1.3. 1.4 came two years later to make J2SE 1.4. J2SE version 1.5 was released in 2004. However, the name Java 2 version 1.5 was then changed to Java 5.
On November 13, 2006, a month before the official release date of Java 6, Sun Microsystems announced that it had open-sourced Java. Java SE 6 was the first Java release for which Sun Microsystems had invited outside developers to contribute code and help fix bugs. True that the company had in the past accepted contributions from non-employees, like the work of Doug Lea on multithreading, but this was the first time Sun had posted an open invitation. The company admitted that they had limited resources, and outside contributors would help them cross the finish line sooner.
In May 2007 Sun released its Java source code to the OpenJDK community as free software. IBM, Oracle and Apple later joined OpenJDK.
In 2010 Oracle acquired Sun.
Java 7 was released in July 2011 and Java 8 in March 2014, both results of open-source collaboration through OpenJDK.
What Makes Java Platform Independent?
You must have heard of the terms platform-independent or cross-platform , which means your program can run on multiple operating systems. It was a major feat that contributed to Javas popularity. But, what makes Java platform independent?
In traditional programming , source code is compiled to executable code . This executable code can run only on the platform it is intended to run. In other words, code written and compiled for Windows will only run on Windows, code written in Linux will only run on Linux, and so on. This is depicted in Figure I.1.
Figure I.1: Traditional programming paradigm
A Java program, on the other hand, is compiled to bytecode . You cannot run bytecode by itself because it is not native code. Bytecode can only run on a Java Virtual Machine (JVM). A JVM is a native application that interprets bytecode. By making the JVM available on many platforms, Sun transformed Java into a cross-platform language. As shown in Figure I.2, the very same bytecode can run on any operating system for which a JVM has been developed.
Figure I.2: Java programming model
Currently JVMs are available for Windows, Unix, Linux, Free BSD, and practically all other major operating systems in the world.
JDK, JRE, JVM, Whats the Difference?
I mentioned that Java programs must be compiled. In fact, any programming language needs a compiler to be really useful. A compiler is a program that converts program source code to an executable format, either a bytecode , native code or something else. Before you can start programming Java, you need to download a Java compiler. The Java compiler is a program named javac , which is short for Java compiler.
While javac can compile Java sources to bytecode, to run bytecode you need a Java Virtual Machine. In addition, because you will invariably use classes in the Java core libraries, you also need to download these libraries. The Java Runtime Environment (JRE) contains both a JVM and class libraries. As you may suspect, the JRE for Windows is different from that for Linux, which is different from the one for yet another operating system.
Next page