Crash Course — Unit 1: Using Objects and Methods
In simple terms: Hey there. I'm Saavi, and we're going to quickly recap AP CS A Unit 1. This unit is the foundation for everything else in the course — it's about how we represent information with variables and how we use pre-built tools, called objects and methods, to get things done. Mastering the difference between a simple value (`int`) and a complex object (`String`) is the single most important skill you'll build here, and it's tested heavily on the exam.
Crash Course — Unit 1: Using Objects and Methods
1 / 4
- Primitive vs. Reference Types Primitives (
int,double,boolean) store simple values directly. Reference types (String,Scanner, any other object) store a memory addressâa pointer to where the actual object lives. - Class vs. Object A class is the blueprint (like the architectural plan for a house). An object is a specific instance created from that blueprint (like the actual house at 123 Main Street).
- Declaration, Initialization, Assignment Declaration creates a variable (
int score;). Initialization gives it its first value (int score = 0;). Assignment updates its value (score = 100;). - Integer Division & Modulo When you divide two integers, the result is an integer (the decimal is dropped).
7 / 2is3. The modulo operator (%) gives you the remainder.7 % 2is1.
- Casting Explicitly forcing a value to become a different type.
(int) 3.9becomes3(it truncates, it doesn't round). - Method Signature A method's "contract" in the documentation. It tells you the method's name, what data it needs (parameters), and what data it gives back (return type).
- Class (static) vs. Instance Methods Class methods belong to the class itself and are called with
ClassName.method(), likeMath.random(). Instance methods belong to a specific object and are called withobjectName.method(), likemyString.length().
- String Immutability Once a
Stringobject is created, its contents can never be changed. Methods likesubstring()ortoUpperCase()don't change the original string; they create and return a new one. - Object Instantiation The process of creating an object from a class using the
newkeyword and a constructor. This is what actually builds the "house" from the "blueprint." - API (Application Program Interface) A library of pre-written classes (like
StringandMath) that you can use so you don't have to build everything from scratch.
Key Formulas / Terms
- Variable Declaration & Initialization
type variableName = value;int playerScore = 1500;double price = 29.99;boolean isComplete = false;
- Object Instantiation (Creation)
ClassName objectName = new Constructor(arguments);String name = new String("Maya");Scanner inputReader = new Scanner(System.in);
- Calling a Class (static) Method
ReturnType result = ClassName.methodName(arguments);double randomNumber = Math.random();int absoluteValue = Math.abs(-10);
- Calling an Instance Method
ReturnType result = objectReference.methodName(arguments);int len = name.length();String part = name.substring(0, 2);
- Explicit Casting
(newType) expressionint x = (int) 9.9; // x becomes 9double y = (double) 5 / 2; // y becomes 2.5
- Common String Methods
str.length()— returns the number of characters.str.substring(start, end)— returns a new string from indexstartup to (not including)end.str.indexOf(target)— returns the index of the first occurrence oftarget.str.equals(other)— returnstrueif the strings have the same characters.
Exam Traps
- TrapInteger Division. The exam will show
double result = 5 / 2;and ask for the value ofresult. · Counter: The division5 / 2is evaluated first. Since both are integers, the result is integer2. Then, it's assigned to thedoublevariable, becoming2.0. To get2.5, one of the numbers must be a double:5.0 / 2. - TrapComparing Strings with
==. The code will create two separateStringobjects with the same text, likeString s1 = new String("hi"); String s2 = new String("hi");, and then check ifs1 == s2. · Counter:==checks if two references point to the exact same object in memory. It does not check if their contents are the same. For objects likeString, always use the.equals()method to compare their contents:s1.equals(s2). - Trap
substring()End Index. A question will ask for the result ofString name = "Jordan"; name.substring(1, 4);. · Counter: Remember the rule:substring(start, end)includes the character atstartbut excludes the character atend. So this takes characters at indices 1, 2, and 3, which are 'o', 'r', and 'd'. The result is the new string"ord". - TrapConfusing Concatenation and Addition. The code will be
System.out.println("Result: " + 10 + 5);. · Counter: Java evaluates from left to right."Result: " + 10produces the string"Result: 10". Then,"Result: 10" + 5produces the string"Result: 105". If you want to perform the math first, use parentheses:"Result: " + (10 + 5). - TrapObject Reference Assignment. The code will be
Dog d1 = new Dog("Fido"); Dog d2 = d1; d2.setName("Spot");and then ask ford1.getName(). · Counter: The lineDog d2 = d1;does not create a new dog. It creates a new reference,d2, that points to the exact sameDogobject asd1. Think of it as two different remote controls for the same TV. Changing the channel with one remote changes what you see on the one TV. So,d1.getName()will return "Spot".
Quiz me — 20 cards
Tap a card to reveal the answer. Use this to self-test before the exam.
Primitive vs. Reference Types
Primitive vs. Reference Types — what's the key idea?
Primitive vs. Reference Types
�� Primitives (
int, double, boolean) store simple values directly. Reference types (String, Scanner, any other object) store a memory address—a pointer to where the actual object lives.1 / 20