Core Java
Core Java
1
Core Java Servlets & JSPs
Agenda
• Introduction
• Access Modifiers
• Operators
• Flow Control
• Arrays and Strings
• OOPS Explored
• Exceptions
• Garbage Collection
• Collections
• Threads
• Demo
2
Introduction – What is Java
• Programming language
– Another programming language using which we can develop applets,
standalone applications, web applications and enterprise applications.
• Platform Independent
– A Java program written and compiled on one machine can be
executed on any other machine (irrespective of the operating system)
• Object Oriented
– Complies to object oriented programming concepts. Your program is
not object oriented unless you code that way
3
Introduction – Java Virtual Machine
Java .class
.java file Compiler file
4
Introduction – My First Program Version 1
package com.sharadballepu.test;
5
Introduction – My First Program Version 2
package com.sharadballepu.test;
6
Introduction – My First Program Version 3
package com.sharadballepu.test;
7
Introduction – My First Program Version 4
package com.sharadballepu.test;
8
Introduction – Java Keywords
9
Introduction – Stack v/s Heap
A B
x = 10 y = new A()
method2()
method1()
C
main()
Stack
Heap
10
Introduction - Object Oriented Concepts
• Class
– A blueprint that defines the attributes and methods
• Object
– An instance of a Class
• Abstraction
– Hide certain details and show only essential details
• Encapsulation
– Binding data and methods together
• Inheritance
– Inherit the features of the superclass
• Polymorphism
– One name having many forms
11
Introduction - Data Types
float 4 - - 1.0
double 8 - - 123.86
• public
– Class can be accessed from any other class present in any package
• default
– Class can be accessed only from within the same package. Classes outside the
package in which the class is defined cannot access this class
• final
– This class cannot be sub-classed, one cannot extend this class
• abstract
– Class cannot be instantiated, need to sub-classs/extend.
• strictfp
– Conforms that all methods in the class will conform to IEEE standard rules for
floating points
14
Modifiers – Class Attributes
• public
– Attribute can be accessed from any other class present in any package
• private
– Attribute can be accessed from only within the class
• protected
– Attribute can be accessed from all classes in the same package and sub-classes.
• default
– Attribute can be accessed only from within the same package.
• final
– This value of the attribute cannot be changed, can assign only 1 value
• transient
– The attribute value cannot be serialized
• volatile
– Thread always reconciles its own copy of attribute with master.
• static
– Only one value of the attribute per class
15
Modifiers – Methods
• public
– Method can be accessed from any other class present in any package
• private
– Method can be accessed from only within the class
• protected
– Method can be accessed from all classes in the same package and sub-classes.
• default
– Method can be accessed only from within the same package.
• final
– The method cannot be overridden
• abstract
– Only provides the method declaration
• strictfp
– Method conforms to IEEE standard rules for floating points
• synchronized
– Only one thread can access the method at a time
• native
– Method is implemented in platform dependent language
• static
– Cannot access only static members. 16
Operators - Types
• Definition:
An operator performs a particular operation on the operands it is applied
on
• Types of operators
– Assignment Operators
– Arithmetic Operators
– Unary Operators
– Equality Operators
– Relational Operators
– Conditional Operators
– instaceof Operator
– Bitwise Operators
– Shift Operators
17
Operators – Assignment Operators/Arithmetic Operators
• Assignment Operator
Operator Description Example
• Arithmetic Operators
Operator Description Example
+ Addition int i = 8 + 9; byte b = (byte) 5+4;
- Subtraction int i = 9 – 4;
* Multiplication int i = 8 * 6;
/ Division int i = 10 / 2;
% Remainder int i = 10 % 3;
18
Operators – Unary Operators/Equality Operators
• Unary Operators
Operator Description Example
+ Unary plus int i = +1;
- Unary minus int i = -1;
++ Increment int j = i++;
-- Decrement int j = i--;
! Logical Not boolean j = !true;
• Equality Operators
19
Operators – Relational Operators/Conditional Operators
• Relational Operators
Operator Description Example
> Greater than if ( x > 4)
< Less than if ( x < 4)
>= Greater than or equal to if ( x >= 4)
<= Less than or equal to if ( x <= 4)
• Conditional Operators
20
Operators – instanceof Operator/Bitwise Operators/shift operators
• instanceof Operator
Operator Description Example
instanceof Instamce of If (john instance of person)
• Bitwise Operators
Operator Description Example
& Bitwise and 001 & 111 = 1
| Bitwise or 001 | 110 = 111
^ Bitwise ex-or 001 ^ 110 = 111
~ Reverse ~011 = -10
• Shift Operators
Operator Description Example
>> Right shift 4 >> 1 = 100 >> 1 = 010 = 2
<< Left Shift 4 << 1 = 100 << 1 = 1000 = 8
>>> Unsigned Right shift 4 >>> 1 = 100 >>> 1 = 010 = 2 21
Flow Control – if-else if-else
• if-else
Syntax Example
22
Flow Control – switch
• switch
Syntax Example
Result: 10
23
Flow Control – do-while / while
• do-while
Syntax Example
do { int i = 0;
// stmt-1 do {
} while (<condition>); System.out.println(“In do”); i++;
} while ( i < 10);
• while
Syntax Example
• for
Syntax Example
25
Arrays and Strings – Arrays Declaration/Construction/Initialization
• Array Declaration
int myArray[];
int[] myArray;
double[][] doubleArray;
1 2 7 5 9 0
• Array Construction
int[] myArray = new int[5];
int[][] twoDimArray = new int[5][4]
• Array Initialization 7 5 2
int[] myArray = new int[5];
8 1 3
for (int I = 0; I < 5; i++) {
myArray[i] = i++;
}
Heap
1 2 3 4 5
myArray
int[ ] myArray = {1,2,3,4,5}
27
Arrays and Strings – Strings
28
Constructors
• Abstraction
Hide certain details and show only essential details
30
OOPS Explored - Encapsulation
I want to share
my wedding gift I can share my
only with my puppy video
friends with everyone
My YouTube videos
My YouTube videos
My Cute My Wedding
My Cute My Hawaii
puppy Gift
cat trip
31
OOPS Explored - Encapsulation
• Encapsulation
Binding data and methods together
• Inheritance
Inherit the features of the superclass
33
OOPS Explored - Polymorphism
• Polymorphism
One name, many forms
s4
square
S3
shape
s2
s1 35
Exceptions – Exception Hierarchy
Throwable
Error Exception
Checked Unchecked
Exception Exception
36
Exceptions – Handling exceptions
• Try/catch/finally
public class MyClass {
public void exceptionMethod() {
try {
// exception-block
} catch (Exception ex) {
// handle exception
} finally {
//clean-up
}
}
}
37
Exceptions – Handling exceptions
• Try/catch/finally - 2
public class MyClass {
public void exceptionMethod() {
try {
// exception-block
} catch (FileNotFoundException ex) {
// handle exception
} catch (Exception ex) {
// handle exception
} finally { //clean-up }
}
}
• Using throws
public class MyClass {
public void exceptionMethod() throws Exception {
// exception-block
}
}
38
Exceptions – try-catch-finally flow
yes
no
Handle exception More exceptions Execute
In the catch block To handle? finally?
yes no
yes
Clean-up code
in finally
END
39
Garbage Collection
40
Garbage Collection
41
Garbage Collection
A a = new A();
a.Name = ‘A1’; A1 A2
a = A2;
Reassign Reference
A a = new A();
a = null; A1
Set null
B
Islands Of Isolation
A C
42
Collections - Introduction
• Data-manipulation issues
• Unnecessary code
43
Collections – Arrays v/s Collections
Arrays
Collections
44
Collections - Overview
LinkedHashSet
45
Collections – Collection types
• The sub-flavors:
Ordered - You can iterate through the collection in a specific order.
Sorted - Collection is sorted in the natural order (alphabetical for
Strings).
Unordered
Unsorted
46
Collections – Lists
• ArrayList
• Vector
Ordered collection
To be considered when thread safety is a concern.
Often used methods – add(), remove(), set(), get(), size()
• Linked List
Ordered collection.
Faster insertion/deletion and slower retrieval when compared to ArrayList
47
Collections – Set
• HashSet
Not Sorted
Not Ordered
Should be used if only requirement is uniqueness
Often used methods – add(), contains(), remove(), size()
• LinkedHashSet
Not Sorted
Ordered
Subclass of HashSet
Should be used if the order of elements is important
• TreeSet
Sorted
Ordered
Should be used if you want to store objects in a sorted fashion
Often used methods – first(), last(), add(), addAll(), subset()
48
Collections – Map
• HashMap
Not Sorted, not ordered
Can have one null key and any number of null values
Not thread-safe
Often used methods – get(), put(), containsKey(), keySet()
• Hashtable
Not Sorted, not ordered
Cannot have null keys or values
Thread-safe
• LinkedHashMap
Not sorted, but ordered
• TreeMap
Sorted, ordered
49
Threads - Introduction
method1()
thread1.start()
main() thread1.run()
Thread Creation
• There are 2 ways one can create a thread
– Extending the Thread Class
– Implementing the Runnable Interface
50
Threads - Introduction
maintenance
EXECUTION
Process report
User validation
TIME
maintenance
EXECUTION
51
TIME
Threads - Creation
public Class MyThread extends Thread public Class MyThread implements Runnable
{ {
public void run() public void run()
{ {
System.out.println(“In Thread”); System.out.println(“In Thread”);
} }
} }
To Invoke: To Invoke:
MyThread t1 = new MyThread(); MyThread t1 = new MyThread();
t1.start(); Thread t = new Thread(t1);
t.Start();
52
Threads – Thread Life cycle
T1 T2
T1
start blocked
T2 T1 T2 T1 T2
T1
runnable running end
53
Threads – Important Methods