Generics that restricts the types to Int, Double, long

This is not possible currently.

Also, see this question for a possible workaround


No, unfortunately there is no such restriction possible. The closest you can get is using struct, IConvertible as generic type constraint.

You may want to read this question though.


Comments

  1. Mekhi

    • 2016/8/1

    Is it possible to constraint a method so that it receives only int , double , long or other numerical types that has the usual numerical 

  2. Torres

    • 2019/7/22

    No, unfortunately there is no such restriction possible. The closest you can get is using struct, IConvertible as generic type constraint. You may want to read this question though.

  3. Mohamed

    • 2018/11/7

    Cannot Instantiate Generic Types with Primitive Types; Cannot Create Instances of Type 

  4. Nash

    • 2017/7/11

    Although you can restrict a generic type to only. Integer Long Float. and other child classes of Number by saying <T extends Number>. PS: For your requirement <T extends Serializable> will work, but this will also accept all the other classes which implement Serializable (Thanks to @Jeroen Vannevel's comment) Share.

  5. Makai

    • 2017/3/7

    Bounded Type Parameters. There may be times when you want to restrict the types that can be used as type arguments in a parameterized type. For example, 

  6. Leonardo

    • 2020/5/18

    Generic Method for Numeric Types. In VB.NET if I want to have an extension method for numerical variables of different types ( Integer, Long, Decimal, Double) I always have to define multiple methods for these: <Extension ()> Public Function Add (a As Integer, b As Integer) As Integer Return a + b End Function <Extension ()> Public Function Add

  7. Jair

    • 2019/12/15

    How will the complier catch a error when user input string type but reasonable with int, float, double, long type? Can you help me this problem?

  8. Demetrius

    • 2019/10/26

    They are constraints for the generic parameters of a generic type, not for generic types. You simply won't be able to compile code if you try to instantiate the generic type with some complete type used as an actual generic parameter to substitute the constrained generic parameter with violation of a constraint. It happens during compile time. In a way, it actually extends the functionality of the generic type, as you can use the members of the generic parameters which otherwise would be

  9. Jamir

    • 2019/8/22

    A Java variable can be one of the eight primitive data types. double, Double The generic type parameter (T) will eventually be an Integer, 

  10. Gentile

    • 2021/1/12

    An introduction to generic types in Java: covariance and contravariance Types. Java is a statically typed language, which means you must first declare a variable and its type before using it. For example: int myInteger = 42; Enter generic types. Generic types. Definition: “A generic type is a generic class or interface that is parameterized

  11. Ace

    • 2018/3/26

    There may be times when you want to restrict the types that can be used might only want to accept instances of Number or its subclasses.

  12. Murati

    • 2019/1/18

    Type should be generic. It must extend from the Numbers class, and restrict the type (int, float, double, and long). Create 4 methods. The first three methods are common for both classes: 1. void addElement(Type) - Must reflect the add() function of the Array List. 2. void removeElement(Type) - Must reflect the remove() function of the Array List. 3.

  13. Conrad

    • 2016/4/7

    Whenever you want to restrict the type parameter to subtypes of a new Sample<Integer>(20); obj1.display(); Sample<Double> obj2 = new 

  14. Rhett

    • 2019/9/30

    Though I know that int, long ,double……these basic elements come from ValueType 5.1) Check if the generic struct type has implemented the 

  15. Elliot

    • 2021/1/15

    This enforcement removes the need for the (Double) cast. Discovering generic types. A generic type is a class or interface that introduces a set 

  16. Julio

    • 2020/12/25

    In the notation <T extends Number>, the Number class. specifies an upper bound for the parameter restricts the types that can be used as type arguments.

Comments are closed.

Recent Posts