简体   繁体   中英

Object vs Class<T> (vs Class<?>?) in java?

I'm very new to Java. This is probably a stupid question--but i can't find the answer anywhere. If you wanted to declare a method that would take in an unknown object and do something with it (copy it, for example), what would be the difference between something like:

<T> T func(Class<T> cls){
    //do something
}

Object func(Object o){
    //do something 
}

Are they comparable? Is there anything you could/would do with one of the above methods and not the other? And where does Class<?> fit in?

The difference in your code is that former func receives a Class<T> (which can be Class<?> ) which means the method only receives a Class type . The latter receives any object, regardless if it's a class or another kind of object.

From Class javadoc:

Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions.

Note that Class is metadata for your classes.


If your code were like this:

<T> T func(T o){
    //do something
}

Object func(Object o){
    //do something 
}

The main difference is the return type: the former return type should be as the same type of the argument, while the latter is a generic Object . For example:

Object func(Object o){
    //do something 
    return o.toString(); //compiles and works
}
<T> T func(T o){
    //do something
    return o.toString(); //does not compile
}

Class is a very specialized type of an object. Class<T> is not a replacement for any kind of object, it is rather a class descriptor. In Java, where everything is an object, also classes are objects, so there is this type - Class - which abstracts over the "class" class of objects.

Here's an example:

If you have this:

Class<Object> obj = Object.class;
func(obj);

, this doesn't mean that inside your func method you will have access to an Object instance; you will have access to a Class<Object> instance, which is he descriptor of the Object class.

So, to answer your question, you should use Object for your declared purpose.

Class and Object are 2 different things in Java. If you wanted to take any type of object, which you don't care the type of, the following is more normally seen.

Object func(Object o){
    //do something 
}

It is more common to declare functions with Object vs Class , since there are a few more steps for passing a class than an object.

lets hava a look at your functions

<T> T func(Class<T> cls){
    //do something
}

this one takes class as parameter, and returns instance of the class, imagine method as black box which do some magic stuff you enter String.class and you will get "hello world"

second one

Object func(Object o){
    //do something 
}

takes object as parameter and returns object, so in theory, you can insert class and returns instance, but you can also put date and received String

The first function accepts a java.lang.Class (that is also an instance of an Object class, because Class extends it. You can find more information about the Class class in the javadoc: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html ). Hence the first method can do something with an instance of Class and it does not accept all objects (note the capital letter, it is a name of a class).

The second method accepts all objects (because every object extends java.lang.Object). (Object's javadoc: http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html )

So if you want to create a method that may "take in an unknown object and do something with it" you have to use the second method. You should also know that usually you do not need a method that accepts any object or unknown object and you should not create such methods if you can find another solution (because it causes the code to be harder to read).

A method that accepts Class as an argument is useful when you want to do something with the definition of a class (retrieve a list of fields, methods, constructors etc.). This webpage explains how you can use the Class class: http://docs.oracle.com/javase/tutorial/reflect/index.html

Additionally, if you want to learn more about generics you should read this tutorial: http://docs.oracle.com/javase/tutorial/java/generics/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM