Java Reference
In-Depth Information
Cast incompatible types
Understand type conversion in expressions
A
t the foundation of any programming language are its data types and operators, and Java
is no exception. These elements define the limits of a language and determine the kind of
tasks to which it can be applied. Fortunately, Java supports a rich assortment of both data
types and operators, making it suitable for any type of programming.
Data types and operators are a large subject. We will begin here with an examination of
Java's foundational data types and its most commonly used operators. We will also take a
closer look at variables and examine the expression.
Why Data Types Are Important
Data types are especially important in Java because it is a strongly typed language. This
means that all operations are type-checked by the compiler for type compatibility. Illegal
operations will not be compiled. Thus, strong type checking helps prevent errors and en-
hances reliability. To enable strong type checking, all variables, expressions, and values
have a type. There is no concept of a “type-less” variable, for example. Furthermore, the
type of a value determines what operations are allowed on it. An operation allowed on one
type might not be allowed on another.
Java's Primitive Types
Java contains two general categories of built-in data types: object-oriented and non-object-
oriented. Java's object-oriented types are defined by classes, and a discussion of classes is
deferred until later. However, at the core of Java are eight primitive (also called elemental
to indicate that these types are not objects in an object-oriented sense, but rather, normal
binary values. These primitive types are not objects because of efficiency concerns. All of
Java's other data types are constructed from these primitive types.