🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Wildcards in Java Generics provide a way to handle unknown types. They are represented by the question mark (?) and can be used in various contexts, such as method parameters, return types, and field types. Wildcards enable greater flexibility in the code, allowing it to work with different types without knowing them explicitly.
Table of Contents
- What are Wildcards?
- Types of Wildcards
- Unbounded Wildcards
- Upper Bounded Wildcards
- Lower Bounded Wildcards
- Example: Using Wildcards
- Guidelines for Using Wildcards
- Conclusion
1. What are Wildcards?
Wildcards represent unknown types in generics. They allow you to specify that a type parameter can be substituted with any type. Wildcards are especially useful when you want to write code that can handle multiple types without needing to specify the exact type.
2. Types of Wildcards
Unbounded Wildcards
Unbounded wildcards can represent any type. They are declared using a single question mark (?).
Syntax:
List<?> list = new ArrayList<>();
Upper Bounded Wildcards
Upper bounded wildcards restrict the unknown type to be a specific type or a subtype of that type. They are declared using the extends keyword.
Syntax:
List<? extends Number> list = new ArrayList<>();
Lower Bounded Wildcards
Lower bounded wildcards restrict the unknown type to be a specific type or a supertype of that type. They are declared using the super keyword.
Syntax:
List<? super Integer> list = new ArrayList<>();
3. Example: Using Wildcards
Example 1: Unbounded Wildcards
Example:
import java.util.List;
import java.util.ArrayList;
public class UnboundedWildcardExample {
public static void printList(List<?> list) {
for (Object elem : list) {
System.out.println(elem);
}
}
public static void main(String[] args) {
List<String> stringList = new ArrayList<>();
stringList.add("Hello");
stringList.add("World");
List<Integer> intList = new ArrayList<>();
intList.add(1);
intList.add(2);
printList(stringList);
printList(intList);
}
}
Output:
Hello
World
1
2
Example 2: Upper Bounded Wildcards
Example:
import java.util.List;
import java.util.ArrayList;
public class UpperBoundedWildcardExample {
public static double sumOfList(List<? extends Number> list) {
double sum = 0.0;
for (Number number : list) {
sum += number.doubleValue();
}
return sum;
}
public static void main(String[] args) {
List<Integer> intList = new ArrayList<>();
intList.add(1);
intList.add(2);
intList.add(3);
List<Double> doubleList = new ArrayList<>();
doubleList.add(1.1);
doubleList.add(2.2);
doubleList.add(3.3);
System.out.println("Sum of intList: " + sumOfList(intList));
System.out.println("Sum of doubleList: " + sumOfList(doubleList));
}
}
Output:
Sum of intList: 6.0
Sum of doubleList: 6.6
Example 3: Lower Bounded Wildcards
Example:
import java.util.List;
import java.util.ArrayList;
public class LowerBoundedWildcardExample {
public static void addNumbers(List<? super Integer> list) {
for (int i = 1; i <= 5; i++) {
list.add(i);
}
}
public static void main(String[] args) {
List<Number> numberList = new ArrayList<>();
addNumbers(numberList);
System.out.println("Number List: " + numberList);
}
}
Output:
Number List: [1, 2, 3, 4, 5]
4. Guidelines for Using Wildcards
- Use Unbounded Wildcards (
?): When you want to accept any type and do not require any type-specific operations. - Use Upper Bounded Wildcards (
<? extends T>): When you want to read items from a structure and need to perform operations on the elements that are guaranteed to be a subtype of a specific type. - Use Lower Bounded Wildcards (
<? super T>): When you want to add items to a structure and need to ensure that the structure can accept elements of a specific type or its subtypes.
5. Conclusion
Wildcards in Java Generics provide a way to write more flexible and reusable code. They allow you to work with unknown types, making your code more adaptable to various situations. By understanding and using unbounded, upper bounded, and lower bounded wildcards, you can enhance the robustness and versatility of your Java programs.
Happy coding!
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment