|
41 | 41 | * [Java Abstraction](#-17-java-abstraction)
|
42 | 42 | * [Java Interfaces](#-18-java-interfaces)
|
43 | 43 | * [Java Encapsulation](#-19-java-encapsulation)
|
44 | | -* [Miscellaneous](#-20-miscellaneous) |
| 44 | +* [Java Generics](#-20-java-generics) |
| 45 | +* [Miscellaneous](#-21-miscellaneous) |
45 | 46 |
|
46 | 47 | <br/>
|
47 | 48 |
|
@@ -4026,7 +4027,70 @@ public class MainClass {
|
4026 | 4027 | <b><a href="#related-topics">↥ back to top</a></b>
|
4027 | 4028 | </div>
|
4028 | 4029 |
|
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 |
4030 | 4094 |
|
4031 | 4095 | <br/>
|
4032 | 4096 |
|
@@ -4424,65 +4488,6 @@ There are 4 types of JDBC drivers:
|
4424 | 4488 | <b><a href="#related-topics">↥ back to top</a></b>
|
4425 | 4489 | </div>
|
4426 | 4490 |
|
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 | | - |
4486 | 4491 | ## Q. What additional methods for working with associative arrays (maps) appeared in Java 8?
|
4487 | 4492 |
|
4488 | 4493 | * `putIfAbsent()` adds a key-value pair only if the key was missing:
|
|
0 commit comments