OOPS THROUGH JAVA
UNIT-I
2
3
4
5
OS/Hardware
machine code
C source code
myprog.c
gcc
myprog.exe
Platform Dependent
JVM
bytecode
Java source code
myprog.java
javac
myprog.class
OS/Hardware
Platform Independent
ISSUES:
6
DIFFERENT PROGRAMMING PARADIGMS
7
SOFTWARE DESIGN APPROACHES:�TOP DOWN VS BOTTOM UP:
8
HIERARCHY OF PROGRAMMING PARADIGMS:
9
10
PROCEDURE ORIENTED PROGRAMMING
11
POP VS OOP:
12
POP : ADVANTAGES VS DISADVANTAGES
1313
14
OBJECT-ORIENTED PROGRAMMING - ADVANTAGES
15
OBJECT-ORIENTED PROGRAMMING - ADVANTAGES
16
DISADVANTAGES OF OOP:
17
OOP(S) - CONCEPTS
OOP stands for Object-Oriented Programming.
There are four main OOP concepts
18
ABSTRACTION
19
WHAT ARE CLASSES AND OBJECTS?
20
ENCAPSULATION
21
INHERITANCE
22
POLYMORPHISM
23
IN REAL LIFE - OOPS
24
25
26
WHAT IS JAVA?
27
WHY JAVA ?
28
HISTORY OF JAVA
29
JAVA VERSIONS
30
JAVA EDITIONS/PLATFORMS
31
FEATURES OF JAVA (BUZZ WORDS)
(Architecture Neutral
�
32
JAVA ENVIRONMENT
33
JDK & JAVA API:
34
JAVA TOOLS JAVA API
35
JAVA API PACKAGES:
36
JDK VS JRE VS JVM
37
JAVA VIRTUAL MACHINE (JVM)
38
JAVA VIRTUAL MACHINE
39
JAVA VIRTUAL MACHINE
40
JAVA IS BOTH COMPILED &INTERPRETED LANGUAGE
41
JAVA PORTABILITY
(Machine Specific)
c
42
WORA:
43
JAVA BYTECODE
44
45
JAVA EXECUTION IS SLOWER ? ? ?
46
47
48
49
JAVA BASICS:
50
HOW TO DOWNLOAD JAVA
51
JAVA INSTALLATION
52
SETTING UP THE ENVIRONMENT VARIABLES
53
SAMPLE JAVA PROGRAM (MYCLASS.JAVA)
import java.lang.*;
public class MyClass
{
public static void main(String[] args)
{
System.out.println("Welcome to Java World!");
}
}
54
JAVA MAIN() METHOD
55
STRUCTURE OF JAVA PROGRAM
56
57
JAVA COMMENTS (DOCUMENTATION)
// Single-Line Comment
/*
* Multiple-Line comment
*/
/**
* JavaDoc comment
*/
58
JAVA TOOLS/IDE:
1) To compile a java program:
Use javac
Example:
C:\Users\Rollno>javac MyClass.java
59
JAVA PROGRAMMING BASICS – DATA TYPES
60
DATA TYPES:
61
JAVA KEYWORDS
62
abstract | boolean | break | byte | byvalue |
case | cast * | catch | char | class |
const * | continue | default | do | double |
else | extends | false # | final | finally |
float | for | future * | generic * | goto * |
if | implements | import | inner * | instanceof |
int | interface | long | native | new |
null # | operator * | outer * | package | private |
protected | public | rest * | return | short |
static | super | switch | synchronized | this |
threadsafe * | throw | throws | transient | true # |
try | var * | void | volatile | while |
Note: Total 60=33 (exists)+27 new keywords & some( *) are reserved for future purpose,
# - defined by java
JAVA PRIMITIVE DATA TYPES
63
Default Value |
0 |
0 |
0 |
0 |
0.0 |
0.0 |
\u0000 |
false |
JAVA CODING(NAMING) CONVENTIONS - STANDARDS:
64
JAVA CODING(NAMING) CONVENTIONS - STANDARDS:
65
For example : package java.lang;
package lbrce.cse;
package lbrce.ece;
For example : RED, YELLOW,GREEN
MAX_PRIORITY,
MAX_MARKS
RED, YELLOW, MAX_PRIORITY, MAX_STOCK_COUNT
VARIABLES – TYPES:
66
HOW TO DECLARE VARIABLES?
67
TYPES OF VARIABLES IN JAVA:
68
1) LOCAL VARIABLES
69
EXAMPLE:
70
2) INSTANCE VARIABLES
71
EXAMPLE:
72
3)STATIC VARIABLES
73
EXAMPLE:
74
JAVA OPERATORS:
75
76
RELATIONAL OPERATORS:
77
Operator | Meaning |
== | Is equal to |
!= | Is not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
LOGICAL OPERATORS:
78
Operator | Meaning | Work |
&& | Logical AND | If both operands are true then only "logical AND operator" evaluate true. |
|| | Logical OR | The logical OR operator is only evaluated as true when one of its operands evaluates true. If either or both expressions evaluate to true, then the result is true. |
! | Logical Not | Logical NOT is a Unary Operator, it operates on single operands. It reverses the value of operands, if the value is true, then it gives false, and if it is false, then it gives true. |
ASSIGNMENT OPERATORS:
79
JAVA COMPOUND ASSIGNMENT OPERATORS:
80
UNARY ARITHMETIC OPERATORS:
81
Example | Description |
val = a++; | Store the value of "a" in "val" then increments. |
val = a--; | Store the value of "a" in "val" then decrements. |
val = ++a; | Increments "a" then store the new value of "a" in "val". |
val = --a; | Decrements "a" then store the new value of "a" in "val". |
BITWISE OPERATORS IN JAVA
82
BITWISE OR (|)
83
BITWISE AND (&)
84
BITWISE XOR (^)
85
BITWISE COMPLEMENT (~)
86
SHIFT OPERATORS:
87
SIGNED RIGHT SHIFT OPERATOR (>>)
88
LEFT SHIFT OPERATOR (<<)
89
UNSIGNED RIGHT SHIFT OPERATOR (>>>)
90
91
Operator | Meaning |
+= | Increments then assigns |
-= | Decrements then assigns |
*= | Multiplies then assigns |
/= | Divides then assigns |
%= | Modulus then assigns |
<<= | Binary Left Shift and assigns |
>>= | Binary Right Shift and assigns |
>>>= | Shift right zero fill and assigns |
&= | Binary AND assigns |
^= | Binary exclusive OR and assigns |
|= | Binary inclusive OR and assigns |
JAVA INSTANCEOF OPERATOR:
92
JAVA CONDITIONAL OPERATOR (TERNARY):
93
CONTROL STATEMENTS IN JAVA
94
SEQUENTIAL STATEMENTS
95
CONTROL STATEMENTS
96
CONTROL STATEMENTS - TYPES
97
SIMPLE IF & IF…ELSE & IF….ELSE…IF�(DECISION MAKING)
98
}
SWITCH…CASE:
99
LOOPS: WHILE & DO…WHILE
100
LOOPS: FOR
101
LOOPS: FOR…EACH
102
INPUT AND OUTPUT
103
1) INPUTSTREAMREADER CLASS & BUFFEREDREADER CLASS
104
1) INPUTSTREAMREADER CLASS & BUFFEREDREADER CLASS
105
1) INPUTSTREAMREADER CLASS & BUFFEREDREADER CLASS : METHODS
1) read() - java.io.BufferedReader.read() method reads a single character from this buffered reader.
2) readLine() - java.io.BufferedReader.readline() method read a line of text. A line is considered to be terminated by any one of a line feed ('\n')
106
2) SCANNER CLASS
107
2) SCANNER CLASS
108
2) SCANNER CLASS : METHODS
109
COMMAND LINE ARGUMENTS: (I/O)
110
EXAMPLE OF COMMAND-LINE ARGUMENTS
111
TYPE CONVERSION IN JAVA
112
TYPE CONVERSION IN JAVA
113
AUTOMATIC TYPE CONVERSION : EXAMPLE
114
TYPE CONVERSION IN JAVA
115
TYPE CONVERSION IN JAVA
116
JAVA ARRAYS:
117
JAVA ARRAYS: 1-DIMENSIONAL
118
JAVA ARRAYS:
119
ARRAY VARIABLES: 3 STEP PROCESS
120
1) ARRAY DECLARATION
121
2) ARRAY CONSTRUCTION
122
3) ARRAY INITIALIZATION / ASSIGNMENT
123
3) ARRAY INITIALIZATION / ASSIGNMENT
124
JAVA ARRAY INDEX
125
JAVA ARRAYS: 2-DIMENSIONAL�(MULTI DIMENSIONAL)
126
JAVA ARRAYS: 2-DIMENSIONAL�(MULTI DIMENSIONAL)
127
JAVA ARRAYS: 2-DIMENSIONAL�(MULTI DIMENSIONAL)
128
JAVA ARRAYS: 2-DIMENSIONAL
129
int[ ][ ] A = new int[3][4];
EXAMPLE TO INITIALIZE A 2D ARRAY (JAGGED ARRAY)�(DIFFERENT SIZE -ROWS)
130
2D ARRAY: REFERENCES
131
int Marks[ ][ ]=new int[4][3];
JAVA ARRAYS: 3-DIMENSIONAL�(MULTI DIMENSIONAL)
132
JAVA ARRAYS: 3-DIMENSIONAL�(MULTI DIMENSIONAL)
133
JAVA ARRAYS: 3-DIMENSIONAL�(MULTI DIMENSIONAL)
134
JAVA ARRAYS: 3-DIMENSIONAL�(MULTI DIMENSIONAL) INITIALIZAION
135
ACCESS SPECIFIERS/MODIFIERS:
136
137
ACCESS SPECIFIERS:
138
139
ACCESS SPECIFIERS:
140
CLASSES AND OBJECTS IN JAVA
141
CLASSES:
142
CLASS:
143
CLASS:
144
OBJECT:
145
OBJECT:
146
It is a basic unit of Object Oriented Programming and represents the real life entities.
A typical Java program creates many objects, which as you know, interact by invoking methods.
characteristics of an object:
State: represents the data (value) of an object.
Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.
147
148
CLASS & OBJECT:
constructor;
149
CLASS & OBJECT:
150
CLASS & OBJECT:
Advantage of Method
151
CLASS & OBJECT: SYNTAX
private int rno;
private String name;
private float cgpa;
public void input()
{
-----
}
public void display()
{
------
}
152
HOW TO INITIALIZE OBJECT: 3 WAYS
Initializing an object means storing data into the object.
153
1)BY REFERENCE VARIABLE
154
2)BY METHOD
155
3)BY CONSTRUCTOR : CONSTRUCTORS IN JAVA
156
RULES FOR CREATING JAVA CONSTRUCTOR:
NOTE:
157
TYPES OF CONSTRUCTORS:
158
A) DEFAULT CONSTRUCTOR �(NO-ARGUMENTS CONSTRUCTOR)
159
B) PARAMETERIZED CONSTRUCTOR
160
CONSTRUCTOR OVERLOADING IN JAVA
161
CONSTRUCTOR VS METHOD IN JAVA
162
Java Constructor | Java Method |
A constructor is used to initialize the state of an object. | A method is used to expose the behavior of an object. |
A constructor must not have a return type. | A method must have a return type. |
The constructor is invoked implicitly. | The method is invoked explicitly. |
The Java compiler provides a default constructor if you don't have any constructor in a class. | The method is not provided by the compiler in any case. |
The constructor name must be same as the class name. | The method name may or may not be same as the class name. |
COPY OF OBJECT 🡪 OBJECT:�(JAVA COPY CONSTRUCTOR)
There are many ways to copy the values of one object into another in Java.
163
1)BY CONSTRUCTOR:
164
3) BY ASSIGNING THE VALUES OF ONE OBJECT INTO ANOTHER:
165
3) BY CLONE() METHOD OF OBJECT CLASS
166
167
WRAPPER CLASSES:
168
USE OF WRAPPER CLASSES IN JAVA
169
AUTO BOXING & UNBOXING:�(PACKING) (UNPACKING)
170
Auto Boxing
Un Boxing
AUTO BOXING:
171
UNBOXING:
172
PRIMITIVE DATA TYPES AND CORRESPONDING WRAPPER CLASS
173
Eight classes of the java.lang package are known as wrapper classes in Java.
PRIMITIVE TO WRAPPER : EXAMPLE (AUTOBOXING)
174
WRAPPER TO PRIMITIVE:EXAMPLE (AUTOBOXING)
175
JAVA WRAPPER CLASSES EXAMPLE
176
1) CONVERSION OF PRIMITIVE DATA TYPES TO OBJECTS:
177
2) CONVERSION OF OBJECTS TO PRIMITIVE DATA TYPES :
178
3) CONVERSION OF NUMBERS(PRIMITIVE) TO STRINGS:
String s1=Integer.toString(i);
String s2=Float.toString(f);
String s3=Double.toString(d);
String s4=Long.toString(l);
179
4) CONVERSION OF STRING OBJECTS TO NUMERIC OBJECTS:
Integer ival=Integer.valueOf(s1);
Float fval=Float.valueOf(s2);
Double dval=Double.valueOf(s3);
Long lval=Long.valueOf(s4);
180
5) CONVERSION OF NUMERIC STRINGS(OBJECTS) TO PRIMITIVE TYPES(NUMBERS):
int i=Integer.parseInt(s1);
long l=Long.parseLong(s2);
float f=Float.parseFloat(s3);
double d=Double.parseDouble(s4);
181
JAVA STRING CLASS
182
STRINGS:
183
CREATE A STRING:
1) Assign a group of characters (i.e., String literal) to variable
Example-1: String s1;
s1=“hello”;
Example-2: String s1=“hello”;
2) Using ‘new’ operator (new keyword)
Example:
String s1=new String(“hello”);
3) By converting a character array into strings. (char array 🡪 String)
Example-1: Example-2: (specified characters only)
char s1[]={‘h’,’e’,’l’,’l’,’o’}; char s1[]={‘c’,’h’,’a’,’i’,’r’};
String s2= new String(s1); String s2= new String(s1,2,3); 🡪 air
184
STRING LITERAL:
185
BY NEW KEYWORD:
186
187
Note: references are stored in stack memory and objects are stored in heap memory
STRING CLASS METHODS:
1) String concatenation:
Syntax:
String concat(String s);
Example:
String s1;
s1="Hello";
String s2="World";
String s3=s1.concat(s2);
String s4=s1+s2;
188
Output:
S3- HelloWorld
S4- HelloWorld
STRING CLASS METHODS:
2) String length:
Syntax:
int length();
Example:
String s1;
s1="Hello";
int n=s1.length();
189
Output:
S1- 5
STRING CLASS METHODS:
3) Specific character:
Syntax:
char charAt(int i); // index-start with 0
Example:
String s1;
s1="Hello";
char ch=s1.charAt(1);
190
Output:
S1- e
STRING CLASS METHODS:
4) String comparison
Syntax:
int compareTo(String s1);
Example:
String s1="Hello";
String s2=“hello”;
int n=s1.compareTo(s2);
191
Output:
String comparison: -32
Note-1:
s1==s2 🡪 0
s1<s2 🡪 -ve
s1>s2 🡪 +ve
Note-2: (Ignore case)
int compareToIgnoreCase(String)
STRING CLASS METHODS:
5) String comparison - true/false
Syntax:
boolean equals(String s1);
Example:
String s1="Hello";
String s2=“hello”;
boolean b=s1.equals(s2);
192
Output:
String comparison: false
Note:
Boolean equalsIgnoreCase(String s)
STRING CLASS METHODS:
6) String Starts with- true/false
Syntax:
boolean startsWith(String s1);
Example:
String s1=“ramkumar";
String s2=“ram”;
boolean b=s1.startsWith(s2);
193
Output:
String starts with: true
Note:
boolean endsWith(String s1);
STRING CLASS METHODS:
7) Index of substring
Syntax:
int indexOf(String s1);
Example:
String s1=“ramkumar";
String s2=“kumar”;
int n=s1.indexOf(s2);
194
Output:
Index of substring:3
Note:
If doesn’t exist 🡪 -ve
STRING CLASS METHODS:
8) Last Index of substring
Syntax:
int lastIndexOf(String s1);
Example:
String s1=“This is book,is it?";
String s2=“is”;
int n=s1.lastIndexOf(s2);
195
Output:
Last Index of substring:13
Note:
If doesn’t exist 🡪 -ve
STRING CLASS METHODS:
9) String substring
Syntax:
String substring(int);
String substring(int,int);
Example:
String s1=“Welcome to java";
String s2=s1.substring(2);
196
Output:
Substring: lcome to java
Note:
String substring(2,6);
( from, to-1)
Substring: lcom
STRING CLASS METHODS:
10) String conversion : lower 🡪 upper
Syntax:
String toLowerCase();
Example:
String s1=“Welcome to java";
String s2 =s1.toUpperCase();
197
Output:
Lower to Upper:WELCOME TO JAVA
Note:
s1 =s2.toLowerCase();
Upper to Lower:welcome to java
STRING CLASS METHODS:
11) String Trim : remove spaces (begin & end)
Syntax:
String trim();
Example:
String s1=“ hello ";
String s2 =s1.trim();
198
Output:
After Trim:hello
STRING CLASS METHODS:
12) String Split: (using delimiter)
Syntax:
String split(String); //parameter is delimiter
Example:
String s1=“Hello, this is java program";
String sarr[]=s1.split(" ");
for(String i:sarr)
System.out.println(i);
199
Output:
Hello,
this
is
java
program
STRING CLASS METHODS:
13) String - character(occurrence) Replace
Syntax:
String replace(char, char)
Example:
String s1=“Hello";
s1=s1.replace(‘e’,’a’);
200
Output:
After replaceing:Hallo
JAVA STRING COMPARE
201
1) BY EQUALS() METHOD
202
2) BY = = OPERATOR
203
204
3) BY COMPARETO() METHOD
Syntax:
int compareTo(String s1);
205
Output:
String comparison: -32
Note-1:
s1==s2 🡪 0
s1<s2 🡪 -ve
s1>s2 🡪 +ve
Note-2: (Ignore case)
int compareToIgnoreCase(String)
IMMUTABILITY OF STRINGS:
206
MUTABLE VS IMMUTABLE:
207
STRINGBUFFER CLASS
208
CONSTRUCTORS OF STRINGBUFFER CLASS
209
Constructor | Description |
StringBuffer() | creates an empty string buffer with the initial capacity of 16. |
StringBuffer(String str) | creates a string buffer with the specified string. |
StringBuffer(int capacity) | creates an empty string buffer with the specified capacity as length. |
METHODS OF STRINGBUFFER CLASS:
210
STRINGBUILDER CLASS
211
CONSTRUCTORS OF STRINGBUILDER CLASS
212
Constructor | Description |
StringBuilder() | creates an empty string Builder with the initial capacity of 16. |
StringBuilder(String str) | creates a string Builder with the specified string. |
StringBuilder(int length) | creates an empty string Builder with the specified capacity as length. |
213
String | StringBuffer | StringBuilder: |
Immutable Strings are read only. String once assigned can not be changed. | Mutable | Mutable |
Storage area is SCP (String Constant Pool) | Heap Memoy | Heap Memory |
A new String is created whenever we do String manipulation. | On the same string object | On the same string object |
Consumes more memory, when we concatenate | Consumes less memory, when we concatenate | Consumes less memory, when we concatenate |
thread-safe | thread-safe | not thread-safe. |
Synchronized. String can not be used by two threads simultaneously. | Synchronized. It means two threads can't call the methods simultaneously. | Non-synchronized. It means two threads can call the methods of StringBuilder simultaneously. |
Can safely shared between multiple threads. | To be used when multiple threads are working on same String | in the single threaded environment |
More efficient when we don’t alter the value , frequent usage is there. | Less efficient than StringBuilder | More efficient than StringBuffer |
Somewhat slower | StringBuffer is faster than String when performing simple concatenations. | StringBuilder is more faster than StringBuffer. |
STRING CONCATENATION OPERATOR ( + ):
214
215