A data type determines the meaning of data and allowable operations. Java data types include primitive types and reference types.
- [Instructor] The purpose of a computer program is to process data, that is, to manipulate and create data items. In Java, all data items have types. Each type defines a set of values for a certain purpose and a set of operations that we can perform on such values. This allows the Java compiler to scan the source code to check data compatibility for all data operations. This safeguards our program to keep many types of data errors from happening during runtime, and it is a great advantage over weakly typed languages.
What is a data type? Everything in a computer is stored in binary, zeroes and ones. A type tells Java how to interpret each chunk of data. So a type tells us exactly how much data we can store in the type and the meaning of the data. In Java, we have two data type categories, primitives and object reference types. First, let's look at the simplest data types, primitives. Java has eight primitive data types.
Each type is represented internally by a fixed number of bits. Therefore, each primitive type has a limited range of values it can represent. In other words, the amount of information we can represent in each piece of primitive data is always limited. Note that the Boolean type has undefined size. It depends on the Java virtual machine implementation. Another way to define primitive types is by contrasting them with non-primitive types, Java objects. While primitive types have fixed sizes, an object can grow and shrink in size.
With the primitive types, we can perform operations on them directly via primitive variables. A primitive variable is simply the name for a stored primitive data item. An object, on the other hand, cannot be operated on directly. In Java, we can define object reference variables to keep track of objects. Each reference variable stores the location or the pointer to an object in memory, not the object itself. Object references can be copied from one variable to another, but it simply copies addresses, not actual objects.
Because of this, we cannot modify objects directly but only indirectly via object references. We'll discuss reference types in greater detail later.
- Data types
- Allowable operations
- Literals and primitives
- Prefixes and suffixes
- Reference types
- Identifier rules