Freewind
Freewind

Reputation: 198398

Is my understanding of Java's Class and Type correct?

In Java, when you define a class, you are defining a type implicitly. Say, I defined a class User:

class User {}

Then we get a class User and an implicit type User.

When we use the name User, sometimes we are referencing class User, sometimes type User. Let me give some examples:

User user = new User();
^^^^(1)         ^^^^(2)

public void saveUser(User user) {
                     ^^^^(3)
}

class Dao<T> {
    def save(T t) {}
}

new Dao<User>();
        ^^^^(4)

Class cls = User.class;
            ^^^^(5)

User user = (User) someObj;
             ^^^^(6)

My understandings:

  1. Position (1) is type User
  2. Position (2) is the constructor of class User
  3. Position (3) is type User
  4. Position (4) is type User
  5. Position (5) is class User
  6. Position (6) is type User

Is my understanding correct?

Upvotes: 3

Views: 107

Answers (4)

Grim
Grim

Reputation: 2040

I would say:

Class cls = User.class;
            ^^^^^^^^^^(5)

User user = new User();
            ^^^^^^^^^^(7)
  1. This is the type of a Declaration of a variable of type User
  2. This is the type of a Definition of a variable of type User (not the constructor, the constructor may exists only i a subclass of User)
  3. This is the type of a Declaration of a parameter/argument of type User
  4. This is the generic type User of a definition of a generic default constructor of type Dao
  5. This is a referention to the class of the type User.
  6. This is a Cast to the type User.
  7. This is a call of the default-constructor of the Type/Class User.

Type or Class? Every Class can be a type, not every type can be a Class. Primitives like int for example, is a type but not a Class. If you are perfect-scientific-correct, you must specify that User is a Class. If you say User is a type, its not wrong but also not specific.

Upvotes: 3

Giovanni Botta
Giovanni Botta

Reputation: 9816

  • in (1), User is the type, user a reference to an object of that type;
  • in (2) the expression new User() creates a new instance of type User; that instance gets assigned to the reference user;
  • in (3) you are passing to function saveUser a reference to an object of type User;
  • in (4) User is a type parameter, that is, it specifies which object type the Dao class is parameterized by;
  • in (5) you are obtaining a reference to an object of type Class<User>, which represent the type User, it is technically not the User type;
  • in (6) you are adding an explicit type casting, telling the compiler that the reference someObj is of type User and assigning the value of that reference to the reference user (which has type User); depending on the type of someObj the cast might or not be required by the compiler and might or might not cause a ClassCastException.

Upvotes: 1

Phate
Phate

Reputation: 6622

A class is an example of abstract data type. So, a class IS a type and you can indifferently name User "class" or "type".

Mind that the opposite is not always true, so a type is not necessarily a class (for example primitive types like int float,ecc. )

Upvotes: 0

Ioan
Ioan

Reputation: 5187

Position (2) represents the object itself instantiated by calling the constructor with no arguments.

All other assumptions are right.

Upvotes: 0

Related Questions