Professional Documents
Culture Documents
Rajeev Gupta
MTech CS
Contents at a glance
Java An Introduction
OO Introduction
Inheritance
Java Thread
Java Collections
rgupta.mtech@gmail.com
Day-1
Session-1
Introduction to Java
Session-2
Object Oriented
Abstraction,
Inheritence,
Procedural Programming
Polymorphism,message
passing
If..else,switch, looping
Array
Examples
rgupta.mtech@gmail.com
Day-2
Session-1
Class and objects
Creating Classes
Implementing Methods
Object: state, identity and behaviour
Constructors: default, parameterized and copy
Instance variable, static variable and local
variable
Initialization block
Static method and applications
Packages and visibility modifier: public,
protected, private and default
rgupta.mtech@gmail.com
Session-2
Inheritance
Type of inheritance, diamond problem
InstanceOf operator
Final
Final variable, final method , final class
Acess control: public,
private,protected and default
Packages
Abstract class
Interface
Polymorphism
Overloading
Overriding
Type of relationship bw objects
IS-A, HAS-A, USE-A
Day-3
Session-1
Session-2
Exception handling
Try, catch, throw, throws,
finally
Checked and
Unchecked exception
User defined exceptions
Inner classes
assertions
rgupta.mtech@gmail.com
Day-4
Session-1
IO
Session-2
Multi-Threading
File handling
Synchronization
rgupta.mtech@gmail.com
Day-5
Session-1
Collections Framework
List, Map, Set usages introduction
Session-2
Java 5 Language Features II
Generics
Annotations
rgupta.mtech@gmail.com
DAY-1
rgupta.mtech@gmail.com
Session-1
Introduction to Java
If..else,switch, looping
Array
Examples
rgupta.mtech@gmail.com
What is Java?
Java=OOPL+JVM+lib
rgupta.mtech@gmail.com
Simple
Object Oriented
Portable
Architecture Neutral
Distributed
Multi-threaded
Robust, Secure/Safe
Interpreted
High Performance
rgupta.mtech@gmail.com
JavaPlatform
rgupta.mtech@gmail.com
Versions
CalledJava2
Versionforthesession
rgupta.mtech@gmail.com
JDK
Byte Code
Platform independent
JRE
Platform dependent
JVM
Platform dependent
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
Understanding JVM
rgupta.mtech@gmail.com
Hello World
Lab set-up
Java 1.7
Eclipse 3.6 ( eclipse helios)
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
Interface Statements
Class Declarations
Main Method Class
{
}
rgupta.mtech@gmail.com
2 type
rgupta.mtech@gmail.com
boolean
char
byte
short
int
long
float
double
rgupta.mtech@gmail.com
Procedural Programming
if..else
switch
Looping; for, while, do..while as usual in Java as in
C/C++
Dont mug the program/logic
Follow dry run approach
program
Prime No check
Date calculation
rgupta.mtech@gmail.com
Array
Session-2
Encapsulation
Abstraction
modularity
Inheritance/ Hierarchy
Polymorphism, message passing
rgupta.mtech@gmail.com
Object technologies
rgupta.mtech@gmail.com
What is an Object?
rgupta.mtech@gmail.com
Class
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
Basic principles of OO
rgupta.mtech@gmail.com
Abstraction
rgupta.mtech@gmail.com
Encapsulation
rgupta.mtech@gmail.com
Modularity
rgupta.mtech@gmail.com
Hierarchy
rgupta.mtech@gmail.com
Most important once are class diagram, use case diagram and
sequence diagram.
rgupta.mtech@gmail.com
DAY-2
rgupta.mtech@gmail.com
Session-1
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
CreatingClassesandobject
rgupta.mtech@gmail.com
Correct way?
rgupta.mtech@gmail.com
Constructors:default,parameterizedandcopy
Can be private
Cant be static*
Default,
Parameterized and
Copy constructor
rgupta.mtech@gmail.com
Need of this ?
Which id assigned to
which id?
this is an reference to
the current object required
to differentiate local
variables with instance
variables
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
Calling one
constructor from
another ?
rgupta.mtech@gmail.com
Static method/variable
rgupta.mtech@gmail.com
Initializationblock
We can put
repeated
constructor code
in an Initialization
block
Static Initialization
block runs before
any constructor
and runs only
rgupta.mtech@gmail.com
once
Initialization block
rgupta.mtech@gmail.com
Packages
rgupta.mtech@gmail.com
java.lang
java.util
java.net
java.awt
java.io
java.applet
rgupta.mtech@gmail.com
Visibility Modifiers
Public
Protected
Default (package level)
Private
For classes
rgupta.mtech@gmail.com
Visibility Modifiers
rgupta.mtech@gmail.com
Visibility Modifiers
Accessibleto:
public
protected
Package
(default)
private
SameClass
Yes
Yes
Yes
Yes
Classinpackage
Yes
Yes
Yes
No
Subclassin
differentpackage
Yes
Yes
No
No
Nonsubclass
differentpackage
Yes
No
No
No
rgupta.mtech@gmail.com
boolean b = stdin.hasNextInt()
rgupta.mtech@gmail.com
Call by value
rgupta.mtech@gmail.com
Call by reference
When you pass an object in an method copy of reference is passed so that we can mutate the
state of the object but cant delete original object itself
rgupta.mtech@gmail.com
Session-2
Inheritance
InstanceOf operator
Final
Packages
Abstract class
Interface
Polymorphism
Overloading
Overriding
rgupta.mtech@gmail.com
Inheritance
Inheritance is the
inclusion of
behaviour (i.e.
methods) and state
(i.e. variables) of a
base class in a
derived class so that
they are accessible in
that derived class.
code reusability.
Diamond Problem?
rgupta.mtech@gmail.com
Inheritance example
rgupta.mtech@gmail.com
Overloading
Overloading deals with multiple methods in the same class with the same name but
different method signatures.
Both the above methods have the same method names but different method
signatures, which mean the methods are overloaded.
Overloading lets you define the same operation in
different ways for different data.
Overriding
Overriding deals with two methods, one in the parent class and
the other one in the child class and has the same name and
signatures.
Both the above methods have the same method names and the
signatures but the method in the subclass MyClass overrides the
method in the superclass BaseClass
rgupta.mtech@gmail.com
Polymorphism
rgupta.mtech@gmail.com
Polymorphism
rgupta.mtech@gmail.com
Example
rgupta.mtech@gmail.com
Sound( ) method of
Animal class dont make
any sense i.e. it dont
have semantically valid
definition
Abstract class
If an class have at
least one abstract
method it should be
declare abstract class.
rgupta.mtech@gmail.com
More example
rgupta.mtech@gmail.com
Final
final
Final variable
Become
Final method
Cant
overridden
Final class
Can
Some examples.
rgupta.mtech@gmail.com
Final class
rgupta.mtech@gmail.com
Final Method
rgupta.mtech@gmail.com
Interface?
Interface method
Only declaration
No method definition
Interface variable
Public static and final constant
Its how java support global constant
Callback in Java*
Some Example .
rgupta.mtech@gmail.com
Interface?
Rules
All interface methods are always public and abstract, whether we say it
or not.
Variable declared inside interface are always public static and final
rgupta.mtech@gmail.com
Implementing an
interface
rgupta.mtech@gmail.com
Note
int i=90;
public static int i=90;
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
Type of relationship bw
objects
USE-A
HAS-A
IS-A (Most costly ? )
IS-A
rgupta.mtech@gmail.com
VS
HAS-A
DAY-3
rgupta.mtech@gmail.com
Session-1
String class
Varargs
Static Import
Enums
Inner classes
rgupta.mtech@gmail.com
String
Only class in java for which object can be created with or without
using new operator
Ex:
String s=india;
String s1=new String(india); What is the difference?
rgupta.mtech@gmail.com
Immutability
Immutabilitymeanssomethingthatcannotbechanged.
StringsareimmutableinJava.Whatdoesthismean?
String literals are very heavily used in applications and
they also occupy a lot of memory.
Therefore for efficient memory management, all the strings
are created and kept by the JVM in a place called string
pool (which is part of Method Area).
Garbage collector does not come into string pool.
How does this save memory?
rgupta.mtech@gmail.com
charAt()
concat()
toUpperCase()
toString()
toLowerCase()
substring()
replace()
length()
equalsIgnoreCase()
trim()
rgupta.mtech@gmail.com
String comparison
rgupta.mtech@gmail.com
Wrapper classes
Boxing
Integer iWrapper = 10;
Prior to J2SE 5.0, we use
Integer a = new Integer(10);
Unboxing
int iPrimitive = iWrapper;
Prior to J2SE 5.0, we use
int b = iWrapper.intValue();
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
Varargs
rgupta.mtech@gmail.com
Static Import
static java.lang.Math.*;
rgupta.mtech@gmail.com
Enums
rgupta.mtech@gmail.com
Inner classes
rgupta.mtech@gmail.com
Inner class?
Inner classes
Top level inner class
Method local inner
class
Anonymous Inner
class
Method local
argument inner class
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
On the fly
rgupta.mtech@gmail.com
Session-2
Exception handling
Assertions
rgupta.mtech@gmail.com
What is Exception?
An exception is an abnormal condition that arises while
running a program.
Examples:
rgupta.mtech@gmail.com
Type of exceptions
Unchecked Exception
1.
Checked Exception
2.
Error
3.
rgupta.mtech@gmail.com
Exception key
words
Try
Catch
Throws
Throw
Finally
Exception Hierarchy
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
ArithmeticException
ArrayIndexOutOfBoundException
ArrayStoreException
FileNotFoundException
IOException general I/O failure
NullPointerException referencing a null object
OutOfMemoryError
SecurityException when applet tries to perform an
action not allowed by the browsers security setting.
StackOverflowError
StringIndexOutOfBoundException
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
DAY-4
rgupta.mtech@gmail.com
Session-1
File handling
Object Serialization
[ObjectInputStream / ObjectOutputStream]
rgupta.mtech@gmail.com
Stream
All streams behave in the same manner, even if the actual physical
devices to which they are linked differ.
2 types of streams:
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
File
boolean flag=
file.createNewFile();
boolean flag=file.exists();
FileWriter
File file = new File( "fileWrite2.txt");
FileWriter fw =new FileWriter(file);
fw.write("howdy\nfolks\n"); // write characters to
fw.flush(); // flush before closing
fw.close(); // close file when done
rgupta.mtech@gmail.com
FileReader
char[]in=newchar[50];//tostoreinput
intsize=0;
FileReaderfr=newFileReader(file);
size=fr.read(in);//readthewholefile!
System.out.print(size+"");//howmany
bytesread
for(charc:in)//printthearray
System.out.print(c);
fr.close();//again,alwaysclose
Serialization
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
De- Serialization
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
Session-2
Multi-Threading
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
Basic fundamentals
rgupta.mtech@gmail.com
Creating threads in
Java?
rgupta.mtech@gmail.com
If a thread is blocked
( put to sleep) specified
no of ms should expire
If an thred is
suspended() some one
must call resume()
rgupta.mtech@gmail.com
Java.lang.Thread
Thread()
void run()
boolean isAlive()
must be overriden
void start()
void stop()
void suspend() and void resume()
rgupta.mtech@gmail.com
Java.lang.Thread
void join()
Main thread Wait for specified thread to complete
boolean isInterrupted()
void interrupt()
Send interrupt request to a thread
rgupta.mtech@gmail.com
Creating Threads
rgupta.mtech@gmail.com
Understanding join()
method
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
Understandingthreadsynchronization
rgupta.mtech@gmail.com
Synchronization
Synchronization
Mechanism to controls the order in which threads
execute
Competition vs. cooperative synchronization
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
wait()
notify()
notifyAll()
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
DAY-5
rgupta.mtech@gmail.com
Session-1
Session-1
HashSet
rgupta.mtech@gmail.com
Object
rgupta.mtech@gmail.com
String toString()
boolean equals()
int hashCode()
clone()
void finalize()
getClass()
toString( )
If we do not override
toString() method of Object
class it print Object
Identification number by
default
rgupta.mtech@gmail.com
toString()
rgupta.mtech@gmail.com
equals
Overriding equals()
rgupta.mtech@gmail.com
hashCode()
rgupta.mtech@gmail.com
clone()
rgupta.mtech@gmail.com
finalize()
Demo programm
WAP to count total number of employee object in the memory at any moment of time if an object is nullified then reduce
count.
rgupta.mtech@gmail.com
Java collection
collection
Name
Collection
Base
of topic
interface
Collections
Static
rgupta.mtech@gmail.com
collection
collection
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
Implementing Comparable
rgupta.mtech@gmail.com
Comparator
rgupta.mtech@gmail.com
Useful stuff
rgupta.mtech@gmail.com
Useful examples
rgupta.mtech@gmail.com
Useful examples
rgupta.mtech@gmail.com
LinkedList : AKA Doubly Link list..
can move back and forth.......
rgupta.mtech@gmail.com
Useful examples
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
ArrayList vs LinkedList
list.remove(0);
if u remove first element, java internally copy all the
element (shift by one)
Adding element at middle in ArrayList is very
inefficient
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
HashMap
Key ---->Value
declaring an hashmap
HashMap<Integer,
String>();
Populating values
map.put(5, "Five");
map.put(8, "Eight");
map.put(6, "Six");
map.put(4, "Four");
map.put(2, "Two");
Getting value
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
LinkedHashMap
TreeMap
for string
"a","b".............
rgupta.mtech@gmail.com
set
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
HashMap vs Hashtable
rgupta.mtech@gmail.com
Session-2
rgupta.mtech@gmail.com
Generics
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
Polymorphic behaviour
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
rgupta.mtech@gmail.com
Generic class
rgupta.mtech@gmail.com
Generic method
rgupta.mtech@gmail.com
Annotation
@ magic !!!
Override
Deprecated
SuppressWarnings
rgupta.mtech@gmail.com
References:-
rgupta.mtech@gmail.com