Learning Objectives
After completing this session, you will be able to:
Explain the history of Java
Describe the features or characteristics of Java
Explain applets
Describe Java Virtual Machine (JVM) and bytecode
Explain Java Runtime Environment (JRE) and Java Development Kit (JDK)
History of Java
The original name of Java was Oak, and it was developed as a part of the Green project at Sun Microsystems.
Java was conceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridon at Sun Microsystems in 1991.
The ultimate objectives of the team were:
To ensure security
To ensure reliability
To set up a platform independent language completely that would function seamlessly, regardless of the CPU
The World Wide Web (WWW), by nature, had requirements such as reliability, security, and architecture independence, which were fully compatible with the design patterns of Java.
Sun formally announced the Java SunWorld in 1995.
Java Technology
Java is a programming language.
Java is a development environment that provides tools such as compiler, interpreter, documentation generator, and so on.
Java is an application environment to run standalone programs that run on any machine where the Java Runtime Environment (JRE) is installed.
Java is a deployment environment that supplies Java2 Software Development Kit (J2SDK) with complete set of Application Programming Interface (APIs) as packages.
Java provides an easy-to-use language by avoiding pitfalls of other languages, such as pointer arithmetic and memory management, which affect the robustness of the code.
Features or Characteristics of Java The features of Java are:
Simple
Object oriented
Secure
Platform independent
Robust
Portable
Automatic garbage collection
Dynamic
Multithreaded
Distributed Applets
An applet is a program written in the Java programming language that can be included in a HTML page, much in the same way an image is included in a page.
An applet is designed to be transmitted over the Internet and is executed by a Java compatible Web browser.
An applet can react to user input dynamically.
Because applets are executed in the client machine, Java enforces a lot of restriction on applets.
Overview of Java Virtual Machine
Java Virtual Machine (JVM) specification defines the JVM as an imaginary (virtual) machine that is implemented by emulating it in software on a real machine. Code for the JVM is stored in .class files, each of which contains code for at most one public class.
The specification enables the Java language to be platform independent.
JVM is implemented in a Java technology development tool or in a Web browser.
Bytecode: Bytecode is a highly optimized set of instructions designed to be executed by the JVM.
The key that allows Java to solve both the security and the portability problem is the output of Java compiler, which is not an executable code rather it is the bytecode.
The JVM is ported to different platforms to provide hardware and operating system independence, which is environment independent of hardware and operating system.
JVM needs to be implemented for each platform.
Java SE or J2SE Runtime Environment
J2SE stands for Java 2 Standard Edition. From Java SDK API version 1.5 onwards, this is referred to as Java SE (Java Standard Edition).
The JRE provides the libraries, JVM, Java Interpreter, and other components necessarily for you to run applets and applications written in Java.
JRE is responsible for loading, verifying, and executing the bytecodes.
The JDK includes the JRE as well as command-line development tools, such as compilers and debuggers that are necessary or useful for developing applets and applications.
The Java API is a code that is written earlier, organized into packages of similar topics, and it is a part of JDK libraries.
Tips and Tricks
What is a Java program? What do you actually deliver?
Solution: A Java program is a pile of classes (or at least one class). In a Java application, one of the classes must have a main method, used to start-up the program. So as a programmer, you write one or more classes, which you deliver. If the end-user does not have a JVM, then you will also need to include that with the classes of your application, so that they can run your program.
There are a number of installer programs that lets you to bundle your classes with a variety of JVMs (say, for different platforms), and put it all on a CD-ROM. Then the end-user can install the correct version of JVM (assuming they do not already have it on their machine.).
Summary
Keeping the following objectives in mind, Java language has been developed:
o Security o Reliability
Completely platform independent and functions seamlessly regardless of the type of CPU
Java is a development environment, which provides tools like compiler, interpreter, documentation generator, and so on.
Secure, object oriented, robust, portable, platform independent, multithreaded are some of the salient features of Java.
An applet is a program, which is downloaded in the client machine and executed by the JVM of the client machine.
Bytecode is a highly optimized set of instructions designed to be executed by the JVM.
The JRE provides the libraries, JVM, Java Interpreter, and other components necessary for you to run applets and applications written in Java.
Test Your Understanding
1. State true or false for the following:
a) Java is an Object Oriented Programming language.
b) Java is developed at Microsoft.
c) Java is secure, robust, and multi-threaded.
d) Java is platform dependent.
e) A Java source file (*.java) is compiled and the corresponding bytecode is available in the *.class file.
f) To run a Java program on any Operating System, JRE appropriate to that OS must be available.
Session 06: Introduction to Java and SDE
Learning Objectives
After completing this session, you will be able to:
Define a class
Create objects
Identify packages, import statement, and Object class
Explain object messaging Defining a Class
A class is a basic building block of an object oriented language.
A class is a template that describes the data and the methods that manipulate the data.
Examples of class:
Classroom
Car
Person
The following Java program defines a class Person with data member name:
Class Person
private String name;
public void setName(String aName) { name = aName;
}
public String getName() { return name;
} }
The data members in a class can be defined as follows:
Instance variable:
o This differentiates one object from another, giving an object its individuality. For example, the particular name and address for a given Person object is declared as an instance variable.
o An instance variable relates to an instance (object) of its class.
Class variable:
o The data that is shared by all the objects are declared as class variables.
o There is only one copy of each of these variables no matter how many objects are created for the class.
o These variables exist even if no object of the class has been created.
o These variables are also referred to as static fields because they are declared with the keyword static.
o Data members of the class are normally declared as instance variables using the keyword private.
Creating Objects
An object is created from a class.
The following statement creates an object:
Person EdmundHillary = new Person();
The preceding statement has three parts:
Declaration: This notifies the compiler that you will use name to refer to data whose type is type;
Person EdmundHillary
Instantiation: The new keyword is a Java operator that creates the object.
EdmundHillary = new Person()
Initialization: The new operator is followed by a call to a constructor. The constructor will contain code that initializes the new object to the desired values.
Packages
Every class in the Java library belong to a package.
In the Java API, classes are grouped into packages.
A class has a full name, which is a combination of the package name and the class name. For example, the class ArrayList is actually java.util.ArrayList.
To use a class in a package other than java.lang, you must tell Java the full name of the class.
Packages are important for three main reasons:
o Firstly, they help in the overall organization of a project or library.
o Secondly, packages give you a name-scoping, to help to prevent collisions if many programmers in a company decide to make a class with the same name.
o Thirdly, packages provide a level of security, because you can restrict the code, which you write so that only other classes in the same package can access it.
To put a class in a package, put a package statement at the top of the source code file, before any import statement like package com.mypack;.
To be in a package, a class must be in a directory structure that exactly matches the package structure. For a class, com.mypack.Book, the Book class must be in a directory named mypack, which is in a directory named com.
Organizing your classes into packages prevents naming collisions with other classes, if you preponed your reverse domain name on to the front of a class name.
Import Statement
A typical set of import statements might look like the following:
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
The import statements must come right after the package statement, before the class statement.
Purpose of import statement:
Because of the rigid source code naming convention, the Java compiler can easily find the corresponding source or class files just from the fully qualified name of a package and class. This can be illustrated with the following example:
java.util.ArrayList myList = new java.util.ArrayList(50);
The alternative to this long-winded style of coding (as shown earlier), is to use import statements like the following:
import java.util.ArrayList;
…
ArrayList myList = new ArrayList(50);
To derive a class from an external superclass, you must first import the superclass using the import statement.
Object Class
Every class in Java extends class Object that is the Object class is the mother of all classes. It is the superclass of everything.
Any class that does not explicitly extend another class, implicitly extends Object class.
Few of the important methods of the Object class are as follows:
equals(Object obj) : Indicates whether some other object is "equal to" this one and it returns a boolean value as true or false
toString() : Returns a string representation of the object
hashCode() : Returns a hash code value (of integer type) for the object
Object Messaging
Objects cooperate and communicate with other objects in a program by passing messages to one another (setting a value, returning a value, or sending an email).
When an object invokes a method on itself or on the method of another object, it is said to pass a message to the object that contains the target method.
The message contains the name of the method and any data that the method requires.
Example: person.setName(“EdmundHillary”);
Data that are passed to a method are known as arguments. The required arguments for a method are defined by a parameter list of the method.
After performing the operation, the body of the method uses the return statement to return the value to the calling object.
this keyword:
o Used to represent the object that invokes the method:
o Used within the member methods of the class those are not static
this keyword
this keyword refers to the object that is currently executing.
this keyword also allows one constructor to explicitly invoke another constructor in the same class.
It is useful for a method to refer to instance variables relative to this keyword when a local variable hides the data member with the same name, like the following code:
Class Person { String name;
Person(String name) { this.name = name;
}
…….
}
Try It Out
Problem Statement:
Illustrate the importance of the objects with relevant to entries in the address book.
Code:
class AddressBook { String id;
int telephoneNumber;
public void getName() { }
public String setName(String aName) { } public String changeName(String aName) { } }
Refer File Name: AddressBook.java to obtain soft copy of the program code
How It Works:
One analogy for objects is a packet of unused visiting cards in the address book.
Each card has the same blank fields (the instance variables).
When you fill out a card, you are creating an instance (object), and the entries you make on that card represent its state.
The methods of the class are the things you do to a particular card.
getName() , changeName(String aName) , and setName(String aName) could all be the methods for the class AddressBook.
So, each card can do the same things (getName() , setName(String aName) ,and so on), but each card knows things unique to that particular card.
Tips and Tricks
What if you have a hundred classes or a thousand? Is not that a big pain to deliver all those individual files? Can these classes be bundled into one application thing?
Solution: Yes, it would be a big pain to deliver a huge bunch of individual files to your end-users, but you will not have to. You can put all of your application files into a Java Archive or a .jar file that is based on the pkzip format. In the jar file, you can include a simple text file formatted as
something called a manifest, that defines which class in that jar holds the main() method that should run.
Summary
Object-oriented programming lets you to extend a program without having to touch the working code that is tested earlier.
A class describes how to make an object of that class type. A class is like a blueprint.
An object knows things and does things.
The things an object knows about itself are called instance variables. They represent the state of an object.
The things an object does are called methods. They represent the behaviour of an object.
Test Your Understanding
1. State true or false for the following:
a) The package statement in a Java program, if available, can be written anywhere in the code.
b) The import statement can be placed next to the package statement in a Java program.
c) A Java class is made up of data members and methods.
d) Every class in Java does not extend the Object class.
Session 07: Introduction to Java and SDE
Learning Objectives
After completing this session, you will be able to explain the following:
The main() method and SOP (System.out.println method)
Code structure in Java The main Method
In Java, everything goes in a class. The source code is typed into a file (with a .java extension). This is then compiled into a new class file (with a .class extension)
When you run your program, you are really running a class.
Running a program means telling the Java Virtual Machine (JVM) to “Load the class”
(Book class for example), then starting to execute its main() method. Keep running till all the code in main() method is finished.
The main() method is where a Java application starts running.
Every Java application has to have at least one class, and at least one main() method.
The main() method will be written as shown in the following code:
public static void main (String[] args) { // your code goes here
}
Uses of main() method:
o To test your real class
o To launch or start your Java application The System.out.println (SOP) Method
The System class is available in the java.lang package.
The “out” is a field of the System class representing the “standard” output stream.
Typically this stream corresponds to display output or another output destination specified by the host environment or user.
For simple stand-alone Java applications, a typical way to write a line of output data is System.out.println(data) .
System.out.println inserts a new line while System.out.print keeps printing to the same line.
Code Structure in Java
Put a class in a source file
Put methods in a class
Put statements in a method
Example /**
*
* Sample program to print This is my first Java program *
*/
class MyFirstProgram { // Declare class MyFirstProgram void displayMessage() {
System.out.println(“This is my first Java program”);
}
public static void main(String[] args) {
MyFirstProgram myFirstProgram = new MyFirstProgram();
myFirstProgram.displayMessage();
} }
Save the preceding contents in a file called MyFirstProgram.java
Compile and Run a Java Program
Compile: To compile MyFirstProgram.java source file in a MS-DOS prompt, give the following command:
javac MyFirstProgram.java
After successfully compiling the earlier source file, the compiler generates MyFirstProgram.class file, which is made up of bytecodes. The compiled bytecode is platform independent.
Run: To run the earlier Java program in a MS-DOS prompt, give the following command:
java MyFirstProgram
The JVM translates the bytecode into something that the underlying platform understands, and runs your program.
The following output will be displayed in the command prompt after running the earlier command:
This is my first Java program
Try It Out
Problem Statement:
Illustrate the basic structure of a Java application program.
Code:
/**
* A Simple application.
public class SimpleApp1 {
public static void main(String args[]) { // A method to output to the console.
System.out.println("Simple program");
} }
Refer File Name: SimpleApp1.java to obtain soft copy of the program code
How It Works:
/**
* A Simple application.*/ : A comment describing the program
public class SimpleApp1 { : Begin with the class specification
public static void main(String args[]) { : Required method for application programs
// A method to output to the console. : Comment using two slashes
System.out.println("Simple program"); : Print to console
}: Curly braces span the code for a class. They also bracket the code of a method.
Tips and Tricks
Does a Java program need to have a main() method in every class written?
Solution: No, a Java program might use dozens of classes (even hundreds), but you might have only one with a main() method, the one that starts the running of the program. You might write test classes, though, that have main methods for testing your other classes.
Summary
The method signature for the main method in a Java application is as follows:
public static void main(String[] args)
Briefly explain the reason that the main method in a Java application is declared public.
The keyword public indicates that the method can be called by any object.
Explain the reason that the main method in a Java application must be declared static.
The keyword static indicates that the method is a class method which can be called without the requirement to instantiate an object of the class. This is used by the Java interpreter to launch the program by invoking the main method of the class identified in the command to start the program.
Describe the purpose of the keyword void when used as the return type for the main method.
The void keyword when used as the return type for any Java methods indicates that the method does not return anything.
Test Your Understanding
1. State true or false for the following:
a) The execution of a Java Program always starts with the main() method.
b) There is no difference between System.out.print and System.out.println methods in displaying the output.
c) javac and java are the tools available in Java Development Kit (JDK).
d) A bytecode is platform dependent.
Session 08: Introduction to Java and SDE
Learning Objectives
After completing this session, you will be able to:
Describe the Standard Development Environment (SDE)
Identify the various kinds of projects in SDE Introduction to Java and SDE
SDE 4.x was built on top of Eclipse 3.3.0 (Europa) Features of SDE 4.x:
Easy to install
You can build variety of applications such as:
Simple Java Application
Java Applets
Dynamic Web applications using different frameworks such as Struts (1.1-2.0) and Spring MVC (MVC stands for Model View Controller)
Has good support to build JSF and Struts application
J2EE Applications (Web applications with EJB and Web services)
CTS Project
The new features of SDE 4.x are:
Platform Support:
JBoss and Tomcat (Advanced version)
Web Sphere community Ed
Oracle application server
Oracle application server