Skip to content

Commit b909d21

Browse files
committed
Update README.md
1 parent a67b971 commit b909d21

File tree

1 file changed

+66
-61
lines changed

1 file changed

+66
-61
lines changed

README.md

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
* [Java Abstraction](#-17-java-abstraction)
4242
* [Java Interfaces](#-18-java-interfaces)
4343
* [Java Encapsulation](#-19-java-encapsulation)
44-
* [Miscellaneous](#-20-miscellaneous)
44+
* [Java Generics](#-20-java-generics)
45+
* [Miscellaneous](#-21-miscellaneous)
4546

4647
<br/>
4748

@@ -4026,7 +4027,70 @@ public class MainClass {
40264027
<b><a href="#related-topics">↥ back to top</a></b>
40274028
</div>
40284029

4029-
## # 20. MISCELLANEOUS
4030+
## # 20. JAVA GENERICS
4031+
4032+
<br/>
4033+
4034+
## Q. Do you know Generics? How did you used in your coding?
4035+
4036+
`Generics` allows type (Integer, String, … etc and user defined types) to be a parameter to methods, classes and interfaces. For example, classes like HashSet, ArrayList, HashMap, etc use generics very well.
4037+
4038+
**Advantages:**
4039+
4040+
* **Type-safety**: We can hold only a single type of objects in generics. It doesn\'t allow to store other objects.
4041+
* **Type Casting**: There is no need to typecast the object.
4042+
* **Compile-Time Checking**: It is checked at compile time so problem will not occur at runtime.
4043+
4044+
**Example:**
4045+
4046+
```java
4047+
/**
4048+
* A Simple Java program to show multiple
4049+
* type parameters in Java Generics
4050+
*
4051+
* We use < > to specify Parameter type
4052+
*
4053+
**/
4054+
class GenericClass<T, U> {
4055+
T obj1; // An object of type T
4056+
U obj2; // An object of type U
4057+
4058+
// constructor
4059+
GenericClass(T obj1, U obj2) {
4060+
this.obj1 = obj1;
4061+
this.obj2 = obj2;
4062+
}
4063+
4064+
// To print objects of T and U
4065+
public void print() {
4066+
System.out.println(obj1);
4067+
System.out.println(obj2);
4068+
}
4069+
}
4070+
4071+
// Driver class to test above
4072+
class MainClass {
4073+
public static void main (String[] args) {
4074+
GenericClass <String, Integer> obj =
4075+
new GenericClass<String, Integer>("Generic Class Example !", 100);
4076+
4077+
obj.print();
4078+
}
4079+
}
4080+
```
4081+
4082+
Output:
4083+
4084+
```java
4085+
Generic Class Example !
4086+
100
4087+
```
4088+
4089+
<div align="right">
4090+
<b><a href="#related-topics">↥ back to top</a></b>
4091+
</div>
4092+
4093+
## # 21. MISCELLANEOUS
40304094
40314095
<br/>
40324096
@@ -4424,65 +4488,6 @@ There are 4 types of JDBC drivers:
44244488
<b><a href="#related-topics">↥ back to top</a></b>
44254489
</div>
44264490
4427-
## Q. Do you know Generics? How did you used in your coding?
4428-
4429-
`Generics` allows type (Integer, String, … etc and user defined types) to be a parameter to methods, classes and interfaces. For example, classes like HashSet, ArrayList, HashMap, etc use generics very well.
4430-
4431-
**Advantages:**
4432-
4433-
* **Type-safety**: We can hold only a single type of objects in generics. It doesn\'t allow to store other objects.
4434-
* **Type Casting**: There is no need to typecast the object.
4435-
* **Compile-Time Checking**: It is checked at compile time so problem will not occur at runtime.
4436-
4437-
**Example:**
4438-
4439-
```java
4440-
/**
4441-
* A Simple Java program to show multiple
4442-
* type parameters in Java Generics
4443-
*
4444-
* We use < > to specify Parameter type
4445-
*
4446-
**/
4447-
class GenericClass<T, U> {
4448-
T obj1; // An object of type T
4449-
U obj2; // An object of type U
4450-
4451-
// constructor
4452-
GenericClass(T obj1, U obj2) {
4453-
this.obj1 = obj1;
4454-
this.obj2 = obj2;
4455-
}
4456-
4457-
// To print objects of T and U
4458-
public void print() {
4459-
System.out.println(obj1);
4460-
System.out.println(obj2);
4461-
}
4462-
}
4463-
4464-
// Driver class to test above
4465-
class MainClass {
4466-
public static void main (String[] args) {
4467-
GenericClass <String, Integer> obj =
4468-
new GenericClass<String, Integer>("Generic Class Example !", 100);
4469-
4470-
obj.print();
4471-
}
4472-
}
4473-
```
4474-
4475-
Output:
4476-
4477-
```java
4478-
Generic Class Example !
4479-
100
4480-
```
4481-
4482-
<div align="right">
4483-
<b><a href="#related-topics">↥ back to top</a></b>
4484-
</div>
4485-
44864491
## Q. What additional methods for working with associative arrays (maps) appeared in Java 8?
44874492
44884493
* `putIfAbsent()` adds a key-value pair only if the key was missing:

0 commit comments

Comments
 (0)