Java Training Report
Java Training Report
Java Training Report
1. Introduction
2. Characteristics of java
3. Java and C
4. Java and C++
5. Object Oriented Principles
6. Classes and Objects
7. Declaring class members
8. Java Architecture
9. Garbage Collection in JVM
10.Java Bytecode
11.JVM
12.Packages
12.1 API
12.2 User-Defined Packages
13. J2ME or JAVA ME
14. J2EE or JAVA EE
Introduction
Characteristics of Java:
1) Platform Independent
Java byte code is exactly the same on every platform.. Java programs that have been
compiled into byte code still need an interpreter to execute them on any given platform.
The interpreter reads the byte code and translates it into the native language of the host
machine on the fly. Since the byte code is completely platform independent, only the
interpreter and a few native libraries need to be ported to get Java to run on a new
computer or operating system.
All these pieces, the javac compiler, the java interpreter, the Java programming language,
and more are collectively referred to as Java.
2) Object oriented
3) Robust
Java implements a robust exception handling mechanism to deal with both expected and
unexpected errors. The worst that an applet can do to a host system is bringing down the
runtime environment. It cannot bring down the entire system.
Most importantly Java applets can be executed in an environment that prohibits them
from introducing viruses, deleting or modifying files, or otherwise destroying data and
crashing the host computer. A Java enabled web browser checks the byte codes of an
applet to verify that it doesn't do anything nasty before it will run the applet.
4) Multithreaded
Java is inherently multi-threaded. A single Java program can have many different threads
executing independently and continuously. Three Java applets on the same page can run
together with each getting equal time from the CPU with very little extra effort on the
part of the programmer.
5) High performance
Java byte codes can be compiled on the fly to code that rivals C++ in speed using a "just-
in-time compiler." Several companies are also working on native-machine-architecture
compilers for Java. These will produce executable code that does not require a separate
interpreter, and that is indistinguishable in speed from C++.
6) Dynamic
Java programs carry with them substantial amounts of run-time type information that is
used to verify and resolve accesses to objects at run-time. This makes it possible to
dynamically link code in a safe and expedite manner. The java programming language is
unusual than other programming languages it first compiles and then interprets the
program. Compile first translate the program into intermediate language called
intermediate language called java byte code. Java byte code is platform independent
code, which is further interpreted by the interpreter on the java platform. Interpreter
parses and run each java byte code instruction on the computer. Compilation occurs only
once, interpretation occurs each time when the program is executed.
Fig 1.1
Java byte code helps in making the program “write once, run anywhere”. The program
can be compiled into byte code by any platform that is having the java compiler; the
compiled java byte code program is ready to run on any machine having the java virtual
machine (JVM). JVM is an interpreter for byte code.
Need of Java
The main motive for developing java was to meet the need of portable and platform
independent language that could be used to produce code that would run on a variety of
systems.
Java and C
Java does not include keywords like goto , sizeof and typedef.
Java does not contain data types like struct, union and enum.
Java does not define the type modifiers keywords like auto, register, signed.
Java does not support an explicit pointer type.
Java does not have preprocessor.
Java does not support variables arguments to functions.
Java adds many features required for Object-Oriented Programming.
The object oriented programming languages provide mechanisms that help you
implement the object-oriented model.
One of the important object-oriented techniques is hiding the data within the class and
making it available only through the methods. This technique is known as encapsulation
because it seals the data (and internal methods) safely inside the "capsule" of the class,
where it can be accessed only by trusted users (i.e., by the methods of the class). The
most important reason is to hide the internal implementation details of your class. If you
prevent programmers from relying on those details, you can safely modify the
implementation without worrying that you will break existing code that uses the class.
Another reason for encapsulation is to protect your class against accidental or willful
stupidity. A class often contains a number of interdependent fields that must be in a
consistent state. If you allow a programmer (including yourself) to manipulate those
fields directly, he may change one field without changing important related fields, thus
leaving the class in an inconsistent state. If, instead, he has to call a method to change the
field, that method can be sure to do everything necessary to keep the state consistent.
Similarly, if a class defines certain methods for internal use only, hiding these methods
prevents users of the class from calling them. When all the data for a class is hidden, the
methods define the only possible operations that can be performed on objects of that
class. Once you have carefully tested and debugged your methods, you can be confident
that the class will work as expected. On the other hand, if all the fields of the class can be
directly manipulated, the number of possibilities you have to test becomes unmanageable.
There are other reasons to hide fields and methods of a class, as well:
Internal fields and methods that are visible outside the class just clutter up the
API. Keeping visible fields to a minimum keeps your class tidy and therefore
easier to use and understand.
If a field or method is visible to the users of your class, you have to document it.
Save yourself time and effort by hiding it instead.
2) Inheritance
Inheritance is the process by which one object acquires the properties of another object.
This is important because it supports the concept of hierarchical classification. Most
knowledge is made by hierarchical classification. Inheritance is a compile-time
mechanism in Java that allows you to extend a class (called the base class or super class)
with another class (called the derived class or subclass).
In Java, unlike C++, these two types of inheritance are made distinct by using different
language syntax. For class inheritance, Java uses the keyword extends and for interface
inheritance Java uses the keyword implements.In Java, unlike C++, only single class
inheritance is supported. I.e., for a given class, there is only one super class.
3) Polymorphism
A class defines user defined objects and their characteristics. Any concept need to
implement in a java program is encapsulated within a class. A class defines the attributes
and methods of objects of the same type sharing common characteristics. The main
coponents of classes are:
Classes contains statements that include the decleration of data members, which specify
the type of data to be stored. Methods of class contain a set of executable statements that
gives a desired output.
Creating Classes in Java
Code:
class ClassName
//Decleration of methods
class_name object_name;
Instantiation or creation: creates an object of the specified class. When you declare an
object, memory is not allocated to it. Therefore you cannot store data in the data members
of the object. New operator is used to allocate memory to an object.
object_name= new_class_name();
Java supports some basic programming elements, such as data types, keywords,
literals, variables and enums. Java is strictly typed language, which means java gives
importance to type checking. Various data types are:
The built-in data types in java are known as primitive or the simple data types.
Group Data Type Size Range Default Value
Integer Byte One byte -27to27-1 0
(signed)
Short Two byte -215to215 -1 0
Int Four byte -231to231-1 0
63 63
Long Eight byte -2 to 2 -1 0
Floating point Float Four byte 3.4e-0.38to 0.0
3.4e+0.38
Double Eight byte 1.7e-308to 0.0
e+308
1.7
Boolean Boolean One bit True or false False
Character Char Two byte A single Null
character
Examples:
o Objects
o Arrays
o Enums
Abstract data types include data types derived from primitive data types and have
more functions than primitive data types. For example, string is an abstract data type
which can store letters, digits and other characters like „$‟, „#‟, „;‟ etc.
Keywords in Java:
Keywords are the reserved words for the java programming language, which
cannot be used as names for variables, classes or methods. They express the language
features. Some keywords are: abstract, case, const, double, finally, if, int, new, public,
boolean, catch, break, char, etc.
Variable:
o The name of a variable needs to be meaningful, short and without any space or
symbol such as „?‟, „!‟, „#‟, „@‟, etc.
o A variable name must be unique.
o A variable name must begin with a letter, an underscore (_), or the dollar symbol
($), which can be followed by a sequence of letters or digits.
o A variable name should not start with a digit.
o A variable name should not contain embedded white spaces.
o A variable name should not contain a keyword.
o A variable name in Java is case-sensitive. There is a difference between uppercase
and lowercase names.
Literals in Java:
Literals are the values to be stored in variables and constants. A literal contains a
sequence of characters, such as digits, alphabets, or any other symbol that represents the
value to be stored. Various types of literals are:
o Integer literals
o Floating point literals
o Character literals
o String literals
o Boolean literal
Java Architecture
Various components of Java architecture are:
Java programs are saved with an extension “.java”. A “.java” file is compiled to
generate the “.class” file, which contains the bytecode. The JVM converts the
bytecode contained in the .class file to machine object code. JVM needs to be
implemented for each platform running on a different operating system.
o JVM
JVM forms the base for the Java platform and is convenient to use on various
hardware- based platforms.
Components of JVM:
o Class Loader
o Execution engine
o Just in time(JIT) compiler
o Java Application Programming Interface(API)
The Java API is a large collection of ready-made software components that provide many
useful capabilities, such as graphical user interface (GUI). The java API is grouped into
libraries of related classes and interfaces these libraries are known as packages.
Java interpreter
When a program stops referencing an object, it is not required anymore and can be
deleted. The space that is used by the object is released for use by another object.
Java Bytecode
Java bytecode is the form of instructions that the Java virtual machine executes.
Each bytecode is one byte in length, although some require parameters, resulting in some
multi-byte instructions. A Java programmer does not need to be aware of or understand
Java bytecode. Understanding bytecode and what bytecode is likely to be generated by a
Java compiler helps the Java programmer in the same way that knowledge
of assembly helps the C or C++ programmer.
A Java virtual machine (JVM) is a virtual machine that can execute Java
bytecode. It is the code execution component of the Java platform. Sun Microsystems has
stated that there are over 5.5 billion JVM-enabled devices. Programs intended to run on a
JVM must be compiled into Java bytecode, a standardized portable binary format which
typically comes in the form of .class files (Java class files). A program may consist of
many classes in different files. For easier distribution of large programs, multiple class
files may be packaged together in a .jar file (short for Java archive).
The Java application launchers offer a standard way of executing Java code, with or
without a console window respectively.[2]
The JVM runtime executes .class or .jar files, emulating the JVM instruction
set by interpreting it or using a just-in-time compiler (JIT) such as Oracle's Hot Spot. JIT
compiling, not interpreting, is used in most JVMs today to achieve greater speed. There
are also ahead-of-time compilers that enable developers to pre-compile class files into
native code for particular platforms.
Like most virtual machines, the Java virtual machine has a stack-based architecture akin
to a microcontroller/microprocessor. However, the JVM also has low-level support for
Java-like classes and methods, which amounts to a highly idiosyncratic memory
model and capability-based architecture.
Packages
Java enables you to combine set of classes within a package. This enables you to
organize the classes and interfaces. A package is a set of classes that are stored in a
directory, which has the same name as the package name. Java packages are classified
into the following two categories:
o Java Application Programming interface (API) packages: The java API consists
of various packages, such as java.lang, java.util, java.io, java.awt, java.net and
java. applet.
o Java User defined Packages: The packages that a user creates are called user-
defined packages. The user-defined packages can be imported in any Java
program.
Java API
The java API contains classes that are grouped in different packages in
accordance to their functions.
“import java.awt.event.*;”
“import java.awt.event.ActionEvent;”
If you do not use a package declaration, your class ends up in an unnamed package.
Classes in an unnamed package cannot be imported from classes in any other package.
Classes within a package can access classes and members declared with default
access and class members declared with the protected access modifier. Default access is
enforced when neither the public, protected nor private access modifier is specified in the
declaration. By contrast, classes in other packages cannot access classes and members
declared with default access. Class members declared as protected can be accessed from
the classes in the same package as well as classes in other packages that are subclasses of
the declaring class.
Packages are usually defined using a hierarchical naming pattern, with levels in
the hierarchy separated by periods („.‟, pronounced "dot"). A package name begins with
the top level domain name of the organization and then the organization's domain and
then any sub domains, listed in reverse order. The Java Language Specification
establishes package naming conventions to avoid the possibility of two published
packages having the same name. The naming conventions describe how to create unique
package names, so that packages that are widely distributed will have unique
namespaces. This allows packages to be separately, easily and automatically installed and
catalogued.
User-Defined Package
A user-defined package contains one or more classes that can be imported in a
Java program. User may create many classes. These classes can be organized by creating
packages. The packages created by user are called user-defined packages.
One can create a user-defined package by using the keyword „package‟. The
package decleration must be at the beginning of the source file. Only one package
decleration can be made in one source file.
Code:
package <package_name>
// Class definition
public Class<classname1>
public Class<classname2>
The file containing the package is saved as .java file. After compiling the source code, the
.class file is created that is stored in the directory having the same name as the package
name.
Code:
import app.empDetails.Employee
J2ME or JAVA ME
It provides a robust, flexible environment for applications running on mobile and
embedded devices: mobile phones, set-top boxes, Blu-ray Disc players, digital media
devices, M2M modules, printers and more. Java ME technology was originally created in
order to deal with the constraints associated with building applications for small devices.
For this purpose Oracle defined the basics for Java ME technology to fit such a limited
environment and make it possible to create Java applications running on small devices
with limited memory, display and power capacity.
Java EE or J2EE
INTRODUCTION:
You are leaving for work in the morning and someone rings the doorbell….
That is an event!
In life, you encounter events that force you to suspend other activities and respond to
them immediately. In Java, events represent all activity that goes on between the user and
the application. Java‟s Abstract Windowing Toolkit (AWT) communicates these actions
to the programs using events. When the user interacts with a program let us say by
clicking a command button, the system creates an event representing the action and
delegates it to the event-handling code within the program. This code determines how to
handle the event so the user gets the appropriate response.
In today‟s tutorial we are going to learn event-driven programming, the event model of
Java, and the different ways in which you can handle events.
Components of an Event:
Event Classes:
The EventObject class is at the top of the event class hierarchy. It belongs to the
java.util package. While most of the other event classes are present in java.awt.event
package. The getSource() method of the EventObject class returns the object that initiated
the event. The getId () method returns the nature of the event. For example, if a mouse
event occurs, you can find out whether the event was click, a press, a move or release
from the event object. AWT provides two conceptual types of events: Semantic and low-
level events.
Semantic events:
Event Listeners:
Applets
An applet is a small Java program that is embedded and ran in some other Java
interpreter program such as a Java technology-enabled browser.Sun‟s applet viewer
program called appletviewer .A Java applet is a small application written in Java and
delivered to users in the form of bytecode. The user launches the Java applet from a web
page and it is then executed within a Java Virtual Machine (JVM) in a process separate
from the web browser itself.
How Applets Differ from Applications :-
Although both the Applets and stand-alone applications are Java programs, there
are certain restrictions are imposed on Applets due to security concerns.
Applets don‟t use the main() method, while they are loaded ,automatically call
certain methods (init, start, paint, stop, destroy).
They are embedded inside a web page and executed in browsers.
They cannot read from or write to the files on local computer.
They cannot communicate with other servers on the network.
They cannot run any programs from the local computer.
They are restricted from using libraries from other languages.
The above restrictions ensures that an Applet cannot do any damage to the local
system.
Every applet inherits a set of default behavior from the Applet class. As a result, when
an applet is loaded, it undergoes a series of changes in its state. The applet states include:
Applet States
Born
Running Idle
Dead
Begin
init()
start()
paint()
stop()
start()
destroy()
End
Viewing Applets
As you know, applets are displayed as a part of a Web page. A special HTML tag,
<APPLET>, is used to attach a Java applet to an HTML page. Running an applet requires
the use of a Web browser or other software that serves the function of a browser, such as
the applet viewer program that ships with the Java Developers Kit from Java Soft. The
browser acts as the operating system for applets-you cannot run an applet as a standalone
program in the same way you can run an executable file. At the time of this writing, there
are three widely available Web browsers that can run Java applets:
These programs load applets from a Web page and run them remotely on the Web user's
computer. This arrangement raises security issues that must be handled by the Java
language itself and by Java-enabled browsers
Applet method
The output stream class is an abstract superclass that provides a minimal programming
interface and a partial implementation of output streams. OutputStream defines methods
for writing bytes or arrays of bytes to the stream. An output stream is automatically
opened when you create it. You can explicitly close an output stream with
the close method, or let it be closed implicitly when the OutputStream is garbage
collected.
The java.io package contains several subclasses of InputStream and OutputStream that
implement specific input or output functions.
JDBC
Servelts
The servlet is a Java programming language class used to extend the capabilities
of a server. Although servlets can respond to any types of requests, they are commonly
used to extend the applications hosted by web servers, so they can be thought of as Java
Applets that run on servers instead of in web browsers.Servlets can be generated
automatically from Java Server Pages by the Java Server Pages compiler. The difference
between servlets and JSP is that servlets typically embed HTML inside Java code, while
JSPs embed Java code in HTML.
o During initialization stage of the servlet life cycle, the web container
initializes the servlet instance by calling the init() method, passing an object
implementing the javax.servlet.ServletConfig interface. This configuration
object allows the servlet to access name-value initialization parameters
from the web application.
o After initialization, the servlet instance can service client requests.
Each request is serviced in its own separate thread. The web container calls
the service() method of the servlet for every request. The service() method
determines the kind of request being made and dispatches it to an
appropriate method to handle the request. The developer of the servlet must
provide an implementation for these methods. If a request is made for a
method that is not implemented by the servlet, the method of the parent
class is called, typically resulting in an error being returned to the requester.
o Finally, the web container calls the destroy() method that takes the servlet
out of service. The destroy() method, like init(), is called only once in the
lifecycle of a servlet.
Servlet API
The Servlet 2.3 API consists of two packages: javax.servlet and javax.servlet.http.
The base functionality is defined in the javax.servlet package whose classes and
interfaces outline a generic, protocol-independent implementation. This means you can
use it for non-Web applications, too. Of course, the exam targets the Web, so the HTTP
protocol is the only one discussed in this blog.
Introduction
The Web container creates JSP implicit objects like pageContext, servletContext, session,
request & response.
JSP technology
JavaServer Pages (JSP) technology allows you to easily create web content that has both
static and dynamic components. JSP technology makes available all the dynamic
capabilities of Java Servlet technology but provides a more natural approach to creating
static content.
o A language for developing JSP pages, which are text-based documents that
describe how to process a request and construct a response.
o An expression language for accessing server-side objects.
o Mechanisms for defining extensions to the JSP language.
o Registering back-end objects with the application so that all parts of the
application have access to them.
o Configuring backing beans and model beans so that they are instantiated with the
proper values when a page makes reference to them.
o Defining navigation rules for each of the pages in the application so that the
application has a smooth page flow.
o Packaging the application to include all the pages, objects, and other files so that
the application can be deployed on any compliant container.
Java interface
Java Libraries
The Java class library is a set of dynamically loadable lib.. that Java applications can call
at run time. Because the Java Platform is not dependent on a specific operating system,
applications cannot rely on any of the platform-native libraries. Instead, the Java Platform
provides a comprehensive set of standard class libraries, containing the functions
common to modern operating systems.
o They provide the programmer a well-known set of useful facilities, such
as container classes and regular expression processing.
o The library provides an abstract interface to tasks that would normally
depend heavily on the hardware and operating system, such
as network access and file access.
o Some underlying platforms may not support all of the features a Java
application expects.
o The library implementation can either emulate those features or provide a
consistent way to check for the presence of a specific feature.
XML
XML is a “meta” markup language used to describe the structure of data.XML has taken
the computer industry by storm since its inception and isnow the markup language of
choice for configuration files, data interchange,B2B transactions, and Java 2 Enterprise
architectures. XML is even being used to represent calls to distributed objects through the
simple object access protocol(SOAP), an XML application.XML has numerous
advantages including being easy to read, easy to parse, extensible,and widely adopted. In
addition, you can define a grammar through a document type definition (DTD) to enforce
application-specific syntax. However,the greatest single advantage of XML is that the
data can be easily processed by otherapplications; XML data is not in a proprietary
format. In essence, XML has done for program.
XML schema
A newer schema language, described by the W3C as the successor of DTDs, is XML
Schema, often referred to by the initialism for XML Schema instances, XSD (XML
Schema Definition). XSDs are far more powerful than DTDs in describing XML
languages. They use a rich datatyping system and allow for more detailed constraints on
an XML document's logical structure. XSDs also use an XML-based format, which
makes it possible to use ordinary XML tools to help process them.
XML features
Conclusion
This training focussed upon increasing our knowledge and interest in toward the
java.Because java is most interesting and most used language in these days. We learnt
how to create a web sites and web pages.It was a great experience.It increase our
practical skills that‟s the main yhing which we learnt in the training session.Thus, we
believe that our project will be beneficial for various purposes & hence our efforts will be
fruitful.
BIBLIOGRAPHY
Books:
Websites:
http://www.PHP.net
http://www.google.co.in/