Sei sulla pagina 1di 18

JAVA2 (CORE)

A Training Report
Submitted by:

AYUSH PARAKH
Roll no: 12115017, 5th Sem

Department of Computer Science & Engineering

National Institute of Technology, Raipur

ACKNOWLEDGMENT
I am very thankful to everyone who all supported me, for I have completed
my project effectively and moreover on time.
I would like to express my special thanks of gratitude to my teacher as well
as our principal who gave me the golden opportunity to do this wonderful
project on the topic java2 and its evolution which also helped me in doing a
lot of research and I came to know about so many new things.
I am making this project not only for marks but also to increase my
knowledge.
Thanking you
AYUSH PARAKH
12115017 , 5TH Sem

AYUSH PARAKH - 12115017

Page 1

ABSTRACT
The report consists of an overview of the famous java core
language. This project represents an in-depth tutorial of the java
language. It begins with basics, including such things as data
types, tokens (escape characters), control statements, objects
and classes. It also discusses javas Exception-Handling
mechanism, static, String, this, super, packages and interfaces.

AYUSH PARAKH - 12115017

Page 2

TABLE OF CONTENTS
Contents
I.
II.
III.

Page Number

Acknowledgement
Abstract
Table of Contents

i.
ii.
iii.

Lesson 1: Overview
I.
II.
III.
1)
2)
3)
4)

History of java
Features of java
Some java core topics
String
Static & static block
Exception-Handling
Multi-Threading

1
2
6
6
8
8
10

IV.
V.

Conclusion
References

14
15

Figures
I.
II.

Fig 1.1- Exception Handling in java


Fig 2.2- Different states of a thread

AYUSH PARAKH - 12115017

Page 3

9
11

Lesson 1: Overview
1: History of Java
Java was conceived by James gosling, Patrick Naughton, Chris Warth, Ed
frank, and Mike Sheridan at sun Microsystems, inc. in 1991. It took 18
months to develop the first working version. This language was initially
called oak, but was renamed java in 1995.

Versions

JDK 1.0 (January 21, 1996)


JDK 1.1 (February 19, 1997)
J2SE 1.2 (December 8, 1998)
J2SE 1.3 (May 8, 2000)
J2SE 1.4 (February 6, 2002)
J2SE 5.0 (September 30, 2004)
Java SE 6 (December 11, 2006)
Java SE 7 (July 28, 2011)
Java SE 8 (March 18, 2014)

2: Features of Java
Simple

AYUSH PARAKH - 12115017

Page 1

Looks familiar to existing programmers: related to C and C++:


Omits many rarely used, poorly understood, confusing features of C++, like
operator overloading, multiple inheritance, automatic coercions, etc.
Contains no goto statement, but break and continue
Has no header files and eliminated C preprocessor
Eliminates much redundancy (e.g. no structs, unions, or functions)
Has no pointer
Added features to simplify:
Garbage collection, so the programmer won't have to worry about storage
management, which leads to fewer bugs.
A rich predefined class library

Object-Oriented
Java is an object-oriented language, which means that you focus on the data in your
application and methods that manipulate that data, rather than thinking strictly in terms
of procedures.
In an object-oriented system, a class is a collection of data and methods that operate on
that data. Taken together, the data and methods describe the state and behavior of an
object. Classes are arranged in a hierarchy, so that a subclass can inherit behavior from
its super class.
Java comes with an extensive set of classes, arranged in packages that you can use in
your programs.

Robust
Java has been designed for writing highly reliable or robust software language
restrictions (e.g. no pointer arithmetic and real arrays) to make it impossible for
applications to smash memory (e.g. overwriting memory and corrupting data).Java does

AYUSH PARAKH - 12115017

Page 2

automatic garbage collection, which prevents memory leaks. Extensive compile-time


checking so bugs can be found early; this is repeated at runtime for flexibility and to
check consistency.

Secure
Security is an important concern, since Java is meant to be used in networked
environments. Without some assurance of security, you certainly wouldn't want to
download an applet from a random site on the net and let it run on your computer.
Java's memory allocation model is one of its main defenses against malicious code (e.g.
can't cast integers to pointers, so can't forge access). Furthermore access restrictions
are enforced (public, private).
Byte codes are verified, which copes with the threat of a hostile compiler.

Architecture-Neutral
Compiler generates byte codes, which have nothing to do with particular computer
architecture.
It is easy to interpret on any machine.

Portable
Java goes further than just being architecture-neutral: no "implementation dependent"
notes in the specification (arithmetic and evaluation order).
Standard libraries hide system differences.
The Java environment itself is also portable: the portability boundary is POSIX
compliant.

High-Performance

AYUSH PARAKH - 12115017

Page 3

Java is an interpreted language, so it will never be as fast as a compiled language as C


or C++. In fact, it is about 20 times as slow as C. However, this speed is more than
enough to run interactive, GUI and network-based applications, where the application is
often idle, waiting for the user to do something, or waiting for data from the network.

Multithreaded
Java allows multiple concurrent threads of execution to be active at once. This means
that you could be listening to an audio clip while scrolling the page and in the
background downloading an image. Java contains sophisticated synchronization
primitives (monitors and condition variables), that are integrated into the language to
make them easy to use and robust. The java.lang package provides a Thread class that
supports methods to start, run, and stop a thread, and check on its status.

Dynamic
Java was designed to adapt to an evolving environment:
Even after binaries have been released, they can adapt to a changing environment
Java loads in classes as they are needed, even from across the network
It defers many decisions (like object layout) to runtime, which solves many of the
version problems that C++ has. Dynamic linking is the only kind present.

Java Platform
Popular languages such as C, C++ follow the approach of write once, compile
anywhere approach. It means, to work on different microprocessors we are not required
to rewrite the program but are required to recompile the program for different MPs.
Java uses compile once, run anywhere approach. It means that a java program
after compilation can be run on any environment (i.e. different MPs and OS).This makes
Java programs immensely portable.

AYUSH PARAKH - 12115017

Page 4

Java achieves this compile once, run anywhere magic through a program called
Java Virtual Machine (JVM).When a java program is compiled, it is converted to byte
code instructions instead of machine language instruction for a specific MP. The byte
code instructions are interpreted by JVM.
Different JVMs are written specifically for different hardware and OS combinations. A
JVM is distributed along with a set of standard class libraries that implement java
programming interface.
Java byte code instructions are analogous to machine code, but are intended to be
interpreted by a virtual machine (VM) written specifically for the host hardware.
End-users commonly use a Java Runtime Environment (JRE) installed on their own
machine for standalone Java applications, or in a Web browser for Java applets.

3: Some Core Java Topics


1. String

AYUSH PARAKH - 12115017

Page 5

1)
2)
3)

The String class is a final class (cant be inherited).


Immutable (manipulations cant be done).
String s = NIT;
String s = new String(NIT);
Char a[] = {N, I, T};
String s = new String(a); // NIT

Methods Of String :s = NIT;


s.length();
// 3
s.charAt(index);
s.toLowerCase();
// nit
s.substring(str index, end index);
s1.equals(s2);

2. Static
When a member is declared static, it can be accessed before any objects
of its class are created, and without reference to any object.
They can only call other static methods, or only static data.
They cannot refer to this or super in any way.
Class usestatic
{
Static int a=3;
Static int b;
Static void meth(int x)
{

AYUSH PARAKH - 12115017

Page 6

System.out.println(x = + x);
System.out.println(a = + a);
System.out.println(b = + b);
}
Static
{
System.out.println(static block initialised.);
b=a*4;
}
public static void main(String args[])
{
meth(42);
}
}

It is a block of code declared as static and is executed as soon as the class


is loaded before the execution of any other piece of code. It basically acts
as an initialization block.

Static Block
static
{
b=10;

AYUSH PARAKH - 12115017

Page 7

a=0;
}

3. Exception-Handling
Every Exception in Java is sub type of Exception class which in turn is the
subclass of Throwable. And as we know everything in Java derived from
Object class Throwable also derived from class Object. Exception and Error
are two different classes that derived from Throwable. Errors represent
situation which doesnt occur because of Programming error but that might
happen at the time of program execution and these are abnormal behavior
which java program cannot handle and shouldnt bother to handle. JVM
running out of memory is a type of Error which can occur at runtime .

E
T
E
rx
h
rc
r
oe
o
p
rw
t
a
i
b
o
l
n
e
Fig. 1.1: Exception Handling in Java

AYUSH PARAKH - 12115017

Page 8

Checked Vs Unchecked Exception


1.

Checked exceptions are subclasses of Exception excluding

Runtime Exception and its subclasses.


2.
Checked Exceptions force programmers to deal with the exception
that may be thrown.
3.
When a checked exception occurs in a method, the method must
either catch the exception and take the appropriate action, or pass the
exception on to its caller.

Example:
// a.java
Class a
{
public static void main(string ar[])
{
int k=0;
try
{
int i = Integer.parseInt(ar[0]);
int j = Integer.parseInt(ar[1]);
k=i/j;
System.out.println(k);
}
catch(AIOOBE e)
{
System.out.println(e);
}
catch(ArithmeticException e)
{
}
catch(NumberFormatException e)
{
}
finally
{
System.out.println(k);

AYUSH PARAKH - 12115017

Page 9

}
}
}

Java handles the exception using following keywords:

try:
catch:
throw:
throws:
finally :

block in which code which can generate exception is placed .


block which catches and handles appropriate exception.
throws the exception
throws the exception to the calling method.
code which must be executed under any circumstances is placed.

4. Multi-Threading
Thread means independent flow of execution.
Multithreading is a concept in which program is broken into 2 or more threads and all
these threads run simultaneously.
Java's multithreading provides benefit in this area by eliminating the loop and polling
mechanism, one thread can be paused without stopping the other parts of the program.
If any thread is paused or blocked, still other threads continue to run.
As the process has several states, similarly a thread exists in several states. A thread
can be in the following states:

Ready to run (New): First time as soon as it gets CPU time.


Running:

Under execution.

Suspended:

Temporarily not active or under execution.

Blocked:

Waiting for resources.

Resumed:

Suspended thread resumed, and start from where it left off.

Terminated:

Halts the execution immediately and never resumes.

AYUSH PARAKH - 12115017

Page 10

Fig.1.2: different states of a thread

Thread Priorities
Each thread has its own priority in Java. Thread priority is an absolute integer value.
Thread priority decides only when a thread switches from one running thread to next,
called context switching. Priority does increase the running time of the thread or gives
faster execution.

Synchronization
Java supports an asynchronous multithreading, any number of thread can run
simultaneously without disturbing other to access individual resources at different
instant of time or shareable resources. But some time it may be possible that shareable
resources are used by at least two threads or more than two threads, one has to write at
the same time, or one has to write and other thread is in the middle of reading it. For
such type of situations and circumstances Java implements synchronization model
called monitor. The monitor was first defined by C.A.R. Hoare. You can consider the
monitor as a box, in which only one thread can reside. As a thread enter in monitor, all

AYUSH PARAKH - 12115017

Page 11

other threads have to wait until that thread exits from the monitor. In such a way, a
monitor protects the shareable resources used by it being manipulated by other waiting
threads at the same instant of time. Java provides a simple methodology to implement
synchronization.

Example:
class RunnableDemo implements Runnable
{
private Thread t;
private String threadName;
RunnableDemo(String name)
{
threadName = name;
System.out.println("Creating " + threadName );
}
public void run()
{
System.out.println("Running " + threadName );
try
{
for(int i=4;i>0;i--)
{
System.out.println("Thread: " + threadName + ", " + i);

// Let the thread sleep for a while.


Thread.sleep(50);
}
} catch(InterruptedException
{
System.out.println("Thread "
}
System.out.println("Thread "
}
public void start ()
{
System.out.println("Starting
if (t == null)

AYUSH PARAKH - 12115017

e)
+

threadName + " interrupted.");

threadName + " exiting.");

" +

threadName );

Page 12

{
t = new Thread (this, threadName);
t.start ();
}
}
}
public class TestThread {
public static void main(String args[]) {
RunnableDemo R1 = new RunnableDemo("Thread-1");
R1.start();
RunnableDemo R2 = new RunnableDemo("Thread-2");
R2.start();
}
}

4: Conclusion
Our project is an effort to understand the working of an ATM and online
banking with the use of CORE JAVA and JDBC. The project was only for
learning basis. User friendly codes have used in process of building it. The
interface is console-based and further attempts are in continued to make it
GUI.

AYUSH PARAKH - 12115017

Page 13

5.

References

http://www.java2s.com
http://www.javaworld.com/javaworld/jw-01-1998/jw-01-bookreview.html
Database Programming with JDBC and Java by O'Reilly
Head First Java 2nd Edition
http://java.sun.com/javase/technologies/desktop/
JAVA Technologies
JAVA Complete Reference
Head First EJB Sierra Bates
Software Engineering by Roger Pressman
http://www.google.com
http://www.microsoft.com
http://www.programmer2programmer.net
http://www.codeproject.com
http://www.msdn.com.
SQL Bible, 2nd Edition
Beginning Java Objects: From Concepts to Code by Jacquie Barker)
Introduction to Java Programming (NIIIT publication)
The Complete Reference Java(McGraw-Hill, Herbert Scheldt- reprint 2008)

AYUSH PARAKH - 12115017

Page 14

Potrebbero piacerti anche