Java - (Data Type|Type)


data type in the java world.

The Java programming language is statically-typed, which means that all variables must first be declared before they can be used.


The language supports four kinds of types:

The first three are known as reference types. Class instances and arrays are objects; primitive values are not.



A supertype is a class that is extended by an other class. See Java - (Inheritance|Class Hierarchy) - (Subclass|Superclass) - (Extends, Super) - ( is a relationship). This is hierarchic relationship.

In object-oriented terminology, this is called an “is a” relationship.

Example: As:

The following code is allowed:

Object someObject = new Object();
Integer someInteger = new Integer(10);
someObject = someInteger;   // OK

public void someMethod(Number n) { /* ... */ }
someMethod(new Integer(10));   // OK
someMethod(new Double(10.1));   // OK


Same as supertype but in the other direction.

A Subtype is a class that extends an other class.


Interface are abstract data type. When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.

As an example, here is a method for finding the largest object in a pair of objects, for any objects that are instantiated from a class that implements Relatable:

public Object findLargest(Object object1, Object object2) {
   Relatable obj1 = (Relatable)object1;
   Relatable obj2 = (Relatable)object2;
   if ( (obj1).isLargerThan(obj2) > 0)
      return object1;
      return object2;

By casting object1 to a Relatable type, the can invoke the isLargerThan method that is define in the Relatable interface.


Documentation / Reference

Powered by ComboStrap