Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

generic classes in java

// A Simple Java program to show multiple
// type parameters in Java Generics
  
// We use < > to specify Parameter type
class Test<T, U>
{
    T obj1;  // An object of type T
    U obj2;  // An object of type U
  
    // constructor
    Test(T obj1, U obj2)
    {
        this.obj1 = obj1;
        this.obj2 = obj2;
    }
  
    // To print objects of T and U
    public void print()
    {
        System.out.println(obj1);
        System.out.println(obj2);
    }
}
  
// Driver class to test above
class Main
{
    public static void main (String[] args)
    {
        Test <String, Integer> obj =
            new Test<String, Integer>("GfG", 15);
  
        obj.print();
    }
}
Comment

java define a generic class that produces


List<String> list1 = new ArrayList<String>(); // java 7 ? List<String> list1 = new ArrayList<>(); 
list1.add("abc");
//list1.add(new Integer(5)); //compiler error

for(String str : list1){
     //no type casting needed, avoids ClassCastException
}
Comment

java define a generic class that produces


List list = new ArrayList();
list.add("abc");
list.add(new Integer(5)); //OK

for(Object obj : list){
	//type casting leading to ClassCastException at runtime
    String str=(String) obj; 
}
Comment

PREVIOUS NEXT
Code Example
Java :: richest customer wealth 
Java :: java stream and filter 
Java :: java lb to kg 
Java :: java to the power of 
Java :: There is no client authentication. Try adding an appropriate authentication filter 
Java :: his version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 4.2 or newer. 
Java :: combine two array in java 
Java :: loop through number java 
Java :: JsonArray get first Object 
Java :: set text from strings.xml 
Java :: java Overridding example 
Java :: java command to start jenkins 
Java :: button getsource 
Java :: hashmap sort ascending 
Java :: zweidimensionales array ausgeben java 
Java :: fibonacci series in java 
Java :: arrays.sort with comparator 
Java :: java array sorting java8 
Java :: how to replace an element in array in java 
Java :: latest android sdk version 
Java :: isEmpty java code 
Java :: How to sort 2d array in java using stream api 
Java :: remove all element in a list java 
Java :: java create arraylist with size 
Java :: remove last node from linked list java 
Java :: Number to decimal places in java 
Java :: add java 8 support to pom 
Java :: lcm of two large positive integers java 
Java :: erstelle hashmap java 
Java :: linked list vs vector 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =