Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Wednesday, October 25, 2017

Best Ways to Java Generic Naming Convention, Rules, Examples

In this post, We will learn what are the naming conventions rules for Generic type parameters. Many people might have seen that Generic types are heavily used in Java Collection api such as ArrayList, HashSet, HashMap.

Best Ways to Java Generic Naming Convention, Rules, Examples

Generics types are should be enclosed in Diamond operator.

Learn Generics in Java.

Naming Conventions Rules:


1) Generic type should be in UPPER case. This makes us easily identification to it.
2) The following are the predefined as described in java official documentation.
    https://docs.oracle.com/javase/tutorial/java/generics/types.html
   E - "E" is the Parameter type name which describes that Element (Extensively used in Java Collection api)
3) K - Represents Key and used in HashMap as key.
4) V - Represents Value and used in HashMap as value.
5) N - Represents number representation.
6) Generics supports for single and multi parameter generic declaration.
   T - Represents type, Used as first generic parameter to the class. This can be user defined or inbuilt api classes.
7) S - To represent the second generic type parameter.
8) U - To represent the third generic type parameter.
9) V - To represent the fourth generic type parameter.

Example for "T" type generic class:


package com.adeepdrive.generics;

public class TypeTExample 
{
    public static void main(String[] args) 
    {
        // House takes String as generic type. Which is valid
        House<String> name = new House<String>();
        name.setT("ADeepDrive");
        System.out.println("House Name :: " + name.getT());

        // House takes Integer as generic type. Which is valid
        House<Integer> number = new House<Integer>();
        number.setT(new Integer("333"));
        System.out.println("House Number :: " + number.getT());
     
        // House takes Long as generic type. Which is valid
        House<Long> housePhoneNumber = new House<Long>();
        housePhoneNumber.setT(new Long("110814673"));
        System.out.println("House Phone Number :: " + housePhoneNumber.getT());
    }
}

// Created a class which takes single generic type. Here, we can pass any class
// while creating House instance.
class House<T> {
    T t;
    public T getT() {
        return t;
    }
     public void setT(T t) {
       this.t = t;
    }
}

Output:

House Name :: ADeepDrive
House Number :: 333
House Phone Number :: 110814673

In this example, we have created a generic class House and used "T" as type name. It accepts any class passing to it. We passed here String, Integer, Long.
While creating instance for generic class, we can not pass primitive types. For primitive types, we should use valid only Wrapper class.

name, number, housePhoneNumber object creations are absolutely fine and no compile errors. Runs fine.


Example for "K", "V" types:


package com.adeepdrive.generics;

public class TypeKVExample {
    public static void main(String[] args) {

        KeyValuePair<Integer, String> pair1 = new KeyValuePair(new Integer(1), "One");
        System.out.println("Pair 1: key = " + pair1.getKey() + ", value = " + pair1.getValue());

        KeyValuePair<String, Float> pair2 = new KeyValuePair<String, Float>("Two", new Float("14.5"));
        System.out.println("Pair 2: key = " + pair2.getKey() + ", value = " + pair2.getValue());
    }
}

// Generic class that takes K, V types.
class KeyValuePair<K, V> {

    K key;
    V value;

    public KeyValuePair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public K getKey() {
        return key;
    }

    public V getValue() {
        return value;
    }
}

Output:

Pair 1: key = 1, value = One
Pair 2: key = Two, value = 14.5

In TypeKVExample class, we have created a class KeyValuePair with two generic types K, V. First is for key and second is for value. In main method, created two instances for KeyValuePair as pair1, pair2. These two are valid. No exception while executing.

Invalid case:

If we do the following then it compiles fine but during runtime, will throw  ClassCastException. Because constructor takes any class for key and value.


KeyValuePair<String, Float> pair3 = new KeyValuePair(new Integer(1), "One"); // valid
     System.out.println("Pair 3: key = " + pair3.getKey() ); // RuntimeException

        
Output:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String       

This is not caught in the compile time. If, we want it to be captured at compile then we should do the following.


KeyValuePair<String, Float> pair3 = new KeyValuePair<String, Float>(new Integer(1), "One");
 

Mentioned types to the constructor which checks during compile time rather than run-time.

No comments:

Post a Comment

Please do not add any spam links in the comments section.