Witscale Test Center

1.3 Variable declarations > 1.3.1 Data types


1.3.1 Data types

Java is a strongly typed language, which means that every variable and expression has a type that is known at compile-time itself. There are two main categories of Java types:

Primitive types

The primitive types comprise of all the numeric types along with a boolean type. The numeric types are four integral types- byte, short, int, long, and char, and two floating-point types- float and double.

 

The primitives are also called as the built-in data types because the compiler directly understands them. This simply means that a variable of primitive type directly holds the primitive value, and it’s placed on the stack. It never stores a reference to an object of any kind.

 

Object-reference types

Whenever you declare a Java class/interface, you are actually declaring a type for similar objects. The object-reference types are nothing but these class types and interface types. In addition, a Java array type is also considered as an object-reference type. The variables of a reference type hold the references to the actual objects.

Types decide which values a variable can hold. For example, a primitive variable of boolean type can hold either of two values, true or false. Types can also limit the operations supported on the value of that type. For example, if you declare a variable of type Vector and store a reference to a Vector object in it, then you can invoke only the operations, which the Vector class implements or inherits on that object.