Objects vs Primitives

Part of: Using Objects

Two Kinds of Data Java splits all data into two families: primitive types and reference types (objects). Knowing which is which controls how values are stored, copied, and compared. Primitive Types The AP CSA Java subset uses three primitives: - int : whole numbers, e.g. 42 - double : decimal numbers, e.g. 3.14 - boolean : true or false A primitive variable holds the actual value directly in its memory slot. When you write int x = 5;, the box named x literally contains 5. Reference Types (Objects) Everything else is an object : String, Scanner, arrays, and any class you write. An object variable does not hold the object itself. It holds a reference : an arrow pointing to where the object lives in memory. Objects are richer than primitives: they bundle data with methods (behaviors) you can call using dot notation, like s.length(). Primitives have no methods, you cannot write n.something(). Spotting the Difference - Object types start with a capital letter by convention: String, Integer, Math. - Primitive types are lowercase keywords : int, double, boolean. - The literal null is a reference that points to no object . Primitives can never be null. Why It Matters The difference shapes

Challenge: Classify the Type