• Steven Tang

Crash Course to APCS: Data Types


Data types are used by the programmer to tell the compiler how they intend to use the data. When declaring variables, data types end up telling the compiler how the data at hand is stored. In Java, data types restrict the operations that can be done on them. Variable declaration and definition will be explained in a later video and blog.


Java is a Strongly and Statically Typed Language

The Java programming language a statically typed language because it checks the types and the type errors during compilation and before the code is run. As a result, when defining variables, the types must be declared explicitly, specifically with the ones predefined by the language.


On the other hand, strongly typed languages are generally strict about how types are distinguished. In other words, once you set a variable to a data type, you cannot change the type. In Java however, there are various exceptions—like casting—that will be explained in a later video, but generally the language is more strongly than weakly typed.


This is opposed to dynamically typed languages and weakly typed language. For a better idea of the difference between dynamically-typed and statically typed languages, as well as the difference between strongly- and weakly-typed languages, please visit this link.


Types of Types

There are two forms of data types: primitive and non-primitive (also called reference) types.


Firstly, primitive types are types predefined by the language. Java specifies the size and type of data that a variable declared with that type can hold. Hence, they come in different sizes.


Primitive types also cannot be broken down into different types. Arrays (explained in a later video and blog), for examples, are containers of certain types, so they could be broken down into primitive types. Even if they contained objects, objects are oftentimes composed of these primitive types as well. Therefore, reference types (obviously) and arrays are not primitive types.


On the other hand, there are reference types, which are not defined by the language, and refer to objects generally created by the programmer. They are all the same size, often containing a reference to an object of that particular type. This term will be explained in a later video as well.


Eight Primitive Types

In Java, there are eight fundamental primitive types: bytes, shorts, integers, longs, floats, doubles, chars, and booleans. They are illustrated and described in the following graphic:

Bytes, shorts, integers, and longs all hold integers of varying sizes; doubles and floats hold floating points numbers of varying sizes; chars hold Unicode characters; and booleans hold logical true or false values.


Unicode characters are the standard for encoding and representing characters in computing systems; they consist of 16-bits in Java. Each character is encoded as a number from 0 to 65535, meaning you can declare a character with the char keyword with a number.


Doubles, ints, and booleans are explained in the next section.


Please visit this link to learn more about these eight primitive types.


Frequently Used Data Types

While all the types previously described are unique and important, three of them (int, double, and boolean) are the most frequently used, and are thus part of the exam description of the AP Computer Science curriculum. As a result, they will be explained in more depth.


Take a look at the following graphic that explains each.

Integers hold values and expressions that have values between thirty two bits, or 4 bytes of data. When declaring a variable as an integer with the int keyword, the compiler allocates 32-bits, or 4-bytes, for that particular variable. The default value of ints is zero.


Doubles hold approximations to real numbers—this is known as floating point. When declaring a variable as a double with the double keyword, the compiler allocates, 64-bits, or 8 bytes, for that particular variable. The default value of 0.0d. How the computer stores decimals inside a double is beyond the scope of this blog. For more information as to how the computer represents these floating-point numbers and integers, please visit this link.


Booleans, lastly, hold values or expressions that are equal to either true or false. It has a default value of false.


Quiz Section

  1. Explain the difference between reference and primitive types? Give examples of each.

  2. Select the correct answer of the two options. In Java, data types and type errors are checked (before/after) compilation.

  3. Classify 3.96 as an int, double, or boolean.

  4. Classify the number of people at your school as an int, double, or boolean.

  5. Classify whether or not it's raining outside as an int, double, or boolean.

  6. Classify human population on Earth as an int, double, or boolean.

  7. Classify 3.14159 as an int, double, or boolean.

  8. Classify Number of Houses on your street (like between 20-30) as an int, double, or boolean.

Answers to the Quiz Sections

Answers will be posted upside down to help you in the learning process.











Note: This blog post is a courtesy of the Los Altos Computer Science Club (LACS).

© 2020 Science Nation