Generic T of java

Generic T of java

Java Generics is a language feature of Java that allows for the definition and use of generic types and methods.Generic types or methods differ from regular types and methods in that they have type parameters. A class like LinkedList is a generic type. It has a type parameter E that represents the type of the elements stored in the list.

Java common type (generics) is a feature of Java language, which is used to define and use common types and methods. Common types and methods differ from other normal types and methods in that they have type parameters. linkedList is a general type. The middle E is its type parameter, which represents the class of the elements in the linkedList.

Generic types are instantiated to form parameterized types by providing actual type arguments that replace the formal type parameters. Instantiations, such as LinkedList or a LinkedList , are called parameterized types, and String and Integer are the respective actual type arguments.

After a generic type is instantiated, it is called a parameterized type. At this time, its type parameters will be replaced by exact type arguments. (I've found that parameter refers to the parameter in the function definition, also known as the line parameter, while argument refers to the actual parameter when the function is called, also known as the actual parameter). After instantiation, LinkedList or LinkedList is a parameterized type, and String and Integer in are type arguments.

Java generics were invented primarily for implementation of generic collections.

It was created for this collection type. I didn't take a close look at this section. Ha ha

benefit of using Java generics - Early error detection at compile time.

The advantage of this java generics is that you can know the type problem during compilation. After the LinkedList is declared, if the list is added with an integer, the compilation will not pass. But for general types, even if different types are added in the middle, no error is reported

LinkedList list = new LinkedList(); 
list.add("abc");       // fine 
list.add(new Date());  // fine as well
LinkedList<String> list = new LinkedList<String>(); 
list.add("abc");       // fine 
list.add(new Date());  // error

In addition, the parameterized type does not need another cast when obtaining internal elements? I don't think it's right to use "conversion" here, because it's this type, just to explain...) One time type:

LinkedList<String> list = new LinkedList<String>(); 
list.add("abc");  
String s = list.get(0);  // no cast needed
LinkedList list = new LinkedList(); 
list.add("abc");  
String s = (String)list.get(0);  // cast required

In Java, a program is considered type-safe if it compiles without errors and warnings and does not raise any unexpected ClassCastException s at runtime.
The idea is that a well-formed program enables the compiler to perform enough type checks based on static type information that no unexpected type error can occur at runtime.

Moreover, when compiling, there is no error reporting, no warning, and no ClassCastException thrown when running. That is, the type safety type is safe. A robust program should have enough at compile time? Don't understand) type checking to prevent type errors when running.

What is a parameterized or generic type?

interface Collection<E>  {  
  public void add (E x);  
  public Iterator<E> iterator(); 
} // Example (of a generic type): 
Collection<String> coll = new LinkedList<String>();//Example (of a parameterized type): 

In addition, sometimes you can see this way in the code:

public static <T> Collection<T> select(Class<T> c, String sqlStatement) {
    Collection<T> result = new ArrayList<T>();
    /* run sql query using jdbc */
    for ( /* iterate over jdbc results */ ) {
        T item = c.newInstance();
        /* use reflection and set all of item's fields from sql results */
        result.add(item);
    }
    return result;
}

There are two < T >. If there is a general method in a class and the returned one is a general type, then the type parameter of this general type should be marked out when the class name is used. If there is no such method, the method return should be preceded by one more to indicate that the return type is a general type.

Another example

class Pair<X,Y>  {  
  private X first; 
  private Y second;
  public Pair(X a1, Y a2) { 
    first  = a1; 
    second = a2; 
  } 
  public X getFirst()  { return first; } 
  public Y getSecond() { return second; } 
  public void setFirst(X arg)  { first = arg; } 
  public void setSecond(Y arg) { second = arg; } 
}

concrete parameterized type?
An instantiation of a generic type where all type arguments are concrete types rather than wildcards.
Examples of concrete parameterized types are List , Map<String,Date> , but not List<? extends Number> or Map< String,?> .
Generally speaking, the parameterized type list < string > is the exact parameterized type. Not list <? Extends number >

Is List< Object> a supertype of List< String>?
No, different instantiations of the same generic type for different concrete type arguments have no type relationship.(Parameterized types are not covariant.)
No, it isn't. It doesn't matter. Arguments of parameterized types are not downward compatible.

void printAll(ArrayList<Object> c) { 
  for (Object o : c)  
    System.out.println(o); 
}
ArrayList<String> list = new ArrayList<String>(); 
... fill list ... 
printAll(list);   // error

Correct writing:

import java.util.ArrayList;
class Hello {
	public static void main(String[] args) {
		System.out.println("Hello");
		ArrayList<String> arr = new ArrayList<String>();
		arr.add("first");
		arr.add("secode");
		arr.add("third");
		Hello self = new Hello();
		self.printAll(arr);
	}
	
	public <T> void printAll(ArrayList<T> c) { 
		  for (Object o : c)  
		    System.out.println(o); 
		}
	
}
Published 1 original article, praised 0 and visited 5
Private letter follow

Tags: Java SQL JDBC

Posted on Mon, 13 Jan 2020 02:37:59 -0800 by coffeeguy