Difference between class and type

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

Leave a Comment