What are E, K, N, S, T, U, V in Java Generics


If you are wondering what <E>, <K, V>, <N> and so on are called in Java Generics? Well, they are known as "Type Parameters".

Java Genetics E - K - V - T - U - V Type Parameters

Type Parameters are placeholders for data types that are defined when creating generic classes, interfaces, or methods. They are specified inside angle brackets (<>) when defining a generic class, interface, or method.

Examples:

    Genetic Interface with Type Parameter E:

    interface Payment<E> {
        void process(E payment);
    }

    Genetic Method with Type Parameter E:

    public <E> void printElement(E element) {
        System.out.println(element);
    }

    Genetic Class with Type Parameter E:

    class Container<E> {
        private E item;
    
        public Container(E item) {
            this.item = item;
        }
    
        public E getItem() {
            return item;
        }
    }

Naming Conventions of Type Parameters:

    The Type Parameters are represented by single uppercase letters e.g., E, T, K, V.

    Naming ConventionType ParameterUsageExample
    EElementUsed extensively by the Java Collections FrameworkList<E>
    KKeyTypically used in key-value pairs or mapsMap<K, V>
    NNumberOften used for numeric typesList<Number>
    TTypeA general-purpose type parameterBox<T>
    VValueCommonly used in key-value pairs or mapsMap<String, V>
    S, U, V, etc.2nd, 3rd, 4th typesUsed for additional type parametersPair<S, T>, Triple<U, V, W>


This is not an AI-generated article but is demonstrated by a human with Java JDK 21 on a M1 Mac.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap