Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Introduction to Java
Module Information
Course Description
• This module will provide you a solid grounding to start
learning the Java language
Prerequisites
Programming concepts
Target Audience
Beginners in JAVA
2
1.0 Introduction
Introduction:
After completing this chapter, you will get a good understanding about JAVA.
Objectives:
After completing this chapter, you will be able to explain,
History of Java
Why is JAVA Important?
Types of Programs in JAVA
Security & Portability
Byte Code
Features of JAVA
PATH and CLASSPATH
Class and Object
JAVAP
JavaDoc
Java API
3
History of Java
• Birth of C
- A structured, efficient high-level language.
- C is a programmers language.
- A powerful, efficient, structured language that was relatively easy to
learn
• The Need for C++
- Why C++
- Complexity
4
History of Java
OOP – OOP is a programming methodology that helps
Organize complex programs through the use of
inheritance,
Encapsulation and polymorphism.
5
History of Java
• The creation of Java
-
Java was conceived by James Gosling, Patrick Naughton,
Chris Warth, Ed Frank, and Mike Sheridon at Sun Microsystems in 1991.
Original impetus of java was not the Internet.
Primary Motivation for developing java
Need for Platform Independent (Architecture neutral) Language.
• Platform Independence
To produce code that could run on a variety of CPU’s under differing
environments.
6
History of Java
• Since different types of CPU, are used as controllers. The trouble with C,
C++ is that they are designed to be compiled for a specific target.
• Platform independence – A high profile necessity
• Finally – Java not an internet version of C++.
• Java is to internet programming and c was to systems Programming.
7
Why is JAVA Important?
Why java important to the internet ?
• Java expands the universe of objects that can move about freely
in cyberspace.
•
In a network two categories of objects are transmitted between
the server and the client.
1. Passive information.
2. Active programs.
Passive Information – Reading a email.
Active programs - A dynamic self executing program.
For ex: A program to display the email properly in the clients
Machine. – Applet.
8
Types of Programs in JAVA
•
Java Applets and Application.
•
Java can be used to write two types of programs.
-Application
-Applet
Applet :
• An applet is an application designed to be transmitted over the internet and
executed by a Java Compatible web browser.
• Applet can react to user input and dynamically change.
• Two Fundamental Problems
– Security
– Portability
9
Security & Portability
Security :
– Fear of viral infection
– Fear of malicious program
Java answers both of these concerns by providing a firewall between a
networked application and your computer. Java achieves this protection by
confining a java program to the Java execution environment and not allowing
it access to other parts of the computer.
Portability :
For programs to be dynamically downloaded to all the various types of platforms
connected to the Internet, some meansof generating portable executable code is
needed.
10
Byte Code
•
The key that allows Java to solve both the security and the portability problems is
that the output of a java compiler is not executable code rather it is Byte code.
•
Byte code is a highly optimized set of instructions designed to be executed by the
Java run-time system, which is called the Java Virtual Machine (JVM).
•
Translating a java program in a wide variety of environments.The reason is only
the JVM needs to be implemented for each platform.
* Remember the details of JVM will differ from Platform to Platform
11
Features of JAVA
• Features of JAVA
– Simple
– Secure
– Portable
– Robust
– Multithreaded
– Architecture –neutral
– Interpreted and high performance
– Distributed
– Dynamic.
12
Features of JAVA
•
Robust
– To better understand how java is robust consider two of the main
reasons for program failure.
– Memory Management mistakes and mishandled exceptional
conditions.
Ex.
Garbage cleaning
division by zero error
file not found etc.
• Architecture - Neutral
– “Write once, run anywhere,any time for ever.”
13
Features of JAVA
• Distributed
– Java is designed for the distributed environment of the Internet because it
handles TCP/IP protocols.
– Accessing a resource using URL is not much different from accessing a file.
** Allowing objects on two different computers to execute procedures remotely
(RMI Remote method invocation )
14
PATH and CLASSPATH
•
Two separate environment variables with separate functions
•
ClassPath:
Environment Variable Consists of :
– Directories,
– JAR files, and
– ZIP files
– Separated by a path separator character (";")
•
Entry in the CLASSPATH represents:
– A location from which the Java runtime will search for classes when it needs
to load them.
15
PATH and CLASSPATH
•
If your classpath contains jars,zip etc.. these have to be added explicitly to the
class path
PATH:
Determines where your system will look for executable programs when they are
named on the command line
Understanding “PATH”:
• Consider that we have a javac.exe executable - this javac.exe helps us to
compile the java files.
• Let us assume that this exe is not in the PATH.
• Let this be placed under c:/jdk/bin;
• Place a java file Test.java under c:/jdk/bin
• Go to your command prompt and go to c:/jdk/bin
• Now type javac Test.java. Your java file will be compiled.
16
PATH and CLASSPATH
• Now place the Test.java under c:
• Go to your command prompt and type javac Test.java Your java file will not be
compiled, the system will say that javac is not recognized. This is because the
file is not in your PATH.
17
Class and Object
Class and Object:
•
Consider a rubber stamp - this represents a class
•
Name and class - represents the properties of the class
•
Now if i use this rubber stamp on a notebook, this stamped image represents an
object(i.e, instance of the rubber stamp). Now you can fill in the name and class
property with your name and class.
•
So you can create 'n' number of objects.
•
Finally,
– Class - just a template
– Object - a template with filled in details
18
Class and Object
19
Class and Object
•
In the above case - the properties name and class are the attributes of that class.
•
For accessing these variables we can define methods in a class, like
getName();
setName();
- these are methods in class.
•
By default a class will have a default constructor. This constructor is responsible
for creating a new object.
Ex:
RubberStamp myLabel = new RubberStamp()
now this new keyword actually calls the default constructor of rubber stamp to
get an instance of the class (i.e.. object)
20
JAVAP
•
Disassembles a class file.
•
Output depends on the options used.
•
If no options are used, javap prints out the public fields and methods of the
classes passed to it.
•
javap prints its output to stdout.
21
JAVAP
Ex:
class C {
static int a = 1;
static int b = 2;
static {
System.out.println(a);
}
static {
a++;
b = 7;
System.out.println(a);
System.out.println(b);
}
static {
System.out.println(b);
}
public static void main(String args[]) {
C c = new C();
}
}
22
JAVAP
• When the resulting class C is passed to javap using no options the
following output results:
Compiled from C:\users\dac\C.java
private class C extends java\lang\Object {
static int a;
static int b;
public static void main(java\lang\String []);
public C();
static void ();
}
23
JAVAP
•
-l : Prints out line and local variable tables.
•
-p : Prints out the private and protected methods and fields of the class in
addition to the public ones.
•
-c : Prints out disassembled code, i.e., the instructions that comprise the Java
byte codes, for each of the methods in the class.
24
JavaDoc
•
Parses the declarations and doc comments in Java source files and formats the
public API into a set of HTML pages.
•
As an argument to javadoc you can pass in either a package name or a list of
Java source files.
•
The package specified on the command line must be in your CLASSPATH. Note
that javadoc uses .java files not .class files.
•
javadoc reformats and displays all public and protected declarations for,
– Classes and Interfaces
– Methods
– Variables
25
JavaDoc
• begin with /** and indicate text to be included automatically in generated documentation.
An example of a class comment:
/**
* A class representing a window on the screen.
* For example:
* <pre>
*
Window win = new Window(parent);
*
win.show();
* </pre>
*
* @see
awt.BaseWindow
* @see
awt.Button
* @version
1.2 12 Dec 1994
* @author
Sami Shaio
*/
class Window extends BaseWindow {
...
}
26
Java API
• The Java Application Programming Interface (API) is prewritten code,
organized into packages of similar topics. For instance, the Applet and AWT
packages include classes for creating fonts, menus, and buttons. The full Java
API is included in the Java 2 Standard Edition download. API Documentation
Building an Application
• For Complete on-line documentation for version 1.4.2 of Java:
•Refer : java.sun.com/j2se/1.4.2/docs/api/
27
Summary
•
•
•
•
•
•
•
•
•
OOP is a programming methodology that helps Organize complex programs
through the use of inheritance,
Encapsulation and polymorphism.
Java can be used to write two types of programs.
-Application
-Applet
Simple, secured and portable
CLASSPATH - A location from which the Java runtime will search for classes
when it needs to load them.
PATH - Determines where your system will look for executable programs when
they are named on the command line.
Class - just a template
Object - a template with filled in details
JAVAP - Disassembles a class file.
Javadoc - Parses the declarations and doc comments in Java source files and
formats the public API into a set of HTML pages.
28
Test Your Understanding
• Why is Java a known as a platform independent Language?
• What is a byte code verifier?
• What is the difference between PATH and CLASSPATH? Are these
only for Windows OS?
• What is an object?
• What is a class disassembler?
• What is an API?
29