package org.code2care.generics;
public class Example<T extends Number> {
public static void main(String[] args) {
Example<String> example = new Example<String>();
}
}
Compilation Error:
Example.java:7: error: type argument String is not within bounds of type-variable T
Example<String> example = new Example<String>();
^
where T is a type-variable:
T extends Number declared in class Example
Example.java:7: error: type argument String is not within bounds of type-variable T
Example<String> example = new Example<String>();
^
where T is a type-variable:
T extends Number declared in class Example
2 errors
Here, we are dealing with bounded type generics (T extends Number) in Java.
Why this error?
In our case, we should have type T to be a subtype of Number, or its upper bound can be the Number itself. As String is not a type of Number hence we get this compilation error.
Fix
You have to make sure that the type that you use with extends should be a subtype of the upper-bound specified or the upper-bound class type itself.
So in our case, we have some of the following possibilities.
Client<Byte> byteType = new Client<Byte>();
Client<Short> shortType = new Client<Short>();
Client<Integer> intType = new Client<Integer>();
Client<Long> longType = new Client<Long>();
Client<Double> doubleType = new Client<Double>();
Client<Number> numberType = new Client<Number>();
Facing issues? Have Questions? Post them here! I am happy to answer!
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- appcompat_v7 errors after updates to API level 21 Material Theme - Android
- Convert java.time.LocalTime to java.util.Date Object - Java
- Python: Print Dictionary Line by Line Example - Python
- StringTokenizer in Java with Examples - Java
- Programmatically Send an Email from Android App using Intent - Android
- 31: Python Program to reverse a String - Python
- Spring Boot: NamedParameterJdbcTemplate batch insert example - Java
- Android Studio : Change FAB icon color : FloatingActionButton - Android-Studio