Download presentation
Presentation is loading. Please wait.
1
Advanced Java FDP on 12th June, 2017
2
Today’s Agenda Java Brush-up Session Data structures in Java
Collections in Java Generics in Java Serialization in Java Networking Database Connectivity with Java Multithreading in Java Java UI
3
Object An entity that has state, behaviour and identity is called as object. E.g.: pen, chair, table, keyboard, etc. Syntax: <Class Name> <identifier name>= new <Class Name>(); Example: Student object = new Student();
4
Class Collection of objects is called as class. It is a logical entity
Syntax: class <Class Name>{ //data members } Example: class Student{ int rollNo; String name; void studies(){ System.out.println(“Student Studies”);
5
Interface Completely incomplete class 100% incomplete class
Just has method declaration No constructor, no instance variables Cannot create object of Interface using new keyword directly Used in designing frameworks E.g.: interface RBI{ public int getRateOfInterest(); }
6
Abstract Class Partially incomplete class 0% to 100% incomplete class
Few methods are complete while few are incomplete Cannot create object of abstract class directly using new keyword Used in designing frameworks E.g.: abstract class Bike{ abstract int speed(); public void start(){ System.out.println(“Starting”); } Note: Abstract class can be an interface but not vice-versa
7
Inheritance When one object acquires all the properties and behaviour of parent object is known as inheritance Provides code reusability Class as Parent – class Child extends Parent Interface as Parent – class Child implements Parent
8
Polymorphism When one task is performed by different ways is known as polymorphism. Polymorphism is achieved by – Method Overloading Method Overriding
9
Abstraction Hiding internal details and showing functionality is known as abstraction For example: Phone call, we don’t know the internal details We use abstract class and interface to achieve abstraction
10
Encapsulation Binding (or wrapping) code and data together into a single unit is known as encapsulation. A Java class is an example of encapsulation For example: Capsule, it is wrapped with different medicines
11
Naming Convention Name Convention class name
should start with uppercase letter and be a noun e.g. String, Color, Button, System, Thread etc. interface name should start with uppercase letter and be an adjective e.g. Runnable, Remote, ActionListener etc. method name should start with lowercase letter and be a verb e.g. actionPerformed(), main(), print(), println() etc. variable name should start with lowercase letter e.g. firstName, orderNumber etc. package name should be in lowercase letter e.g. java, lang, sql, util etc. constants name should be in uppercase letter. e.g. RED, YELLOW, MAX_PRIORITY etc.
12
Java Package Package can be considered similar to folder
Package can hold - Sub-packages Similar classes Similar interfaces Advantages: Used to categorize the classes and interfaces so that they can be easily maintained Provides access protection Removes naming collision
13
Sample Java Program Sample Code - package com.main; class HelloWorld{
public static void main(){ System.out.println(“Hello World”); } Compile: javac -d . HelloWorld.java Execute: java com.main.HelloWorld
14
Pre-session intructions
How to create project in Eclipse? How to create package in Eclipse? How to create class in Eclipse? How to run a program in Eclipse? Create 2 different projects – SampleProject Assignments
15
END OF ‘Java Brush up Session’
16
Data Structures in Java
Is a particular way of organizing objects so that they can be used efficiently DS are provided by java utility package Very powerful Perform wide range of functionalities
17
Data Structures in Java continued...
It encompasses – Enumeration BitSet Vector Stack Dictionary Hashtable Properties But all these classes are legacy now All are replaced by new framework Collections
18
Enumeration Interface
Isn’t itself a Data Structure Defines the methods by which we can enumerate the elements in collection of objects Has been superseded by Iterator Methods – boolean hasMoreElements() Object nextElment()
19
BitSet Class (Refer Example1)
Creates a special type of array that holds bit values. Can increase in size as needed Constructor – BitSet() BitSet(int size) 26 methods, some of them are Methods- void and(BitSet bitSet) void or(BitSet bitSet) void set(int index) boolean isEmpty() void flip(int index) boolean get(int index)
20
Vector Class (Refer Example2)
Implements a dynamic array Similar to Array List with 2 differences – Vector is synchronized Contains many legacy methods Provides to be very useful if size of array is not known It can shrink and grow automatically using increment property Re-engineered in Collection Framework
21
Vector - Constructor & Methods
Constructors – Vector() Vector(int size) Vector(int size, int incr) Vector(Collection c) 42 Methods, some of them are – void addElement(Object obj) int capacity() void clear() boolean contains(Object obj) Enumeration elements() Object firstElement() Object remove(int index)
22
Stack Class (Refer Example3)
Is a sub-class of vector Implements standard last-in, first-out stack Re-engineered in Collection framework Constructor – Stack() Includes all methods of vectors and few of its own – boolean empty() Object peek() Object pop() Object push(Object element) int search(Object element)
23
Dictionary Abstract Class
Represents a key-value storage repository Once the value is stored can be retrieved using key Similar to Map Methods – Enumeration elements() Object get(Object Key) boolean isEmpty() Enumeration keys() Object put(Object key, Object value) Object remove(Object key) int size()
24
Hashtable Class (Refer Example4)
Is a concrete implementation of Dictionary Stores key-value pairs The key is hashed, and the resultant hashcode is the index where the value is stored Re-engineered in Collection Framework
25
Hashtable – constructors and methods
Hashtable(int size) Hashtable(int size, float fillRatio) Hashtable(Map<? extends K, ? extends V > t) Methods (All methods from Dictionary and few of its own)- void clear() Object clone() boolean contains(Object value) boolean containKey(Object key) void rehash()
26
Properties Class (Refer Example5)
Is a sub-class of Hashtable Used to maintain key-value pairs where key and value both are String Constructors – Properties() Properties(Properties getDefault) All methods from Hashtable and few of its own – String getProperty(String key) void list(PrintStream streamOut) void load(InputStream streamIn) throws IOException Enumeration propertyNames() Object setProperty(String key, String value)
27
END OF ‘Java Data Structures’
28
Collections Framework in Java
Is a framework that provides an architecture to store and manipulate the group of objects All the operations can be performed on the group of objects as like data Simply means a single unit of object Interfaces in Collection framework– List, Set, Queue, Dequeue Classes implementing above interfaces – ArrayList, LinkedList, Vector, PriorityQueue, HashSet, LinkedHashSet, TreeSet, etc.
29
Goals achieved by Collection Framework
High Performance Framework Highly Efficient Allows different types of collections to work in a similar manner (central, unifying theme) High degree of interoperability One collection can adapt/extend other collection easily
30
Hierarchy of Collection Framework
31
Iterator Interface Provides the facility of iterating the elements in forward direction only Methods- public boolean hasNext() public Object next() public void remove()
32
Collection Interface Methods – public boolean add(Object element)
public boolean addAll(Collection c) public boolean remove(Object element) public boolean removeAll(Collection c) public boolean retainAll(Collection c) public int size() public void clear() public boolean contains(Object element) pubic boolean containsAll(Collection c) public Iterator iterator() public Object[] toArray() public boolean isEmpty() public boolean equals(Collection c) public int hashCode()
33
List Interface Is a sub-interface of Collection interface
It contains method to insert and delete elements on index basis Methods – public void add(int index, Object o) public boolean addAll(int index, Collection c) public Object get(int index) public Object set(int index, Object o) public Object remove(int index) public ListIterator listIterator() public ListIterator listIterator(int index)
34
ArrayList Class (Refer Example6)
Uses a dynamic array for storing the elements Inherits AbstractList class and implements List interface Important Points – Can contain duplicate elements Maintains insertion order Is non-synchronized Allows random access because array works at the index basis Manipulation is slow since, shifting of elements occur on deletion of any element
35
ArrayList – Constructors and Methods
ArrayList(Collection c) ArrayList(int capacity) Methods – void add(int index, Object element) boolean addAll(Collection c) void clear() int lastIndexOf(Object o) Object[] toArray() boolean add(Object o) Object clone() int indexOf(Object o) void trimToSize()
36
LinkedList Class (Refer Example7)
Uses doubly linked list to store the elements It extends the AbstractList class and implements List and Deque interfaces Important Points – Can contain duplicate elements Maintains insertion order Is non-synchronized Manipulation is fast because no shifting needs to be occurred Can be used as list, sack or queue
37
LinkedList – Constructor and Methods
LinkedList(Collection c) Methods – void add(int index, Object element) void addFirst(Object o) void addLast(Object o) int size() boolean add(Object o) boolean contains(Object o) boolean remove(Object o) Object getFirst() Object getLast() int indexOf(Object o) int lastIndexOf(Object o)
38
ArrayList v/s LinkedList
Uses Dynamic Array to store elements Uses doubly linked list to store elements Manipulation is slow Manipulation is fast Can act as list only Can act as list and queue both Better for storing and accessing Better for manipulating data
39
HashSet Class (Refer Example8)
Used to create a collection that uses a hash table for storage Inherits AbstractSet class and implements Set interface Important Points – Stores elements by hashing Contains unique elements only Does not main insertion order Difference between List and Set List can contain duplicate elements Set contains unique elements only
40
HashSet – constructor and methods
HashSet(Collection c) HashSet(int capacity) Methods – void clear() boolean contains(Object o) boolean add(Object o) boolean isEmpty() boolean remove(Object o) Object clone() Iterator iterator() int size()
41
LinkedHashSet class (Refer Example9)
Is a hash table and linked list implementation of the Set interface Inherits HashSet class and implements Set interface Important Points – Contains unique elements Permits null elements Maintains insertion order
42
LinkedHashSet - constructor and methods
HashSet(Collection c) LinkedHashSet(int capacity) LinkedHashSet(int capacity, float fillRatio) Methods – All methods of HashSet
43
TreeSet Class (Refer Example10)
Implements the Set interface which uses a tree for storage Inherits AbstractSet class and implements NavigableSet interface Objects of TreeSet class are stored in ascending order Important Points – Contains unique elements Access and retrieval times are quite fast Maintains in ascending order
44
TreeSet – constructor and methods
TreeSet(Collection c) TreeSet(Comparator comp) TreeSet(SortedSet ss) Methods – boolean addAll(Collection c) boolean contains(Object o) boolean isEmpty() boolean remove(Object o) void add(Object o) void clear() Object clone() Object first() Object last() int size()
45
Queue Interface Orders the element in the FIFO manner Methods –
boolean add(Object o) boolean offer(Object o) Object remove() Object poll() Object element() Object peek()
46
PriorityQueue Class (Refer Example11)
Provides the facility of using queue But does not orders the elements in FIFO order Inherits AbstractQueue class Methods – Same as Queue interface
47
Deque interface Is a linear collection that supports element insertion and removal at both ends It represents Double Ended Queue Methods – boolean add(Object o) boolean offer(Object o) Object remove() Object poll() Object element() Object peek()
48
ArrayDeque Class (Refer Example12)
Provides the facility of using deque and resizable-array Inherits AbstractCollection class and implements Deque interface Important Points – Can add and remove elements from both ends Null elements are not allowed Not thread safe No capacity restriction Is faster than LinkedList and Stack
49
Map Interface Contains values on the basis of key
Each key-value pair is called as an entry Map contains only unique keys Useful if we have to search, update and delete on the basis of key Methods – Object put(Object key, Object value) void putAll(Map map) Object remove(Object key) Object get(Object key) boolean containsKey(Object key) Set keySet() Set entrySet()
50
Map.Entry interface Entry is a sub-interface of Map
Hence, should be accessed as Map.Entry Provides methods to get key and value Methods – Object getKey() Object getValue()
51
HashMap Class (Refer Example13)
Implements Map interface by using a hashtable Inherits AbstractMap class and implements Map interface Important Points – Contains value based on the key Only unique elements Have one null key and multiple null values Maintains no order
52
HashMap – constructors and methods
HashMap(Map m) HashMap(int capacity) HashMap(int capacity, float fillRatio) Methods – void clear() boolean containsKey(Object key) boolean containsValue(Object value) boolean isEmpty() Object clone() Set entrySet() Set keySet() Object put(Object key, Object value) int size() Collection values()
53
LinkedHashMap Class (Refer Example14)
Is a Hash table and Linked List implementation of Map It inherits HashMap class and implements Map interface Important Points – Contains values based on key Contains only unique elements May have one null key and multiple null values Same as HashMap instead maintains insertion order
54
LinkedHashMap – constructor and methods
LinkedHashMap(int capacity) LinkedHashMap(int capacity, float fillRatio) LinkedHashMap(Map m) Methods – Object get(Object key) void clear() boolean containsKey(Object key)
55
TreeMap Class (Refer Example15)
Implements the Map interface by using tree Provides an efficient means of storing key- value pairs in sorted order Implements the NavigableMap interface and extends AbstractMap class Important Points – Contains only unique element Cannot have null key but can have multiple null values Same as HashMap instead maintains ascending order
56
TreeMap – constructor and method
TreeMap(Comparator comp) TreeMap(Map m) TreeMap(SortedMap sm) Methods – boolean containsKey(Object key) boolean containsValue(Object value) Object firstKey() Object get(Object key) Object lastKey() Object remove(Object key) void putAll(Map map) Set entrySet() int size() Collection values()
57
HashMap v/s TreeMap HashMap TreeMap Can contain one null key
Cannot contain any null key Maintains no order Maintains ascending order
58
HashMap v/s HashTable HashMap Hashtable
Non-synchronized, hence not thread safe Synchronized, hence thread safe Allows one null key and multiple null values Neither key nor value null is allowed Newly introduced Legacy class Fast Slow Traversed by Iterator Traversed by Enumerator and Iterator Inherits AbstractMap class Inherits Dictionary class
59
END OF ‘Collection Framework’
60
Generics in Java Introduced to deal with type-safe objects
Before generics -> able to store any type of objects in collection After generics -> forces to store specific type of objects in collection Advantages – Type-safety Type casting is not required Compile-Time checking Syntax – ClassORInterface<Type> Example – ArrayList<String>
61
Generic Class (Refer Example16)
A class that can refer to any type is known as generic class Example – class MyGen<T>{ T obj; void add(T obj){ this.obj = obj; } T get(){ return obj; ‘T’ indicates that class can refer to any type (like String, Integer, Employee, etc.)
62
Type Parameters T – Type E – Element K – Key N – Number V - Value
63
Generic Method (Refer Example17)
A method that can accept any type of argument is called as Generic method Example – class TestGenerics{ public <E> void printArray(E[] elements){ }
64
Wild Card in Generics (Refer Example18)
The ? (question mark) symbol represents Wild card element It means any type If we write <? extends Number>, it means any child class of Number Take Example
65
END OF ‘Generics in Java’
66
Serialization in Java Is a mechanism of writing the state of an object into a byte stream Mainly used in Hibernate, RMI, JPA, EJB and JMS technologies Reverse operation is know as Deserialization
67
Advantages of Serialization
Mainly used to travel object’s state on network (Known as Marshalling)
68
Serializable interface
Belongs to java.io package Is a marker interface What is marker interface? Has no data member and no method is defined Used to “mark” java classes so that objects of that class may get certain capability Cloneable and Remote also are marker interfaces So if a class is marked Serialize, its object can be converted to stream
69
Example Serializable Student Example
Student class implements Serializable interface Hence marked as Serializable Its objects can be converted into Stream
70
How to convert objects into stream?
Converting object into stream is Serialization ObjectOutputStream class is used to – write primitive data types to an OutputStream Java objects to an OutputStream Only objects of classes which are marked Serializable are supported Example of writing Student object to File
71
How to convert stream into object?
Converting stream back to object is Deserialization Deserialization is process of reconstructing the object from the serialized state ObjectInputStream class is used to – Deserialize java objects which are serialized by ObjectOutputStream Deserialize prmitive data type which written using ObjectOutputStream Example of Deserializing Student object from File
72
END OF ‘Serialization in Java’
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.