Meet the Primitive Types

Part of: Primitive Types

What Is a Primitive Type? In Java every value lives in a typed variable. A primitive type stores a single, simple value directly in memory, not a reference to an object. The AP Computer Science A exam uses only three primitive types: - int : whole numbers like 42, -7, 0. Range is about ±2.1 billion. - double : real numbers with a fractional part like 3.14, -0.5, 2.0. - boolean : a truth value, either true or false. Java is statically typed : you must declare a variable's type before using it, and that type never changes. Declaring and Initializing A declaration names a variable and its type. Initialization gives it a first value. You can combine both: The assignment operator = copies the value on the right into the variable on the left. Read score = 100 as "score gets 100", not "score equals 100". Why Types Matter The type controls: - What values fit : you cannot store 3.14 in an int. - How much memory is used and the legal range. - What operations are allowed (math on numbers, logic on booleans). Reading Input with Scanner To read values typed by a user, AP CSA uses Scanner. Each method matches a type: nextInt() reads an integer; nextDouble() reads a decimal. The Scanner skips whi

Challenge: Echo Three Types