Difference between class and type

"Type" defines 'what type of data it is'

Ex: "hello world" is a String --> "hello world" is String type (String is not a premetive data unlike int .. so we can say "hello world" is a string class type)

10 is a int --> 10 is a integer data type.


"Type" is the more inclusive category. Variables in Java can have three kinds of types: the 8 "primitive" types like int and float, interfaces, and classes. Values (as opposed to variables) can be primitive or class instances.


A class is a type. An interface is a type. A primitive is a type. An array is a type.

Therefore, every type is also either a class (including an enum constant), an interface, a primitive, or an array.

There are two distinct categories of types: primitive types and reference types:

  • A variable of primitive type always holds a primitive value of that same type. Such a value can only be changed by assignment operations on that variable.
  • A variable of reference type always holds the value of a reference to an object. All objects, including arrays, support the methods of class Object. The reference types are class types (including enum types), interface types, and array types.

Every piece of data has a type which defines its structure, namely how much memory it takes up, how it is laid out, and more importantly, how you can interact with it.

Examples of primitive types:

  1. int
  2. float
  3. char
  4. boolean

Examples of class types:

  1. String
  2. Integer
  3. Boolean
  4. ArrayList
  5. StringBuilder

Examples of interface types:

  1. Collection
  2. List
  3. Map
  4. Serializable

Examples of array types:

  1. int[]
  2. String[]
  3. Integer[][][]

Basically, anything that you can refer to as a variable has a type, and classes are a kind of a type.

More info here: http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html


TLDR - Class is one of the Type in Java.

Note - To fully understand the answer, you must have a little idea about generics in Java.


To understand the difference let us first understand what a Type is in Java.

According to JLS SE 10 ,

There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3).

What is primitive Type ?

a) The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing UTF-16 code units (§3.1).

b) The floating-point types are float, whose values include the 32-bit IEEE 754 floating-point numbers, and double, whose values include the 64-bit IEEE 754 floating-point numbers.

c) The boolean type has exactly two values: true and false.

Now , let us come to what is reference type ?

There are four kinds of reference types: class types (§8.1), interface types (§9.1), type variables (§4.4), and array types (§10.1).

Let us discuss one by one.

If you see how in JLS , Class is defined like this :

A class declaration specifies a new named reference type.

There are two kinds of class declarations: normal class declarations and enum declarations.

ClassDeclaration:
NormalClassDeclaration 
EnumDeclaration
NormalClassDeclaration:
{ClassModifier} class TypeIdentifier [TypeParameters] [Superclass] [Superinterfaces] ClassBody

You see that [TypeParameters], this shows that class type includes those generic classes too.

class Example<T>{

}

The class type will be called Example

In short , a class type covers our enums , our regular (non generic) classes like String etc and our generic classes too.

Similarly , I hope interface and array types is also clear. By array Type we mean like int[], String[] etc.

Let us come to the last part - Type variables. What are they ?

A type variable is an unqualified identifier used as a type in class, interface, method, and constructor bodies.

Let us understand by the example in the JLS below it.

class Test {
    <T extends C & I> void test(T t) {  
        t.mI();           // OK
        t.mCPublic();     // OK 
        t.mCProtected();  // OK 
        t.mCPackage();    // OK

    } 
}

You see that your object in the method parameter is of type T. Yes , this T is Type variable and is/can be used as a reference . Yes it is. (Could not understand this strange example - Learn what is generic method in Java)

This completes the answer.

Tags:

Java

Types

Class