1. SIX WEEKS TRAINING REPORT
(JUNE-JULY’17)
JAVA AND IT’S APPLICATIONS
Submitted by: Submitted to:
Aisha Siddiqui Mr. R.N.Katiyar
CSE (2K14) (Reader CSE-Department)
005
UNIVERSITY INSTITUTE OF ENGINEERING AND
TECHNOLOGY
2. ACKNOWLEDGEMENT
I acknowledge my gratitude and thanks to all the well
knowledgeable people for giving me opportunity to avail
all the best facilities available at Varags Institute through
which I gained knowledge thinking so as too just in the
environment suitable for harmonic adjustment. I m
grateful to all the persons involved in this for various help
rendered by them during my vocational training period .I
would also thank my teachers , my friend and my family
members for their constant encouragement.
AISHA SIDDIQUI
3.
4.
5. CONTENTS
* History
* Features
* JVM , JRE , JDK
* Variables and Data Types
* Operators
* Wrapper Class
* Classes and Constructor
* Java OOPs Concepts
* Package
* Exception Handling
* Multithreading
* Strings
* Inner Class
* Regular Expressions
* Generics
* Collections
* JAVA IO
* Serialization
* Socket Programming
6. * JDBC
*Projects Undertaken
1 :> Creating a LAN (Client-Server) connection via
TCP/IP using socket Programming in JAVA
2 :> serialization and Deserialization of objects using the
concept of serialization and deserialization in JAVA
* Bibliography
7. HISTORY
Java is a programming language and environment invented by James
Gosling and others in 1994. Java was originally named Oak and was
developed as a part of the Green project at the Sun Company. Java is an
object-oriented programming language with a built-in
application programming interface (API) that can handle graphics
and user interfaces and that can be used to create applications or
applets. Because of its rich set of API's, similar to Macintosh and
Windows, and its platform independence, Java can also be thought of
as a platform in itself. Java also has standard libraries for doing
mathematics. Currently, Java is used in internet programming,
mobile devices, games, e-business solutions etc.
Java Version History
There are many java versions that have been released.
Current stable release of Java is Java SE 8.
1.JDK Alpha and Beta (1995)
2.JDK 1.0 (23rd Jan, 1996)
3.JDK 1.1 (19th Feb, 1997)
4.J2SE 1.2 (8th Dec, 1998)
5.J2SE 1.3 (8th May, 2000)
6.J2SE 1.4 (6th Feb, 2002)
7.J2SE 5.0 (30th Sep, 2004)
8.Java SE 6 (11th Dec, 2006)
9.Java SE 7 (28th July, 2011)
8. 10. Java SE 8 (18th March, 2014)
Features of Java
There is given many features of java. They are also
known as java buzzwords. The Java Features given below
are simple and easy to understand.
1.Simple
2.Object-Oriented
3.Portable
4.Platform independent
5.Secured
6.Robust
7.Architecture neutral
8.Dynamic
9.Interpreted
10. Distributed High Performance
11. Multithreaded
Difference between JDK, JRE and JVM
JVM
JVM (Java Virtual Machine) is an abstract machine. It is a
specification that provides runtime environment in which
java byte code can be executed .JVMs are available for
many hardware and software platforms. JVM, JRE and
JDK are platform dependent because configuration of
9. each OS differs. But, Java is platform independent. The
JVM performs following main tasks:
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
JRE
JRE is an acronym for Java Runtime Environment. It is
used to provide runtime environment .It is the
implementation of JVM. It physically exists. It contains
set of libraries + other files that JVM uses at runtime.
JDK
JDK is an acronym for Java Development Kit. It physically
exists .It contains JRE + development tools.
Variables and Data Types in Java
Variable is a name of memory location. There are three
types of variables in java: local, instance and static.
There are two types of data types in java: primitive and
non-primitive.
Local Variable
A variable which is declared inside the method is called
local variable.
10. Instance Variable
A variable which is declared inside the class but outside
the method, is called instance variable . It is not
declared as static.
Static variable
A variable that is declared as static is called static
variable. It cannot be local.
Data Types in Java
Data types represent the different values to be stored in
the variable. In java, there are two types of data types:
o Primitive data types
o Non-primitive data types
11. o
Operators in java
Operator in java is a symbol that is used to perform
operations. For example: +, -, *, / etc.
There are many types of operators in java which are
given below:
o Unary Operator,
o Arithmetic Operator,
o shift Operator,
o Relational Operator,
o Bitwise Operator,
12. o Logical Operator,
o Ternary Operator and
o Assignment Operator.
Wrapper class in Java
Wrapper class in java provides the mechanism to convert
primitive into object and object into primitive. Since J2SE
5.0, auto boxing and unboxing feature converts primitive
into object and object into primitive automatically. The
automatic conversion of primitive into object is known as
auto boxing and vice-versa unboxing
Java classes blocks n constructors
Class in Java
A class is a group of objects which have common
properties. It is a template or blueprint from which
objects are created. It is a logical entity. It can't be
physical. A class in Java can contain:
o fields
o methods
o constructors
o blocks
o nested class and interface
Object in Java
An entity that has state and behavior is known as an
object e.g. chair, bike, marker, pen, table, car etc. It can
13. be physical or logical (tangible and intangible). The
example of intangible object is banking system.An object
has three characteristics:
o State : represents data (value) of an object.
o Behavior: represents the behavior (functionality) of
an object such as deposit, withdraw etc.
o Identity : Object identity is typically implemented
via a unique ID. The value of the ID is not visible to
the external user. But, it is used internally by the
JVM to identify each object uniquely.
Constructor in Java
Constructor in java is a special type of method that is
used to initialize the object. Java constructor is invoked
at the time of object creation. It constructs the values
i.e. provides data for the object that is why it is known as
constructor.
Types of java constructors
There are two types of constructors:
* Default constructor (no-arguments constructor)
* Parameterized constructor
Java OOPs Concepts
Object Oriented Programming is a paradigm that provides
many concepts such as inheritance, data
binding, polymorphism etc. Simula is considered as the
14. first object-oriented programming language. The
programming paradigm where everything is represented
as an object , is known as truly object-oriented
programming language.
OOPs (Object Oriented Programming
System)
Object means a real word entity such as pen, chair, table
etc. Object-Oriented Programming is a methodology or
paradigm to design a program using classes and objects.
It simplifies the software development and maintenance
by providing some concepts:
o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
Object
Any entity that has state and behavior is known as an
object. For example: chair, pen, table, keyboard, bike
etc. It can be physical and logical.
Class
Collection of objects is called class. It is a logical entity.
15. Inheritance
When one object acquires all the properties and
behaviours of parent object i.e. known as inheritance. It
provides code reusability. It is used to achieve runtime
polymorphism.
Polymorphism
When one task is performed by different ways i.e. known
as polymorphism. For example: to convince the customer
differently, to draw something e.g. shape or rectangle
etc. In java, we use method overloading and method
overriding to achieve polymorphism. Another example
can be to speak something e.g. cat speaks meaw, dog
barks woof etc.
Abstraction
Hiding internal details and showing functionality is known
as abstraction. For example: phone call, we don't know
the internal processing. In java, we use abstract class
and interface to achieve abstraction.
Encapsulation
Binding (or wrapping) code and data together into a
single unit is known as encapsulation. For example:
capsule, it is wrapped with different medicines. A java
class is the example of encapsulation. Java bean is the
fully encapsulated class because all the data members
are private here.
16. Java advanced
Java Package
A java package is a group of similar types of classes,
interfaces and sub-packages. Package in java can be
categorized in two form, built-in package and user-
defined package. There are many built-in packages such
as java, lang, awt, javax, swing, net, io, util, sql etc.
Here, we will have the detailed learning of creating and
using user-defined packages.
Advantage of Java Package
1) Java package is used to categorize the classes and
interfaces so that they can be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
17. Exception Handling in Java
The exception handling in java is one of the
powerful mechanism to handle the runtime errors so that
normal flow of the application can be maintained In java,
exception is an event that disrupts the normal flow of the
program. It is an object which is thrown at runtime.
What is exception handling
Exception Handling is a mechanism to handle runtime
errors such as ClassNotFound, IO, SQL, Remote etc.
Advantage of Exception Handling
18. The core advantage of exception handling is to maintain
the normal flow of the application. Exception normally
disrupts the normal flow of the application that is why we
use exception handling.
Types of Exception
There are mainly two types of exceptions: checked and
unchecked where error is considered as unchecked
exception. The sun microsystem says there are three
types of exceptions:
1) Checked Exception
The classes that extend Throwable class except
RuntimeException and Error are known as checked
exceptions e.g.IOException, SQLException etc. Checked
exceptions are checked at compile-time.
2) Unchecked Exception
The classes that extend RuntimeException are known as
unchecked exceptions e.g. ArithmeticException,
NullPointerException, ArrayIndexOutOfBoundsException
etc. Unchecked exceptions are not checked at compile-
time rather they are checked at runtime.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
19. Multithreading in Java
Multithreading in java is a process of executing multiple
threads simultaneously. Thread is basically a lightweight
sub-process, a smallest unit of processing.
Multiprocessing and multithreading, both are used to
achieve multitasking. But we use multithreading than
multiprocessing because threads share a common
memory area. They don't allocate separate memory area
so saves memory, and context-switching between the
threads takes less time than process. Java Multithreading
is mostly used in games, animation etc.
Advantages of Java Multithreading
1) It doesn't block the user because threads are
independent and you can perform multiple operations at
same time.
2) You can perform many operations together so it saves
time.
3) Threads are independent so it doesn't affect other
threads if exception occur in a single thread.
Java String
In java, string is basically an object that represents
sequence of char values. An array of characters works
same as java string. For example:
1.char[] ch={'j','a','v','a','t','p','o','i','n','t'};
2.String s=new String(ch);
20. is same as:
1.String s="javatpoint";
Java String class provides a lot of methods to perform
operations on string such as compare(), concat(),
equals(), split(), length(), replace(), compareTo(),
intern(), substring() etc.
The java.lang.String class
implements Serializable, Comparable and CharSequence i
nterfaces.
Java Inner Classes
Java inner class or nested class is a class which is
declared inside the class or interface. We use inner
classes to logically group classes and interfaces in one
place so that it can be more readable and maintainable.
Additionally, it can access all the members of outer class
including private data members and methods.
Java Regex
The Java Regex or Regular Expression is an API to define
pattern for searching or manipulating strings. It is widely
used to define constraint on strings such as password
and email validation. After learning java regex tutorial,
you will be able to test your own regular expressions by
the Java Regex Tester Tool. Java Regex API provides 1
interface and 3 classes in java.util.regex package.
java.util.regex package
21. It provides following classes and interface for regular
expressions. The Matcher and Pattern classes are widely
used in java regular expression.
1.MatchResult interface
2.Matcher class
3.Pattern class
4.PatternSyntaxException class
Generics in Java
The Java Generics programming is introduced in J2SE 5
to deal with type-safe objects. Before generics, we can
store any type of objects in collection i.e. non-generic.
Now generics, forces the java programmer to store
specific type of objects.
Advantage of Java Generics
There are mainly 3 advantages of generics. They are as
follows:
1) Type-safety : We can hold only a single type of objects
in generics. It doesn’t allow to store other objects.
2) Type casting is not required: There is no need to
typecast the object.
3) Compile-Time Checking: It is checked at compile time
so problem will not occur at runtime. The good
programming strategy says it is far better to handle the
problem at compile time than runtime.
22. Collections in Java
Collections in java are a framework that provides
architecture to store and manipulate the group of
objects. All the operations that you perform on a data
such as searching, sorting, insertion, manipulation,
deletion etc. can be performed by Java Collections. Java
Collection simply means a single unit of objects. Java
Collection framework provides many interfaces (Set, List,
Queue, Deque etc.) and classes (ArrayList, Vector,
LinkedList, PriorityQueue, HashSet, LinkedHashSet,
TreeSet etc.).
Java I/O
Java I/O (Input and Output) is used to process the
input and produce the output. Java uses the concept of
stream to make I/O operation fast. The java.io package
contains all the classes required for input and output
operations. We can perform file handling in java by Java
I/O API.
Stream
A stream is a sequence of data .In Java a stream is
composed of bytes. It's called a stream because it is like
a stream of water that continues to flow. In java, 3
streams are created for us automatically. All these
streams are attached with console.
1) System.out: standard output stream
23. 2) System.in: standard input stream
3) System.err: standard error stream
OutputStream
Java application uses an output stream to write data to a
destination, it may be a file, an array, peripheral device
or socket.
InputStream
Java application uses an input stream to read data from a
source, it may be a file, an array, peripheral device or
socket.
Serialization in java
Serialization in java is a mechanism of writing the state
of an object into a byte stream .It is mainly used in
Hibernate, RMI, JPA, EJB and JMS technologies .The
reverse operation of serialization is called deserialization.
Advantage of Java Serialization
It is mainly used to travel object's state on the network
(known as marshaling).
Deserialization in java
Deserialization is the process of reconstructing the object
from the serialized state.It is the reverse operation of
serialization.
24. Java Socket Programming
Java Socket programming is used for communication
between the applications running on different JRE. Java
Socket programming can be connection-oriented or
connection-less. Socket and ServerSocket classes are
used for connection-oriented socket programming and
DatagramSocket and DatagramPacket classes are used
for connection-less socket programming. The client in
socket programming must know two information:
1.IP Address of Server, and
2.Port number.
25. Socket class
A socket is simply an endpoint for communications
between the machines. The Socket class can be used to
create a socket.
Java JDBC
Java JDBC is a java API to connect and execute query
with the database. JDBC API uses jdbc drivers to connect
with the database.
Why use JDBC
Before JDBC, ODBC API was the database API to connect
and execute query with the database. But, ODBC API
uses ODBC driver which is written in C language (i.e.
platform dependent and unsecured). That is why Java
has defined its own API (JDBC API) that uses JDBC
drivers (written in Java language).
26. CREATING A LAN (CLIENT -SERVER)
CONNECTION VIA TCP/IP USING
SOCKET PROGRAMMING IN JAVA
Objective:
The server and client present on different machines can
have live chat until server says “stop” to terminate the
connection.
INTRODUCTION
In this project, I have described the steps to make a
connection in Java using Sockets. Let's start to
understand what a communication link is created via
TCP/IP. A communication link created via TCP/IP sockets
is a connection-oriented link and is reliable. This means
that the connection between the server and client
remains open throughout the duration of the dialogue
between the two and is only broken (under normal
circumstances) when one end of the dialogue formally
terminates the exchanges (like in our case when the
clients says “stop”).
27. FEATURES
* Network based application having two faces:
:> One for client
:> One for server
* It has two page source codes: one for server and one
for client.
* The machines are connected through a LAN cable.
* The machines have jre n jdk files installed on them to
run java code.
STEPS ON SERVER SIDE
* Establish a connection to the server.
* Create a ServerSocket object.
* Put the server into a waiting state.
* Set up input and output streams.
* Send and receive data.
JAVA CODE ON SERVER SIDE
import java.net.*;
import java.io.*;
class MyServer{
29. STEPS ON CLIENT SIDE
* Establish a connection to the server.
* Set up input and output streams.
* Send and receive data.
* Close the connection.
JAVA CODE ON CLIENT SIDE
import java.net.*;
import java.io.*;
class MyClient{
public static void main(String args[])throws Exception{
Socket s=new Socket("localhost",3333);
DataInputStream din=new
DataInputStream(s.getInputStream());
DataOutputStream dout=new
DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop")){
33. * Code on client side is compiled and run.
* Chat on client side
34. * Chat on server side
*Server asks client to terminate the chat.
35. * Chat terminates when client says “stop”.
EXTENSION
This concept can be used for MULTIPLE CLIENT SERVER
CHAT too using the concept of threads along with the
ones used in the above project.
36. SERIALISATION AND
DESERIALISATION OF OBJECTS
USING THE CONCEPT OF
SERIALISATION AND
DESREALISATION IN JAVA
OBJECTIVE
In this, there is a class and one program is trying to
serialize it i.e. write it on a file and later some other
program wants to it read it from the fie i.e. deserialise it
and again get back to the same form. It’s kind of a mini
project showing this concept in java.
INTRODUCTION
Serialization in java is a mechanism of writing the state
of an object into a byte stream. Writing an object on file
is serialization and reconstructing it from file is
deserialization. The file that stores the object has
extension .ser in this case the file is address.ser . In this
implementation, the state of objects is printed in the
order the objects are created.
37. FEATURES
* The class that’s implementing the built in class
Serializable is Address.
* The class that’s storing objects on the file is
WriteObject.class.
* The class that’s reading objects from the file is
ReadObject.class.
CODE FOR CLASS Address
import java.io.Serializable;
public class Address implements Serializable{
private static final long serialVersionUID = 1L;
String street;
String country;
public void setStreet(String street)
{
this.street=street;
}
public void setCountry(String country)
{
this.country=country;
38. }
public String getStreet()
{
return this.street;
}
public String getCountry()
{
return this.country;
}
@Override
public String toString()
{
return new StringBuffer("Street :")
.append(this.street)
.append("country :")
.append(this.country).toString();
}
}
39. CODE FOR CLASS WriteObject
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class WriteObject{
public static void main (String args[])
{
Address address = new Address();
Address address2 = new Address();
address.setStreet("Delhi");
address.setCountry("india");
address2.setStreet("Del");
address2.setCountry("ina");
try
{
FileOutputStream fout = new
FileOutputStream("address.ser");
ObjectOutputStream oos = new
ObjectOutputStream(fout);
oos.writeObject(address);