Sun Microsystems developed the original specifications for the language in the mid 1990s. Patrick Naughton, Mike Sheridan, and James Gosling were the original inventors of Java and the language was called Oak at the beginning.
Java is a full-fledged object-oriented programming language. It is platform independent and is normally interpreted rather than compiled like C/C++. It is syntactically and structurally modeled after C/C++ and performs various compile-time and run-time checking operations. Java performs automatic memory management that helps to greatly reduce the problem of memory leaks found in other languages and libraries that dynamically allocate memory.
Java supports many features that, at its time of conception, were not found directly in other languages. These features include threading, networking, security, and Graphical User Interface (GUI) development. Other languages could be used to support these capabilities, but they were not integrated in the language to the extent that it was done with Java.
Java uses an independent bytecode that is architecture neutral. That is, it is designed to be machine independent. The byte codes are interpreted and executed by a Java Virtual Machine (JVM). All of its primitive data types are fully specified, as we will see in Chapter 3, Decision Constructs. The various releases of the Java Development Kit (JDK) and other significant moments are depicted in the following timeline diagram:
Let's digress for a moment and consider why we are using Java at all. One of the most significant aspects of Java is that it is an object-oriented (OO) language. OO technologies are a popular paradigm for developing applications. This approach models an application around a series of real world objects, such as an employee or a ship. In order to solve a problem, it is useful to think of the real world objects that make up the problem domain.
The OO approach is based on three distinct activities:
Object Oriented Analysis (OOA ): This is concerned with determining the functionality of the system, that is, what should the application do
Object Oriented Design (OOD): This is concerned with how the architecture supports the functionality of the application
Object Oriented Programming (OOP): This is concerned with the actual implementation of the application
The products of the analysis and design steps are often referred to as analysis and design artifacts. While there may be several different types produced, the one of most interest to the OOP step is called the class diagram
. The following diagram shows a partial class UML diagram depicting two classes: Customer
and CustomerDriver
. In the A simple Java application section, we will examine the code for these classes. The Unified Modeling Language (UML) is a widely used OO technique used to design and document an application. A class diagram is one of the end products of the technique and is used by programmers to create the application:
Each box represents a class and is divided into three sections:
The first section at the top of the box is the name of the class
The second section lists the variables that make up the class
The last section lists the class methods
The symbols preceding the variable and method names specify the visibility of these class members. The following are the class diagram symbols used:
-
: Private+
: Public#
: Protected (used with inheritance)
Normally, a class diagram consists of many classes and is interconnected with annotated lines showing the relationship between the classes.
The class diagram is intended to clearly show what objects make up the system and how they interact. Once a class diagram is complete it can be implemented using an OOP language such as Java.
Note
The object-oriented approach is typically used for medium-scale to large-scale projects, where many developers must communicate, and work together, to create an application. For smaller projects involving only a few programmers, such as the one dealt with in most programming classes, the object-oriented approach is not normally used.
While there is some disagreement in what actually makes a programming language an OOP programming language, there are generally three underlying principles that must be supported by an OOP language:
Data encapsulation
Inheritance
Polymorphism
Data encapsulation is concerned with hiding irrelevant information from the users of a class and exposing the relevant. The primary purpose of data encapsulation is to reduce the level of software development complexity. By hiding the details of what is needed to perform an operation, the use of that operation is simpler. How to achieve data encapsulation in Java is explained in the Access modifiers section, later in this chapter.
Data encapsulation is also used to protect the internal state of an object. By hiding the variables that represent the state of an object, modifications to the object are controlled through the methods. Any changes to the state are verified by the code in the methods. Also, by hiding variables, sharing of information between classes is eliminated. This reduces the amount of coupling possible in an application.
Inheritance describes the relationship between two classes such that one class re-uses the capabilities of another class. This enables the re-use of software resulting in a more productive developer. Inheritance is covered in detail in Chapter 7, Inheritance and Polymorphism.
The third principle is polymorphism and its primary concern is to make the application more maintainable and extendable polymorphism behavior is where the behavior of one or identical methods is dependent upon the object it is executing against. For example, a person
object and a square
object can both have a draw
method. What it draws depends on the object the method is executed against. Polymorphism is discussed in Chapter 7, Inheritance and Polymorphism.
These principles are summarized in the following table:
Principle |
What it is |
Why we use it |
How to do it |
---|---|---|---|
Technique that hides information from the users of that class |
To reduce the level of software development complexity |
Use access modifiers such as | |
Technique to allow a derived or child class to use parts of a base or parent class |
To promote the re-use of the software |
Use the | |
Technique which supports different behavior of methods that is dependent on the object the method is executing against |
To make an application more maintainable |
Inherent to the Java language |
The implements
keyword is used in support of polymorphic behavior as is explained in Chapter 7, Inheritance and Polymorphism.
There are several types of Java applications. These types have allowed Java to flourish in a number of different areas and contributed to Java becoming a very popular programming language. Java is used to develop the following:
Console and window applications
Server-based web applications supported by Servlets, JSPs, JSF, and other JEE standards
Applets that execute within a browser
Embedded applications
Componentized building blocks using JavaBeans
While a basic understanding of the types of Java applications is useful in putting Java into context, it also helps to be able to recognize the basic code for these applications. You may not completely understand all of the ins and outs of these application types, but it is useful to see simple code examples.
Reading the code goes a long way towards understanding a language and a specific program. Throughout the book we will use numerous examples to illustrate and explain various aspects of Java. The basic types of Java applications are shown below by presenting short code snippets that are central to that application type.
A simple console application consists of a single class with a main
method, as shown in the following code snippet:
public class Application { public static void main(String[] args) { // Body of method } }
We will examine this type of application in more depth.
Applets
are normally embedded within an HTML page and offer a means of achieving client-side execution of a code. It does not have a main
method but uses a series of callback methods used by the browser to manage the application. The following code provides an idea of the general appearance of an applet:
import java.applet.*; import java.awt.Graphics; public class SimpleApplet extends Applet { @Override public void init() { // Initialization code } @Override public void paint( Graphics g ) { // Display graphics } }
The @Override
annotation is used to ensure that the method that follows is actually overridden. This is discussed in more detail in the Annotations section of this chapter.
A servlet
is a server-side application which renders an HTML page sent to a client. A doGet
or doPut
method responds to client-side request. The out
variable in the following example represents the HTML page. The println
methods are used to write the HTML code, as shown in the following code snippet:
class Application extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); // then get the writer and write the response data PrintWriter out = res.getWriter(); out.println( "<HEAD><TITLE> Simple Servlet</TITLE></HEAD><BODY>"); out.println("<h1> Hello World! </h1>"); out.println( "<P>This is output is from a Simple Servlet."); out.println("</BODY>"); out.close(); } }
A JavaServer Page (JSP) is actually a disguised Servlet. It provides a more convenient way of developing web pages. The following example uses a JavaBean to display "Hello World" on the web page. The JavaBean is detailed in the following example:
<html> <head> <title>A Simple JSP Page</title> </head> <body> Hello World!<br/> <% // This is a scriptlet that can contain Java code %> <hr> <jsp:useBean id="namebean" class="packt.NameBean" scope="session" > <jsp:setProperty name="namebean" property="name" value=" Hello world"" /> </jsp:useBean> <h1> <jsp:getProperty name="namebean" property="name" /></h1> </body> </html>
JavaBeans are building blocks for shared application functionality. They are frequently designed to be used in multiple applications and follow a standard naming convention. The following is a simple JavaBean designed to hold a name (it was used in the previous JSP page):
package packt; public class NameBean { private String name= "Default Name""; public String getName() { return this.name; } public void setName(String name) { this.name = name; } }
Enterprise Java Beans (EJB) are components designed to be used in a client/server configuration from a web server. This is a fairly specialized topic that is not relevant to the associate level of certification.
There are several other types of Java technologies such as JSF and Facelets that are a part of JEE. These are improvements over the older Servlet and JSP technologies used to develop web pages.
In this book we will only use simple Java console applications. This type of application is more than sufficient to explain the essence of Java.