Primitive Data Types
A primitive data type specifies the size and type of information. Primitive types are the simplest type of variables in Java. They simply store a small amount of data, according to the type. They are not associated with a class.
The 3 Primitive Data Types for College Board
There are eight primitive data types in Java, but only these 3 are used in AP CSA:
Data Type | Description |
---|---|
int | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
double | Stores decimal numbers. Sufficient for storing 15 decimal digits |
boolean | Stores true or false values |
To declare a variable, you write:
Type VariableName = Value;
For example:
int count = 0;
Variable Terms and Conventions
These are important items to remember with regards to Java and the College Board.
- A
variable name
is often referred to as thevariable identifier
in Java. - A
variable name
follows camel case conventions in Java (e.g., firstName). - A
class name
, which is a Data Type, follows Pascal case (e.g., BankAccount).
The 8 Primitive Data Types
Here is the complete list of primitive types in Java:
- byte: An 8-bit signed two’s complement integer.
- short: A 16-bit signed two’s complement integer.
- int: A 32-bit signed two’s complement integer.
- long: A 64-bit signed two’s complement integer.
- float: A single-precision 32-bit IEEE 754 floating point.
- double: A double-precision 64-bit IEEE 754 floating point.
- boolean: Stores either
true
orfalse
. - char: Stores a single 16-bit Unicode character.
Popcorn Hack: Greatest Value Terms
Primitive Data types have constraints
The program shows the constraints of Integers and Doubles. Define the following terms…
-
Constraints: In the context of primitive data types, constraints refer to the limitations and boundaries within which these types can operate. These constraints include the range of values that a data type can represent, the precision of floating-point numbers, and the amount of memory allocated for storing these values. For example, an
int
in many programming languages might be constrained to represent values from -2,147,483,648 to 2,147,483,647. -
Overflow: Overflow occurs when a calculation produces a result that exceeds the maximum limit of the data type. For example, if you try to store a number larger than the maximum value an
int
can hold (e.g., adding two large integers that exceed this limit), the result may “wrap around” and produce an incorrect value or result in unexpected behavior. In many languages, this can lead to errors or undefined behavior. -
Underflow: Underflow happens when a calculation produces a result smaller than the minimum value that the data type can represent. For floating-point numbers, this might mean that a number is too close to zero to be represented accurately, leading to a loss of precision. For integers, underflow occurs when a number goes below the minimum representable value, causing it to “wrap around” to the maximum representable value of the data type (similar to overflow but in the negative direction).
public class GreatestValue {
public static void main(String[] args) {
System.out.println("Max Integer: " + Integer.MAX_VALUE);
System.out.println("Min Integer: " + Integer.MIN_VALUE);
System.out.println("Max Double: " + Double.MAX_VALUE);
System.out.println("Min Double: " + Double.MIN_VALUE);
// Integer Show Overflow
int i = Integer.MAX_VALUE;
i++;
System.out.println("Integer Max + 1, Overflow: " + i);
// Integer Show Underflow
int j = Integer.MIN_VALUE;
j--;
System.out.println("Integer Min - 1, Underflow: " + j);
// Integer Max + Min
int k = Integer.MAX_VALUE + Integer.MIN_VALUE;
System.out.println("Integer Max + Min: " + k);
}
}
GreatestValue.main(null);
Max Integer: 2147483647
Min Integer: -2147483648
Max Double: 1.7976931348623157E308
Min Double: 4.9E-324
Integer Max + 1, Overflow: -2147483648
Integer Min - 1, Underflow: 2147483647
Integer Max + Min: -1
Popcorn Hack: Fill in Data Type
The code below is broken….
- Fill in the blank, replace the underbars with the correct type.
- Output the contents to the Jupyter Terminal
int zero = 0; // Whole number
double pi = 3.14159; // Decimal values. Floating point numbers.
boolean iAmTakingCSA = true; // Stores a true or false binary value
char myProjectedGrad = 'A'; // Single character
String iLoveCodeCodeCoding = "Yes"; // String of characters
System.out.println(zero);
System.out.println(pi);
System.out.println(iAmTakingCSA);
System.out.println(myProjectedGrad);
System.out.println(iLoveCodeCodeCoding);
0
3.14159
true
A
Yes