Difference between class and type
JavaClassTypesJava Problem Overview
Being new to Java, I'm confused between the concepts of class and type.
For example, should the object "Hello World!"
belong to the type String
or class String
? Or maybe both?
Java Solutions
Solution 1 - Java
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:
int
float
char
boolean
Examples of class types:
Examples of interface types:
Examples of array types:
int[]
String[]
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
Solution 2 - Java
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.
Solution 3 - Java
"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.
Solution 4 - Java
"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.
Solution 5 - Java
This is a question about programming terminology, and as expected, there is yet another answer that could be considered valid.
The Java type system, as exposed by the reflection API, has two types: Class<T>
class and Type
interface, which the former implements. In this system, a class (an element that is described by an instance of Class<T>
) is any type created with the keyword class
or interface
, any primitive type, any array thereof, and void
. Everything else is just a Type
.
So what is everything else? While a class in this context is everything the runtime sees and has access to when considering the capabilities of an object, a type, in general, is everything the compiler sees. This can be a parametrized type (like ArrayList<String>
– remember that type erasure causes every ArrayList<T>
to map to the same class at runtime), a generic type parameter created as a part of a method or class declaration, a wildcard type, and any array thereof. Those types are exposed by API methods in all places where type erasure is not in effect,
like when traversing parameter types or base types.
Solution 6 - Java
Types are associated with compile-time entities such as variables and expressions, whilst classes are associated with run-time entities such as in-memory objects. Consider the language used in the specification 15.5. Expressions and Run-Time Checks
> If the type of an expression is a reference type, then the class of
> the referenced object... An expression whose type is a reference type may be tested using instanceof
to find out whether the class of the object referenced by the run-time value of the expression...
Robert Harper, author of Practical Foundations for Programming Languages, makes the following distinction
> I do think it is important to distinguish carefully classes, which may > be attached to objects, from types, which are static classifiers
Java generics are one example where this distinction is relevant. First consider the following snippet
String greeting = "Hello" + " " + "world!";
- the compile-time type of the variable
greeting
isString
- the compile-time type of the expression
"Hello" + " " + "world!"
isString
- the run-time class of the instantiated object
Hello world!
referred to by variablegreetings
isString
It appears in the above case the distinction does not matter much, however let's try similar reasoning on the following snippet
List<String> greetings = List.of("Hello World!");
- the compile-time type of the variable
greetings
isList<String>
- the compile-time type of the expression
new ArrayList("Hello World!")
isList<String>
- the run-time class of the instantiated object referred to by variable
greetings
isImmutableCollections$List1
Now we see there is indeed a difference between type List<String>
and class ImmutableCollections$List1
. We see the compile-time type is "richer" in information where compiler is aware the element type is String
whilst at run-time the element type information is lost due to type erasure.
Here is another demonstration of the difference between types and classes
jshell> /set feedback verbose
| Feedback mode: verbose
jshell> List.of("Hi", "Hello")
$1 ==> [Hi, Hello]
| created scratch variable $1 : List<String>
jshell> List.of(42, -11)
$2 ==> [42, -11]
| created scratch variable $2 : List<Integer>
jshell> $1.getClass() == $2.getClass()
$3 ==> true
| created scratch variable $3 : boolean
Note how types of expressions List.of("Hi", "Hello")
and List.of(42, -11)
are different, and yet the classes of corresponding instantiated objects are the same.