-
Notifications
You must be signed in to change notification settings - Fork 6
/
Java_Recap.txt
225 lines (168 loc) · 6.88 KB
/
Java_Recap.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
1. Core Java and JDBC
Recap:
1. JVM, JDK, features of Java - Open Source, Platform Independent, Object Oriented, Secured.
2. Flow Control - Conditional Construts(If, If-else if, switch) and Looping Constructs (for, while, do while).
3. break, continue
4. Operators
5. Packages - package keyword, import
6. Arrays - one dimensional and multi dimentional array
An array is an object which contains collection of elements of similar data type. Size of an array is fixed.
7. OOPS - Abstraction and Encapsulation, Inheritance, Polymorphism
Abstration - Providing relavent information about an Object
Encapsulation - Wrapping data into a single unit / Hiding data
class Employee
{
private int empId;
private String empName;
public void setEmpId(int empId)
{
this.empId = empId;
}
public int getEmpId()
{
return empId;
}
}
Inheritance - Inherting the attributes and methods of base class into derived class.
Inheritance provides reusability of code.
Inheritance is done with the keyword extends
class Manager extends Employee
{
public void getDetails()
{
}
}
Polymorphism - An object taking more that one form is called as polymorphism.
a. static polymorphism - method overloading
Method overloading means, many methods with same name but with different arguments
in the same class.The implementation in derived class will be different.
The argument must differ based on datatype, number of arguments, sequence of arguments
b. dynamic polymorphism - method overriding
Method overriding means, same method name with same arguments and same return type which
is inherited in derived class. The implementation in derived class will be different.
c. Object polymorphism
Base class variable can point to derived class object.
Employee emp = new Manager();
List l = new ArrayList();
8. Java is case sensitive.
class, interface, enum names in java - PascalCase - Eg: Employee, EmployeeDetails.
methods & attributes in java - camelCase - Eg: weight, display(), firstName, getDetails().
keywords in java - lowercase
constant in java (final) - UPPERCASE - final int AGE = 10;
9. static - we can use static modifier on a variable, method and inner class.
we also have static block.
class Test
{
public static int a = 10;
public int b = 20;
}
Test obj = new Test();
obj.b++;
obj.a++;
Test obj1 = new Test();
obj1.b++;
obj1.a++;
Static members can be called with an object of a class and also they can be called without an
object of a class.
Static block is used to intialize values for static variables.
10. abstract - class, method
A class can extends only one abstract class. and it should have is-a relationship
abstract class Employee
{
public abstract void display();
}
class Manager extends Employee
{
public void display()
{
}
}
11. Interface - A class can implement interface by using interface keyword
A class can implement any number of interfaces.
A same interface can be implemented by many classes.
interface Music
{
public void playMusic();
}
12. final - final modifier can be applied on top of variable, method and class.
final variable - constant
final method - cannot override
final class - cannot inherit
13. Constructor - Intialize Objects and intialize default values for instance variables.
We can overload a constructor
We cannot override a constructor - because constructors dont get inherited
public Employee()
{
}
14. Exception - Ubnormal condition of your application
try and catch block is used to handle exceptions.
finally block - works on both occasions - when there is an exception and when there is no exception
throw - used to throw explicit exception.
throws - used to declare that the method will throw exception
try with resource statement - try(){}
User defined exceptions - class MyExp extends Exception
15. Collections - java.util.*
Wrapper class - Boxing (primitive data type to an object) and UnBoxing (object to primitive datatype)
List - ArrayList, LinkedList
Set - HashSet, LinkedHashSet, TreeSet
Map - HashMap, LinkedHashMap, TreeMap
Deque - ArrayDeque, LinkedList
How to add user defined objects inside collection
Comparable and Comparator Interface to sort userdefined objectes added inside collection
Iterators, ListIterators, Adv for loop.
Scanner class can be used to read input from user @ run time.
16. IO - java.io.*
Byte Stream - read and write data as sequence of bytes is called as byte stream
Char Stream - read and write data as sequence of char is called as char stream
Read data - BufferedReader and InputStreamReader, readLine().
read from console - write to console
read from console - write to file
read from file - write to console
read from file - write to file
If a class name ends with reader or writer - it is char stream class
If a class name ends with stream - it is byte stream class
Serialization - Save the object and its state as a byte stream into a file.
DeSerialization - Retrieving the sequence of byte as an object from the file.
17. JDBC - Java Database Connectivity.
import java.sql.*;
Exception Handling - SQLException
1. Add the dependencies of that database server into the application - Load the database
driver class into the application
2. Establish the database connection by passing the database URL, username and password.
3. Create a SQL query and create a Statement or PreparedStatement Object, based on the
query and execute the Statement or PreparedStatement object.
4. Handle the code using try, catch - SQLExcetion
Three methods to execute the statement object - execute (Create, Alter, Drop - DDL) and (Insert, Update, Delete - DML), executeUpdate (DML), executeQuery (Select)
18. Threads - java.lang
Thread - Thread is a light weight process or subset of process.
Thread class is used to create an object of Thread.
The task for a thread will be defined inside a class which implements runnable interface and overrides run method.
Assign the runnable object to the Thread object.
new - runnable - running - dead
-blocked-
synchronized - make threads work one after the other by placing locks.
19. Enum - User defined data type for constant values.
Type safey of the variable is achieved.
enum WeekDay
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday;
}
WeekDay week = WeekDay.Monday;
20. regex - Regular Expression - validation (Pattern Matching)
Pattern, Matcher
Compile(), Matches()
21. String, StringBuilder, StringBuffer
- String is Immutable (Constant)
- Others are mutable (not a constant)
- StrinBuilder is not Thread Safe as all the methods are not synchronized.
- StringBuffer is Thread Safe as all the methods are synchronized.
22. Assertions
- check assumptions of a developer.
- assertions are used Unit testing.
- assert(boolean):"message";