Code Monkey home page Code Monkey logo

java-generics's Introduction

Generics in Java

Generics are a way to create type-safe classes and methods that can operate on different types of objects to catch the type errors at compile time rather than runtime.

It is introduced in version 5.2

Generic class:

public class GenericClass<T> {
    private final T item;
    
    public GenericClass(T item) {
        this.item = item;
    }
    
    public T getItem() {
        return item;
    }
}

Generic method:

    private static <T> void printItem(T item) {
        System.out.println(item.toString());
    }

Bounded Generic type:

  • It is used to restrict or bind to certain types or their subtypes.
  • It will allow us to invoke methods of bounded classes or interfaces.
    private static <T extends Number> double add(T numOne, T numTwo) {
        return numOne.doubleValue() + numTwo.doubleValue();
    }

Type inference:

  • It is a feature that allows the compiler to automatically determine the type of generic method or constructor parameter.
    List<Integer> list = new ArrayList<>();

Wild cards:

  • It is a <?> symbol is used to represent an unknown type argument.
    private static void printItemsInList(List<?> list) {
        for (Object item : list) {
            System.out.println(item);
        }
    }

Upper bounded wildcard:

  • It is used to specify that the type argument must be a subtype of a certain class or interface.
  • It is used to access the items.
    private static void printItemsInList(List<? extends Number> list) {
        for (Number number : list) {
            System.out.println(number);
        }
    }

Lower bounded wildcard:

  • It is used to specify that the type argument must be a supertype of a certain class or interface.
  • It is used to modify the items.
    List<? super Number> numbers = new ArrayList<>();
    numbers.add(10);
    numbers.add(10.2);
    numbers.add(10.4f);
    System.out.println(numbers);

Advantages:

  • Type safety
    • data types are corrected at compile time rather than runtime which makes code more robust and reliable.
  • Code re-usability
    • write classes and methods that can work with multiple types of data which can help reduce the amount of code you need to write.
  • Performance
    • improve performance by reducing the need for type casts and avoiding the overhead of runtime type checking.

Disadvantages:

  • Type erasure
    • it is a key feature where it erases the generic code at runtime which can limit the usefulness of generics in certain situations.
  • Limited support for primitive types
    • Generics in Java do not support primitive types, such as int or double, directly. Instead, you must use their corresponding wrapper classes, such as Integer or Double. This can be less efficient than working with primitive types directly.
  • Complexity
    • can add complexity to code which uses complex type hierarchies and wildcard types.

java-generics's People

Contributors

shaikrasheed99 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.