Seminar Rahul
Seminar Rahul
Seminar Rahul
TRAINING REPORT
ON
“CORE JAVA”
Submitted in partial fulfillment for the award of Bachelor Of
Technology degree
Of the Bikaner Technical University, Kota
Rahul Sharma
19EBKIT025
BKBIET,PILANI
I would like to thank all the lecturers of Udemy to co-ordinate with me and provide
me the information needed to complete the analysis part of this project.
(RAHUL SHARMA)
CONTENTS
Chapter 1: History
1.1 Principles
2.1 Encapsulation
Gosling aimed to implement a virtual machine and a language that had a familiar
C/C++ style of notation. Sun released the first public implementation as Java 1.0 in
1995. It promised "Write Once, Run Anywhere" (WORA), providing no-cost
runtimes on popular platforms. Fairly secure and featuring configurable security, it
allowed network- and file-access restrictions. Major web browsers soon incorporated
the ability to run Java applets within web pages, and Java quickly became popular.
With the advent of Java 2 (released initially as J2SE 1.2 in December 1998), new
versions had multiple configurations built for different types of platforms. For
example, J2EE targeted enterprise applications and the greatly stripped-down version
J2ME for mobile applications. J2SE designated the Standard Edition. In 2006, for
marketing purposes, Sun renamed new J2 versions as Java EE, Java ME, and Java SE,
respectively.
In 1997, Sun Microsystems approached the ISO/IEC JTC1 standards body and later
the Ecma International to formalize Java, but it soon withdrew from the process. Java
remains a de facto standard, controlled through the Java Community Process. At one
time, Sun made most of its Java implementations available without charge, despite
their proprietary software status. Sun generated revenue from Java through the selling
of licenses for specialized products such as the Java Enterprise System.
Sun distinguishes between its Software Development Kit (SDK) and Runtime
Environment (JRE) (a subset of the SDK); the primary distinction involves the JRE's
lack of the compiler, utility programs, and header files. On 13 November 2006, Sun
released much of Java as open source software under the terms of the GNU General
Public License (GPL).
On 8 May 2007 Sun finished the process, making all of Java's core code available
under free software / open-source distribution terms, aside from a small portion of
code to which Sun did not hold the copyright.
SIMPLE
Java was designed to be easy for the professional programmer to learn and use
effectively. The basic concepts of object-oriented programming , make learning Java
easier. Beyond its similarities with C/C++, Java has another attribute that makes it
easy to learn: it makes an effort not to have surprising features.
SECURE
Every time that you download a “normal” program, you are risking a viral infection.
Prior to Java, most users did not download executable programs frequently, and those
who did scanned them for viruses prior to execution. Even so, most users still worried
about the possibility of infecting their systems with a virus. When you use a
Javacompatible Web browser, you can safely download Java applets without fear of
viral infection or malicious intent.
PORTABILITY
Many types of computers and operating systems are in use throughout the world and
many are connected to the Internet. For programs to be dynamically downloaded to all
the various types of platforms connected to the Internet, some means of generating
portable executable code is needed, which is provided by java.
OBJECT-ORIENTED
Java was not designed to be source-code compatible with any other language. This
allowed the Java team the freedom to design with a blank slate. One outcome of this
was a clean, usable, pragmatic approach to objects. The object model in Java is simple
and easy to extend, while simple types, such as integers, are kept as high-performance
non objects.
ROBUST
MULTITHREADED
ARCHITECTURE-NEUTRAL
A central issue for the Java designers was that of code longevity and portability. One
of the main problems facing programmers is that no guarantee exists that if you write
a program today, it will run tomorrow—even on the same machine. Operating system
upgrades, processor upgrades, and changes in core system resources can all combine
to make a program malfunction. The Java designers made several hard decisions in
the Java language and the Java Virtual Machine in an attempt to alter this situation.
Their goal was “write once; run anywhere, any time, forever.” To a great extent, this
goal was accomplished.
DISTRIBUTED
Java is designed for the distributed environment of the Internet, because it handles
TCP/IP protocols. In fact, accessing a resource using a URL is not much different
from accessing a file. The original version of Java (Oak) included features for
intraaddress-space messaging. This allowed objects on two different computers to
execute procedures remotely. Java revived these interfaces in a package called
Remote Method Invocation (RMI). This feature brings an unparalleled level of
abstraction to client/server programming.
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 expedient manner. This is crucial to the
robustness of the applet environment, in which small fragments of bytecode may be
dynamically updated on a running system.
3
SOFTWARE DEVELOPMENT USING JAVA
3.1WHAT IS A JAVA BEAN?
Swing is a set of classes that provides more powerful and flexible components than
are possible with the AWT. In addition to the familiar components, such as buttons,
check boxes, and labels, Swing supplies several exciting additions, including tabbed
panes, scroll panes, trees, and tables. Even familiar components such as buttons have
more capabilities in Swing.
The Swing component classes that are used in this book are shown here:
Class Description
AbstractButton Abstract superclass for Swing
buttons.
ButtonGroup Encapsulates a mutually exclusive set of
buttons.
ImageIcon Encapsulates an icon.
JApplet The Swing version of Applet.
JButton The Swing push button class.
JCheckBox The Swing check box class.
JComboBox Encapsulates a combo box (an
combination of a drop-down list and text
field).
JLabel The Swing version of a label.
JRadioButton The Swing version of a radio button.
JScrollPane Encapsulates a scrollable window.
JTabbedPane Encapsulates a tabbed window.
JTable Encapsulates a table-based control.
JTextField The Swing version of a text field.
JTree Encapsulates a tree-based control.
This example Swing application creates a single window with "Hello, world!" inside:
// Hello.java (Java SE 5)
import java.awt.BorderLayout;
import javax.swing.*;
public class Hello extends JFrame
{
public Hello()
{
super("hello");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(new JLabel("Hello, world!"));
pack();
}
public static void main(String[] args)
{
new Hello().setVisible(true);
}
}
The first import statement directs the Java compiler to include the BorderLayout class
from the java.awt package in the compilation; the second import includes all of the
public classes and interfaces from the javax.swing package.
The Hello class extends the JFrame class; the JFrame class implements a window with
a title bar and a close control.
The Hello() constructor initializes the frame by first calling the superclass constructor,
passing the parameter "hello", which is used as the window's title. It then calls the
setDefaultCloseOperation(int) method inherited from JFrame to set the default
operation when the close control on the title bar is selected to
AWT - BUILDING A GRAPHICAL INTERFACE
Next, the layout of the frame is set to a BorderLayout; this tells Swing how to
arrange the components that will be added to the frame.
3.3SERVLETS
Servlets are small programs that execute on the server side of a Web connection.
A Simple Servlet:-
To become familiar with the key servlet concepts, we will begin by building and
testing a simple servlet. The basic steps are the following:
• Create and compile the servlet source code.
• Start Tomcat.
• Start a Web browser and request the servlet.
4
SYNTAX
The syntax of Java is largely derived from C++. Unlike C++, which combines the
syntax for structured, generic, and object-oriented programming, Java was built
almost exclusively as an object oriented language. All code is written inside a class
and everything is an object, with the exception of the intrinsic data types (ordinal and
real numbers, boolean values, and characters), which are not classes for performance
reasons.
Java uses the same commenting methods as C++. There are two different styles of
comment: a single line style marked with two forward slashes, and a multiple line
style opened with a forward slash asterisk (/*) and closed with an asterisk forward
slash (*/).
Example:
/* This is an example of a multiple line comment using the forward slash and asterisk.
This type of comment can be used to hold a lot of information but it is very important
to remember to close the comment. */
5
EXAMPLES
5.1 Hello world
The traditional Hello world program can be written in Java as:
/*
* Outputs "Hello, world!" and then exits
*/
public class HelloWorld { public static
System.out.println("Hello, world!");
Source files must be named after the public class they contain, appending the suffix
.java, for example, HelloWorld.java. It must first be compiled into bytecode, using a
Java compiler, producing a file named HelloWorld.class.
It is simply the name of the method the Java launcher calls to pass control to the
program. Java classes that run in managed environments such as applets and
Enterprise JavaBean do not use or need a main() method. A java program may contain
multiple classes that have main methods, which means that the VM needs to be
explicitly told which class to launch from.
import java.awt.*;
import javax.swing.*;
Java applets are programs that are embedded in other applications, typically in a Web
page displayed in a Web browser.
// Hello.java import
javax.swing.JApplet; import
java.awt.Graphics;
public class Hello extends JApplet
{ public void paintComponent(Graphics g)
{
g.drawString("Hello, world!", 65, 95);
}
}
The import statements direct the Java compiler to include the javax.swing.JApplet and
java.awt.Graphics classes in the compilation.The import statement allows these
classes to be referenced in the source code using the simple class name (i.e. JApplet)
instead of the fully qualified class name (i.e. javax.swing.JApplet).
The Hello class extends (subclasses) the JApplet (Java Applet) class; the JApplet class
provides the framework for the host application to display and control the lifecycle of
the applet.
An applet is placed in an HTML document using the <applet> HTML element. The
applet tag has three attributes set: code="Hello" specifies the name of the JApplet
class and width="200" height="200" sets the pixel width and height of the applet.
Applets may also be embedded in HTML using either the object or embed
element[26], although support for these elements by Web browsers is inconsistent.
[27] However, the applet tag is deprecated, so the object tag is preferred where
supported.
The host application, typically a Web browser, instantiates the Hello applet and
creates an AppletContext for the applet. Once the applet has initialized itself, it is
added to the AWT display hierarchy.
The paint method is called by the AWT event dispatching thread whenever the display
needs the applet to draw itself.
6.2 SERVLET
Servlets are server-side Java EE components that generate responses (typically HTML
pages) to requests (typically HTTP requests) from clients.
JavaServer Pages (JSPs) are server-side Java EE components that generate responses,
typically HTML pages, to HTTP requests from clients.
JSPs embed Java code in an HTML page by using the special delimiters <% and %>.
A JSP is compiled to a Java servlet, a Java application in its own right, the first time it
is accessed.After that, the generated servlet creates the response.
6.4 GENERICS
In 2004 generics were added to the Java language, as part of J2SE 5.0. Prior to the
introduction of generics, each variable declaration had to be of a specific type.
For container classes, for example, this is a problem because there is no easy way to
create a container that accepts only specific types of objects.
Either the container operates on all subtypes of a class or interface, usually Object, or
a different container class has to be created for each contained class.
Generics allow compile-time type checking without having to create a large number
of container classes, each containing almost identical code.
7
Documentation
Javadoc is a comprehensive documentation system, created by Sun Microsystems,
used by many Java developers. It provides developers with an organized system for
documenting their code. Whereas normal comments in Java and C are set off with /*
and */, the multi-line comment tags, Javadoc comments have an extra asterisk at the
beginning, so that the tags are /** and */.
8.1 Examples
/**
* A program that does useful things.
*/
public class Program
{
/**
* A main method.
* @param args The arguments
*/
public static void main(String[] args)
{
//do stuff
}
8
Editions
Sun has defined and supports four editions of Java targeting different application
environments and segmented many of its APIs so that they belong to one of the
platforms.
Java Card –Java Card refers to a technology that allows Java –dd
application(applets)to be run securely on smart cards and similar small memory
footprint devices. Java Card is the tiniest of Java targeted for embedded devices.
Java Card gives the user ability to program the device and make them application
specific. It is widely used in SIM cards(used in mobile phones) and ATM cards.
The first Java Card was introduced in 1996 by Schlumberger’s card division
which later merged with Gemplus to form Gemalto. Java Card products are
based on the Java Card Platform specifications developed by Sun Microsystems,
a subsidiary of Oracle Corporation.
Java Platform, Standard Edition (Java SE) — J2SE is Sun's platform that
includes the Java Virtual Machine (JVM) and packages. There is the core package
which is basically the contents of rt.jar in the distribution package of J2SE. Then
there are specification such as Swing and Networking. Java Platform, Standard
Edition or Java SE is a widely used platform for programming in the Java
language. It is the Java Platform used to deploy portable applications for general
use. In practical terms, Java SE consists of a virtual machine, which must be used
to run Java programs, together with a set of libraries (or "packages") needed to
allow the use of file systems, networks, graphical interfaces, and so on, from
within those programs
Java Platform, Enterprise Edition (Java EE) — J2EE on the other hand builds
on top of J2SE so to speak; J2EE needs J2SE. It includes tools to create
frameworks, API specifications suitable for companies. By specification, JSP and
Servlets (and Beans) are J2EE. Java Platform, Enterprise Edition or Java EE is
a widely used platform for server programming in the Java programming
language. The Java platform (Enterprise Edition) differs from the Java Standard
Edition Platform (Java SE) in that it adds libraries which provide functionality to
deploy fault-tolerant, distributed, multi-tier Java software, based largely on
modular components running on an application server
Java Platform, Micro Edition (Java ME) — Java Platform, Micro Edition, or
Java ME, is a Java platform designed for embedded systems (mobile devices are one
kind of such systems) . Target devices range from industrial controls to mobile phones
(especially feature phones) and set-top boxes. Java ME was formerly known as Java
2 Platform, Micro Edition (J2ME).
The classes in the Java APIs are organized into separate groups called packages. Each
package contains a set of related interfaces, classes and exceptions. Refer to the
separate platforms for a description of the packages available.
The set of APIs is controlled by Sun Microsystems in cooperation with others through
the Java Community Process program.Companies or individuals participating in this
process can influence the design and development of the APIs.
This process has been a subject of controversy. Sun also provided an edition called
PersonalJava that has been superseded by later, standards-based Java ME
configuration-profile pairings.