4. CONTENTS:
1. The History and Evolution of Java.
2. Java Language Basics.
3. Classes and Objects.
4. Inheritance.
5. Interfaces and Abstract Classes.
6. Packages.
7. Exception Handling.
8. Numbers and Strings.
9. Multithreaded Programming.
10. Basic I/O and Applets.
5. 1. The History and Evolution of Java.
•History and Evolution of Java.
•The Java Technology.
•Features of Java Language.
•Types of Java Programs.
•The ” HelloWorld” Application.
•“HelloWorld” for Windows OS.
•“HelloWorld” for Linux/Solaris.
•A Closer Look at “HelloWorld” Program.
• File naming rules.
• Platforms /Editions of Java.
6. History and Evolution of Java
Java was conceived by James Gosling, Patrick Naughton, Chris Worth, Ed-
Frank and Mike Sheridan at Sun Microsystems. Inc USA in 1991.
The language was initially called ‘Oak’ but renamed ‘Java’ in 1995.
The primary motivation for the creation of Java was the need for a platform-
independent (Architectural Neutral) language that could be used to create
software to be embedded in various consumer electronic devices, such as
Microwave Owens and Remote Controls.
Many types of CPUs are used as controllers. The trouble with most other
languages is that they are designed to be compiled for a specific target.
Although it is possible to compile a C++ program for any type of CPU, it
requires a full C++ compiler targeted for that CPU. The problem is that
compilers are expensive and time consuming to create.
In an attempt to find an easier and cost efficient solution, the developers
began to work on a portable, platform independent language that could be
used to produce code that would run on a variety of CPUs under differing
environment. This efforts ultimately led to the creation of Java.
7. The Java Technology
Java technology is both a programming language and a platform.
The Java Programming Language.
In the Java programming language, all source code is first written in plain
text files ending with the .java extension. Those source files are then
compiled into .class files by the javac compiler. A .class file does not contain
code that is native to our processor; it instead contains byte codes — the
intermediate code understood by the Java Virtual Machine (Java VM). The
java launcher tool (Java interpreter) then runs our application with an
instance of the Java Virtual Machine.
Figure : An overview of the software development process
8. Because the Java VM is available on many different operating systems, the
same .class files are capable of running on Microsoft Windows, the Solaris™
Operating System (Solaris OS), Linux, or Mac OS.
Through the Java VM, the same application is capable of running on multiple platforms
9. The Java Platform.
A platform is the hardware or software environment in which a program runs.
Most platforms can be described as a combination of the operating system and
underlying hardware. The Java platform differs from most other platforms in
that it's a software-only platform that runs on top of other hardware-based
platforms. The Java platform has two components:
• The Java Virtual Machine.
• The Java Application Programming Interface (API)
The Java Virtual Machine [JVM].
All language compilers translates source code in to machine code for a specific
computer. Java compiler also do this. Java achieves platform- independency
in the way that Java compiler produces an intermediate code known as ‘java
byte-code’ for a machine that does not exists. This machine is called The
‘Java Virtual Machine’ [JVM] and it exists only inside the computer’s
memory. It is a simulated computer inside the real computer and does all
major functions of a real computer.
The byte code is not machine specific. The machine code is generated by the
Java Interpreter by acting as an intermediary between java virtual machine
and the real machine. The interpreter is different for different machines.
10. (API) The Java Application Programming Interface
The API is a large collection of ready-made software components that provide
many useful capabilities. It is grouped into libraries of related classes and
interfaces; these libraries are known as packages.
Figure : The API and Java Virtual Machine insulate the program from the underlying hardware
As a platform-independent environment, the Java platform can be a bit
slower than native code.
11. Features of Java Language.
Simple, Small & Familiar Platform Independent
Object Oriented Portable
Distributed High performance
Multithreaded Robust & Secure
Compiled & Interpreted Dynamic & Extensible
Java programming language is a high-level language that can be
characterized by all of the following buzzwords:
Simple, Small & Familiar.
Java is simple to use for professionals. Java Codes looks like a ‘C++’ code. In
fact Java is a simplified version of C++.
Platform Independent
Java programs can be easily moved from one computer to another,
anywhere any time. Changes, upgrades in OS, processors, and system
resources will not force any changes in Java programs.
Object Oriented.
Java is a true object-oriented programming language. Almost everything in
java is an object. All program codes and data resides within objects and
classes.
Portable.
Java program can be easily moved from one computer to another computer,
anywhere, any time.
Distributed.
Java is designed as a distributed language for creating applications on
networks. It has the ability to share both data and programs. Java
applications can open and access remote objects on Internet as they can do
in local machines.
High Performance.
Java architecture is designed to reduce overheads during runtime.
Multithreading enhances the overall execution speed of java programs.
Multithreaded.
Multithreaded means handling multiple tasks simultaneously. Java supports
multithreaded programming. We needn’t wait for the application to be
finished one task before beginning another.
Robust & Secure.
Java provides many safeguards to ensure reliable code. It has strict compile
time and runtime checking for data types. It is designed as a garbage collected
language. It also incorporates the concepts of exception handling which
captures critical errors and eliminates any risk of crashing the system.
Compiled & Interpreted.
Running a Java program is a two stage process. First java compiler translates
source code in to an intermediate code known as ‘byte code’. Second Java
interpreter converts he byte code in to machine code that can be directly
executed by the machine that is running the java program.
Dynamic & Extensible.
Java is capable of dynamically linking in new class libraries, methods and
objects.
Java programs support functions written in other languages such as C & C+
+. These functions are called native methods. Native methods are linked
dynamically at runtime.
12. Types of Java Programs
Java Programs are of two types:
Applications.
Applets.
Applications are stand alone programs. They are designed to run on local
machines.
An Applet is a special kind of Java program that is designed to be
transmitted over the Internet and automatically executed by a Java
compatible web browser. An ‘Applet’ is downloaded on demand just like an
image, sound or video clip.
13. The "Hello World!" Application
OhK! It's time to write our first application!
A Checklist
To write our first program, we need:
1.The Java SE Development Kit 7 (JDK 7)
2.A text Editor
In this example, we'll use Notepad, a simple editor included with the Windows
platforms. You can easily adapt these instructions if you use a different text
editor.
These two items are all we need to write your first application.
HelloWorld Application in Windows.
14. Creating Your First Application
Our first application, HelloWorld.java, will simply display the greeting
"Hello world!".
To create this program, we will:
Create a source file:
A source file contains code, written in the Java programming language, that
we and other programmers can understand. We can use any text editor to
create and edit source files.
Compile the source file into a .class file:
The Java programming language compiler (javac) takes our source file and
translates its text into instructions that the Java virtual machine (JVM) can
understand. The instructions contained within this file are known as
bytecodes.
Run the program
The Java application launcher tool- ie., interpreter (java) uses the Java virtual
machine to run our application.
15. Create a Source File:
To create a source file, First, start the editor.
We can launch the Notepad editor from the Start menu by selecting
Programs > Accessories > Notepad. In a new document, type in the
following code:
class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
16. Save the code in a file with the name HelloWorld.java.
To do this in Notepad, first choose the File > Save As menu item.
Then, in the Save As dialog box:
Using the Save in combo box, specify the folder (directory) where we'll save
our file. In this example, the directory is worksjava on the F drive.
In the File name text field, type HelloWorld.java,
From the Save as type combo box, choose ‘All Files’.
In the Encoding combo box, leave the encoding as ANSI.
When you're finished, the dialog box should look like this.
18. Compile the Source File into a .class File
Bring up a shell, or "command," window. We can do this from the Start
menu by choosing Command Prompt (Windows XP), or by choosing Run...
and then entering cmd. The shell window should look similar to the
following figure.
19. The prompt shows our current directory. When we bring up the prompt, our
current directory is usually our home directory for Windows XP.
To compile our source file, change our current directory to the directory
where our file is located. For example, if your source directory is java which is
inside works directory on the F drive, type the following command at the
prompt and press Enter:
F:
cd WorksJava
Now the prompt should change to F:WORKSjava>
Changing directory on an alternate drive.
20. If you enter dir at the prompt, you should see your source file, as the
following figure shows.
Directory listing showing the .java source file.
21. Now you are ready to compile.
At the prompt, type the following command and press Enter.
javac HelloWorld.java
The compiler has generated a bytecode file, HelloWorld.class.
At the prompt, type dir to see the new file that was generated, as shown in the
following figure.
Directory listing, showing the generated .class file
Now that you have a .class file, you can run your program.
22. Run the Program
In the same directory, enter the following command at the prompt:
java HelloWorld
The next figure shows what we should now see:
Congratulations! You have done !. Your program works!
23. "Hello World!" for Solaris OS and Linux
It's time to write our first application!
These detailed instructions are for users of Solaris OS and Linux.
A Checklist
To write your first program, we'll need:
The Java SE Development Kit 6 (JDK 6)
A Text Editor
In this example, we'll use vi, an editor available for many UNIX-based
platforms. We can easily adapt these instructions if we use a different text
editor, such as pico or emacs.
These two items are all we need to write our first application.
24. Creating Our First Application
Our first application, HelloWorldApp, will simply display the greeting "Hello
world!". To create this program, we will:
Create a source file
A source file contains code, written in the Java programming language, that
we and other programmers can understand. we can use any text editor to
create and edit source files.
Compile the source file into a .class file
The Java programming language compiler (javac) takes our source file and
translates its text into instructions that the Java virtual machine can
understand. The instructions contained within this .class file are known as
bytecodes.
Run the program
The Java application launcher tool ie., the Java interpreter (java) uses the
Java virtual machine to run our application.
25. Create a Source File
To create a source file, First, open a shell, or "terminal,"
window.
A new terminal window.
26. When we first bring up the prompt, our current directory will usually be our
home directory. We can change our current directory to our home directory at
any time by typing cd at the prompt and then pressing Return.
The source files we create should be kept in a separate directory. we can create
a directory by using the command mkdir. For example, to create the directory
java in your home directory, use the following commands:
$ mkdir java
To change our current directory to this new directory, we then enter:
$ cd java
Now we can start creating your source file.
Start the vi editor by typing ‘vi’ at the prompt and pressing Return.
Type the following code into the new opened buffer:
class HelloWorldApp{
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
27. Save the code in a file with the name HelloWorld.java
You can press Esc key followed by a column ( : ) and ‘wq’ to exit vi.
Compile the Source File into a .class File
Bring up another shell window. To compile our source file, change our
current directory to the directory where our file is located. For example, if
our source directory is /home/java, type the following command at the
prompt and press Return:
28. $ cd /home/java
If we enter pwd at the prompt, we should see the current directory, which in
this example has been changed to /home/jdoe/java.
If we enter ls at the prompt, we should see your file.
Now are ready to compile the source file. At the prompt, type the following
command and press Return.
$ javac HelloWorldApp.java
The compiler has generated a bytecode file, HelloWorldApp.class.
Results of the ls command, showing the .java source file.
29. Now that you have a .class file, you can run your program.
Results of the ls command, showing the generated .class file.
30. Run the Program
In the same directory, enter at the prompt:
java HelloWorldApp
The next figure shows what we should now see.
The output prints "Hello World!" to the screen
Congratulations! You have done !. Your program works!
31. A Closer Look at “HelloWorld.java” Program
class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
Here ‘class HelloWorld’ declares a class. Everything must be placed inside
a class. Here ‘class’ is a keyword and ‘HelloWorld’ is the class name.
public static void main(String[] args) – defines a method main( ). Every
Java application program must include a main() method. This I the starting
point for the interpreter to begin the execution of the program.
Here the keyword ‘public’ is an access specifier that declares the main
method as an unprotected and there for making it accessible to all other
classes.
32. The ‘static’ keyword declares this method as one that belongs to the entire
class and not a part of any objects of the class. The main must be declared as
static because the interpreter uses this method before any objects are created.
The ‘void’ states that the main method does not return any value.
Any information that we want to pass to a method is received by variables
specified within the sets of parentheses.
The main method accepts a single argument: an array of elements of type
String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes
information to your application.
For example: java HelloWorld arg1 arg2
Each string in the array is called a command-line argument. Command-line
arguments let users affect the operation of the application without
recompiling it. For example, a sorting program might allow the user to
specify that the data be sorted in descending order with this command-line
argument: -descending
The "Hello World!" application ignores its command-line arguments.
33. The line System.out.println("Hello World!"); prints the string inside the
parentheses on standard output. Here ‘System’ is a class which includes a
field called ‘out’. This has a method named println( ) which does the actual
displaying of text.
A Second Program.
/** The MyWishes class implements an application that simply prints two
lines to the standard output. */
class MyWishes
{
public static void main(String[] args)
{
System.out.println("Hello Duke!"); // Display the string.
System.out.println(“Welcome To Java World”);
}
}
34. Source Code Comments.
The following yellow colored text defines the comments of the MyWishes
application:
/** The MyWishes class implements an application that simply
prints two lines to the standard output. */
class MyWishes
{
public static void main(String[] args)
{
System.out.println("Hello Duke!"); // Display the string.
System.out.println(“Welcome To Java World”);
}
}
Comments are ignored by the compiler but are useful to other
programmers to give information about the program.
35. The Java programming language supports three kinds of comments:
/* text */
The compiler ignores everything from /* to */.
/** documentation */
This indicates a documentation comment (doc comment, for short). The
compiler ignores this kind of comment, just like it ignores comments that
use /* and */. The javadoc tool uses doc comments when preparing
automatically generated documentation.
// text
The compiler ignores everything from // to the end of the line.
36. A File name can be any legal identifier — an unlimited-length sequence of
Unicode letters and digits, beginning with a letter, the dollar sign "$", or the
underscore character "_". The convention, however, is to always begin our file
names with a letter, not "$" or "_". Additionally, the dollar sign character, by
convention, is never used at all.
Similar convention exists for the underscore character; while it's technically
legal to begin your variable's name with "_", this practice is discouraged.
File Naming Rules.
Every programming language has its own set of rules and conventions for the
kinds of names that we're allowed to use, and the Java programming language
is no different. The rules and conventions for naming our files can be
summarized as follows:
File names are case-sensitive. Java treats capital and small letters
differently.
Subsequent characters may be letters, digits, dollar signs, or underscore
characters. Conventions (and common sense) apply to this rule as well. When
choosing a name for your file use full words instead of cryptic abbreviations.
37. The first letter of the file name, by convention, be capitalized. If the name
we choose consists of only one word, spell the rest letters in lowercase letters.
If it consists of more than one word, capitalize the first letter of each words.
Examples are
HelloWorld.java
MyFirstProgram.java
Temperature.java
Usually the class name is given as the file name in Java. If a program
contains multiple classes, the name of the class which contains main method
is given as the class name.
White space is not permitted.
38. Platforms/Editions of Java
There are four platforms/editions of Java programming language:
Java Platform, Standard Edition (Java SE).
Java Platform, Enterprise Edition (Java EE).
Java Platform, Micro Edition (Java ME).
Java FX.
All Java Platforms consists of a Java Virtual Machine (JVM) and an
Application Programming Interface (API). This allows applications written
for that platform to run on any compatible system with all the advantages
of Java programming language.
Java SE
Java SE’s API provides the core functionality of Java programming
language. It defines everything from the basic types and objects of the
language to the high-level classes that are used for networking, security,
database access, GUI development and XML parsing. Java SE platform
consists of JVM, development tools, deployment technologies, and other
class libraries and toolkits commonly used in Java technology
applications.
39. Java EE
The Java EE platform is built on top of the Java SE platform. The Java EE
platform provides an API and runtime environment for developing and
running large-scale, multi-tiered, scalable, reliable, and secure network
applications.
Java ME
The Java ME platform provides an API and a small-footprint virtual
machine for running Java programming language applications on small
devises, like mobile phones. The API is a subset of Java SE API, along with
special class libraries useful for small devise application development.
Java FX
Java FX technology is a platform for creating rich internet applications
written in Java FX script. Java FX script is a statically typed declarative
language that is compiled to Java technology ‘byte code’, which can then
be run on Java VM. Applications written for the Java FX platform can
include and link to Java programming language classes, and may be client
of Java EE platform services.