What Does Class<> Mean in Java

What does Class? mean in Java?

Class is a parameterizable class, hence you can use the syntax Class<T> where T is a type. By writing Class<?>, you're declaring a Class object which can be of any type (? is a wildcard). The Class type is a type that contains meta-information about a class.

It's always good practice to refer to a generic type by specifying his specific type, by using Class<?> you're respecting this practice (you're aware of Class to be parameterizable) but you're not restricting your parameter to have a specific type.

Reference about Generics and Wildcards: http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html

Reference about Class object and reflection (the feature of Java language used to introspect itself): https://www.oracle.com/technetwork/articles/java/javareflection-1536171.html

What does .class mean in Java?

When you write .class after a class name, it references the class literal -
java.lang.Class object that represents information about a given class.

For example, if your class is Print, then Print.class is an object that represents the class Print on runtime. It is the same object that is returned by the getClass() method of any (direct) instance of Print.

Print myPrint = new Print();
System.out.println(Print.class.getName());
System.out.println(myPrint.getClass().getName());

What does Classsomething mean in Java?

It defines the types of generics.


For example:

List<Integer> list;

You declare a variable of class List. But List is declared like this, if I'm right:

public interface List<T> extends Collection<T>

The T is a placeholder for a type that the user of this class can define. In my example, I chose to fill in the T with Integer. In this case it means I'll have a List of Integers.

How to use ClassT in Java?

Using the generified version of class Class allows you, among other things, to write things like

Class<? extends Collection> someCollectionClass = someMethod();

and then you can be sure that the Class object you receive extends Collection, and an instance of this class will be (at least) a Collection.

Class[] - What does it mean?

Yes the literal meaning is exactly what you are thinking it is

Class[] types = new Class[] { String.class, String.class }; is a declaration and initialization in one line. It says create an array that holds objects of type Class and initialize it with two objects of type Class, namely String.class and String.class.

A similar example would be

int[] nums = new int[]{1,2,3};

or

float[] decimals = new float[]{1.2, 3.1, 5.2}

What does the generic nature of the class ClassT mean? What is T?

Type parameter <T> has been added to java.lang.Class to enable one specific idiom1 - use of Class objects as type-safe object factories. Essentially, the addition of <T> lets you instantiate classes in a type-safe manner, like this:

T instance = myClass.newInstance();

Type parameter <T> represents the class itself, enabling you to avoid unpleasant effects of type erasure by storing Class<T> in a generic class or passing it in as a parameter to a generic method. Note that T by itself would not be sufficient to complete this task2: the type of T is erased, so it becomes java.lang.Object under the hood.

Here is a classic example where <T> parameter of the class becomes important. In the example below, Java compiler is able to ensure type safety, letting you produce a typed collection from a SQL string and an instance of Class<T>. Note that the class is used as a factory, and that its type safety can be verified at compile time:

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;
}

Since Java erases the type parameter, making it a java.lang.Object or a class specified as the generic's upper bound, it is important to have access to the Class<T> object inside the select method. Since newInstance returns an object of type <T>, the compiler can perform type checking, eliminating a cast.



1 SUN Oracle has published a good article explaining all this.

2 This is different from implementations of generics without type erasure, such as one in .NET.

3 Java Generics tutorial by Oracle.

What does the E syntax mean in Java?

These are called Generics.

In general, these enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods.

Using generics give many benefits over using non-generic code, as shown the following from Java's tutorial:

  • Stronger type checks at compile time. A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find.

    For example:

    // without Generics
    List list = new ArrayList();
    list.add("hello");

    // With Generics
    List<Integer> list = new ArrayList<Integer>();
    list.add("hello"); // will not compile
  • Enabling programmers to implement generic algorithms. By using generics, programmers can implement generic algorithms that work on collections of different types, can be customized, and are type safe and easier to read.

  • Elimination of casts.

    For example, the following code snippet without generics requires casting:

    List list = new ArrayList();
    list.add("hello");
    String s = (String) list.get(0);

    When re-written to use generics, the code does not require casting:

    List<String> list = new ArrayList<String>();
    list.add("hello");
    String s = list.get(0); // no cast


Related Topics



Leave a reply



Submit