Unit 1: Using Objects and Methods
A plain-language study guide to AP Computer Science A Unit 1, Using Objects and Methods: all 76 ideas across 15 topics, each explained for students.
Everything the AP Computer Science A exam can ask about in Unit 1, explained in plain language. This unit is roughly 15–25% of the multiple-choice section and has 15 topics and 76 ideas.
Each idea has a code like 1.2.A.1: the unit, the topic, the learning objective (the letter), then the idea itself.
The codes on the practice questions link straight to the idea they tested. These are summaries in our own words.
For the exact official wording, see the
AP Computer Science A Course and Exam Description.
Topics in this unit
- 1.1 Introduction to Algorithms, Programming, and Compilers
- 1.2 Variables and Data Types
- 1.3 Expressions and Output
- 1.4 Assignment Statements and Input
- 1.5 Casting and Range of Variables
- 1.6 Compound Assignment Operators
- 1.7 Application Program Interface (API) and Libraries
- 1.8 Documentation with Comments
- 1.9 Method Signatures
- 1.10 Calling Class Methods
- 1.11 Math Class
- 1.12 Objects: Instances of Classes
- 1.13 Object Creation and Storage (Instantiation)
- 1.14 Calling Instance Methods
- 1.15 String Manipulation
Topic 1.1: Introduction to Algorithms, Programming, and Compilers
Practice this topic: Topics 1.1–1.2: Algorithms, Compilers, and Variables
1.1.A — Describe an everyday routine or pattern as a series of steps, using written words or a diagram.
- 1.1.A.1 An algorithm is a list of steps for getting a job done or solving a problem. You can show one with plain sentences or with a diagram, like a flowchart.
- 1.1.A.2 Sequencing means the steps happen in a set order, and the computer finishes one step before moving to the next. Changing the order can change the result.
1.1.B — Explain how Java code gets compiled and then run, and what tools help with each stage.
- 1.1.B.1 You can type Java in any plain text editor. Most programmers use an IDE (integrated development environment), a program that bundles tools for writing, compiling, and running your code in one place.
- 1.1.B.2 The compiler (the tool that checks and translates your code before it runs) catches some kinds of mistakes. You must fix every mistake it finds before the program is allowed to run.
1.1.C — Recognize the different kinds of programming errors and tell which kind a given mistake is.
- 1.1.C.1 A syntax error means you broke the grammar rules of Java, like a missing semicolon or a misspelled keyword. The compiler finds these for you.
- 1.1.C.2 A logic error means your program runs but does the wrong thing because the plan or code is flawed. The compiler will not catch it. You find it by testing with specific inputs and comparing to the answer you expected.
- 1.1.C.3 A run-time error is a mistake that only shows up while the program is running. It usually makes the program stop suddenly instead of finishing normally.
- 1.1.C.4 An exception is a kind of run-time error caused by something unexpected that the compiler could not spot ahead of time. It cuts your program's normal flow short.
Topic 1.2: Variables and Data Types
Practice this topic: Topics 1.1–1.2: Algorithms, Compilers, and Variables
1.2.A — Pick the best data type category (primitive or reference) for a given situation.
- 1.2.A.1 A data type describes which values something can hold and which operations you can do with those values. Every type is either primitive (a basic built-in kind of value) or reference.
- 1.2.A.2 Primitive types cover the basic values you work with in this class: numbers and true/false values. Each one sets what values are allowed and which operations work on them.
-
1.2.A.3 A reference type is used for objects, meaning anything that is not a primitive type.
Stringis one example.
1.2.B — Write code that declares variables to hold numbers and true/false values.
-
1.2.B.1 You will use three primitive types.
intholds whole numbers,doubleholds real numbers (numbers that can have decimals), andbooleanholds eithertrueorfalse. You will not needlong,short,byte,float, orchar. -
1.2.B.2 Think of a variable as a labeled box in memory. It has a name and a data type, and its value can change while the program runs. When the type is primitive (like
int), the box holds the actual value.
Topic 1.3: Expressions and Output
Practice this topic: Topics 1.3–1.6: Expressions, Assignment, Casting, and Compound Operators
1.3.A — Write code that prints output and figure out exactly what it will display.
-
1.3.A.1
System.out.printandSystem.out.printlnboth show text on the screen.printlnthen moves the cursor down to the next line, butprintleaves the cursor where it is, so the next output continues on the same line.
1.3.B — Write code that uses string literals and predict what the result will be.
-
1.3.B.1 A literal is a value written directly in your code, exactly as it is, such as
42ortrue. -
1.3.B.2 A string literal is text written directly in your code, wrapped in double quotes, like
"hello". -
1.3.B.3 Escape sequences are special character combos inside a String that start with a backslash and mean something special to Java. You need three of them.
\"puts a double quote inside the String.\\puts a single backslash.\nstarts a new line.
1.3.C — Write arithmetic expressions and work out the value they produce.
-
1.3.C.1 An arithmetic expression combines numbers, variables, and operators to produce a number. In this course, those expressions are of type
intordouble. -
1.3.C.2 Java's math operators are
+(add),-(subtract),*(multiply),/(divide), and%(remainder). Math on twointvalues gives anint. If at least one value is adouble, you get adouble. Special results like infinity and NaN are not covered. -
1.3.C.3 Dividing two
intvalues drops everything after the decimal point, so7 / 2is3. If at least one number is adouble, you get the full quotient, so7 / 2.0is3.5. -
1.3.C.4 The
%operator gives what is left over after dividingabyb. For example,7 % 3is1. You only need to work withathat is 0 or more andbthat is greater than 0. -
1.3.C.5 You can chain operators into a longer expression. Java decides how to group the pieces by precedence (which operators go first).
*,/, and%go before+and-. Parentheses can override that order. When operators tie, Java goes left to right. -
1.3.C.6 If your code divides an
intby theintvalue0, Java throws anArithmeticException(a run-time error). Dividing by zero when adoubleis involved is not covered here.
Topic 1.4: Assignment Statements and Input
Practice this topic: Topics 1.3–1.6: Expressions, Assignment, Casting, and Compound Operators
1.4.A — Write assignment statements using expressions and figure out what value ends up stored in the variable.
-
1.4.A.1 A variable needs a value before you use it in an expression, and the value must fit the variable's type. The first time you give it a value, it is initialized. A reference variable can hold an object or
null, a special value that means it points to no object. -
1.4.A.2 The
=operator gives a variable its first value or replaces its current one. Java works out the right side first, then stores the result in the variable on the left. Chaining likea = b = 4is not covered. - 1.4.A.3 When your program runs, Java evaluates an expression down to one single value. That value has a type, which depends on what the expression is made of.
1.4.B — Write code that reads input from the user.
-
1.4.B.1 Programs can receive input in lots of forms, such as touch, sound, pictures, or typing. One option for reading typed text from the keyboard is the
Scannerclass. No particular kind of input is required.
Topic 1.5: Casting and Range of Variables
Practice this topic: Topics 1.3–1.6: Expressions, Assignment, Casting, and Compound Operators
1.5.A — Write code that casts between primitive types in math expressions and work out the resulting value.
-
1.5.A.1 A cast tells Java to treat a value as a different type. Writing
(int)or(double)in front of a value converts betweendoubleandint, in either direction. -
1.5.A.2 Casting a
doubleto anintchops off the decimal part without rounding. For example,(int) 3.99is3. -
1.5.A.3 Sometimes Java automatically turns an
intinto adoublefor you. This is called widening, and you do not need to write a cast for it. -
1.5.A.4 To round a
doublexto the nearest whole number, use(int) (x + 0.5)whenxis 0 or positive. Use(int) (x - 0.5)whenxis negative.
1.5.B — Describe when an integer calculation produces a value outside the range an int can hold.
-
1.5.B.1
Integer.MAX_VALUEis a constant for the biggestintJava can store.Integer.MIN_VALUEis a constant for the smallest one. -
1.5.B.2 An
intis stored in a fixed amount of memory (4 bytes), so it can only hold values fromInteger.MIN_VALUEup toInteger.MAX_VALUE, including both ends. -
1.5.B.3 If a calculation would produce an
intoutside the allowed range, integer overflow happens. You still get anintin range, but it is probably not the number you expected.
1.5.C — Describe situations that make the results of calculations less accurate.
-
1.5.C.1 A computer gives each data type a limited amount of memory. If a
doubleresult needs more precision than fits, a round-off error happens and Java stores the closest value it can. Usingintvalues avoids these rounding errors. Special decimal types are not covered.
Topic 1.6: Compound Assignment Operators
Practice this topic: Topics 1.3–1.6: Expressions, Assignment, Casting, and Compound Operators
1.6.A — Write assignments using compound operators and increment or decrement, and work out the value stored afterward.
-
1.6.A.1 Compound assignment operators (
+=,-=,*=,/=,%=) are shortcuts. They do the math between the variable and the right-hand value, then store the result back in the variable. For example,x += 3does the same thing asx = x + 3. -
1.6.A.2
++adds 1 to a numeric variable and--subtracts 1, and the new value is stored back in the variable. You only need the form after the variable, likex++, and only as its own statement.
Topic 1.7: Application Program Interface (API) and Libraries
Practice this topic: Topics 1.7–1.9: APIs, Comments, and Method Signatures
1.7.A — Name the data and actions that a class from a library offers, using the API documentation.
- 1.7.A.1 A library is a bundle of ready-made classes you can reuse. An API (application programming interface) is the guide that explains how to use them. Each class is a reference type, classes are organized into packages, and you can make objects from them.
- 1.7.A.2 A class has attributes (the data it keeps, stored in variables) and behaviors (the things its objects can do, or that you can do to them, written as methods).
Topic 1.8: Documentation with Comments
Practice this topic: Topics 1.7–1.9: APIs, Comments, and Method Signatures
1.8.A — Use comments to explain what a piece of code does and how it should be used.
-
1.8.A.1 Comments are notes for people, including you later. The compiler skips them, so they never run. Java has three kinds:
/* */for a multi-line block,//for the rest of one line, and/** */for Javadoc, which is used to build API documentation. - 1.8.A.2 A precondition is something that has to be true right before a method runs if you want it to work properly. The method is not required to check that the precondition actually holds.
- 1.8.A.3 A postcondition is something that is guaranteed to be true once a method finishes. It describes the result, such as the value returned or what an object's attributes now hold.
Topic 1.9: Method Signatures
Practice this topic: Topics 1.7–1.9: APIs, Comments, and Method Signatures
1.9.A — Pick the right method to call by reading its documentation and its signature.
- 1.9.A.1 A method is a named chunk of code that runs only when you call it. A block is any code wrapped in curly braces. Procedural abstraction means you can use a method just by knowing what it does, without seeing how it works inside.
- 1.9.A.2 A parameter is a variable listed in a method's or constructor's header, usable in its body, that receives the values passed in. A method signature is the name plus the ordered list of parameter types, or an empty list if there are no parameters.
1.9.B — Explain how to call a method, including passing arguments and using any returned value.
-
1.9.B.1 A
voidmethod gives nothing back, so you call it as its own statement. You cannot use the call as part of an expression, such as on the right side of an assignment. -
1.9.B.2 A method that is not
voidhands back a value of the type named in its header. To make use of that value, save it in a variable or use the call inside a larger expression. - 1.9.B.3 An argument is the actual value you give a method when calling it. Arguments must match the parameter list in count, order, and type. Java uses call by value, so each parameter starts as its own copy of the argument.
- 1.9.B.4 Overloaded methods share one name but have different signatures, meaning their parameter type lists differ.
-
1.9.B.5 Calling a method pauses the current line of execution and runs the method's statements first. When the method ends, either at its last statement or at a
return, the program picks up right after the call.
Topic 1.10: Calling Class Methods
Practice this topic: Topics 1.10–1.11: Class Methods and the Math Class
1.10.A — Write code that calls class methods and work out what value or effect each call produces.
-
1.10.A.1 A class method belongs to the class itself, not to any single object. You spot one by the keyword
staticplaced before the method name in its header. - 1.10.A.2 You normally call a class method by writing the class name, a dot, and the method name. If you are calling it from inside the same class that defines it, you can leave off the class name.
Topic 1.11: Math Class
Practice this topic: Topics 1.10–1.11: Class Methods and the Math Class
1.11.A — Write expressions that use Math methods and figure out the value they produce.
-
1.11.A.1
Mathlives in thejava.langpackage. Everything injava.langis available automatically, so you do not need animportto use it. -
1.11.A.2 Every method in
Mathis a class method. These come up often and are on the reference sheet.static int abs(int x)gives the absolute value of anint.static double abs(double x)gives the absolute value of adouble.static double pow(double base, double exponent)givesbaseraised to the powerexponent.static double sqrt(double x)gives the square root, never negative.static double random()gives adoublethat is at least 0.0 and less than 1.0.
-
1.11.A.3 You can stretch, shift, and cast the result of
Math.random()with arithmetic to get a randomintordoublein any range you need. For each end of the range, check whether it can be picked (inclusive) or cannot (exclusive).
Topic 1.12: Objects: Instances of Classes
Practice this topic: Topics 1.12–1.15: Objects, Instantiation, and Strings
1.12.A — Explain how a class and the objects made from it are related.
- 1.12.A.1 A class is the blueprint that defines what data and abilities its objects have. An object is one actual instance built from that blueprint, with its own values for the attributes.
- 1.12.A.2 Related classes can share common attributes and behaviors by putting them in one superclass. Subclasses extend it and reuse what it has without rewriting it. This is called inheritance. Designing your own inheritance is not required in this course.
-
1.12.A.3 Every class in Java is a subclass of
Object, directly or indirectly.
1.12.B — Write variable declarations whose type is a class so they can hold object references.
- 1.12.B.1 A variable whose type is a class holds an object reference, which you can picture as the address in memory where that object is stored.
Topic 1.13: Object Creation and Storage (Instantiation)
Practice this topic: Topics 1.12–1.15: Objects, Instantiation, and Strings
1.13.A — Tell which constructor is being called by matching the call to a constructor signature.
- 1.13.A.1 A constructor is special code that creates an object when it is called. It always has exactly the same name as its class.
- 1.13.A.2 A constructor signature is the constructor's name (same as the class) plus the ordered list of parameter types. In the constructor header, that list shows each parameter's type and its variable name.
- 1.13.A.3 Overloaded constructors are several constructors in one class that each have a different signature, so you can create objects in more than one way.
1.13.B — Declare variables with the correct class type so they can refer to objects.
-
1.13.B.1 A variable of a class type holds either a reference to an object or
null, which means it refers to no object at all.
1.13.C — Write code that creates an object by calling a constructor with new.
-
1.13.C.1 You normally build an object with the
newkeyword plus a constructor call, likenew ClassName(arguments). A class can offer several constructors, and you choose the one that fits what you want. - 1.13.C.2 Constructor parameters let you pass in values so the new object starts out with the attribute values you want.
- 1.13.C.3 A constructor argument is the value you supply when calling a constructor. It must match the signature's parameter list in count, order, and type. Java passes by value, so each parameter begins as a copy of the argument.
- 1.13.C.4 Calling a constructor temporarily pauses your code so the constructor's statements run first. After its last statement, the program continues on the line right after the call.
Topic 1.14: Calling Instance Methods
Practice this topic: Topics 1.12–1.15: Objects, Instantiation, and Strings
1.14.A — Write code that calls instance methods on objects and predict the result of each call.
-
1.14.A.1 An instance method is called on an object. Write the object's name, then a dot, then the method, like
obj.go(). -
1.14.A.2 If a variable holds
nulland you call a method on it, there is no object to run it, so Java throws aNullPointerException.
Topic 1.15: String Manipulation
Practice this topic: Topics 1.12–1.15: Objects, Instantiation, and Strings
1.15.A — Create String objects, combine them, and work out what the resulting String is.
-
1.15.A.1 A
Stringobject holds a sequence of characters. You can make one with a string literal in double quotes, like"hi", or by calling aStringconstructor. -
1.15.A.2
Stringis in thejava.langpackage, which Java makes available automatically, so you do not need animportfor it. -
1.15.A.3 A
Stringis immutable, meaning it can never be changed after it is made. Calling a method on aStringdoes not alter it, though the method may give back a new one. -
1.15.A.4 You can join two
Stringobjects with+or+=, which builds a newString. You can also join a primitive value to aString, and Java quietly converts the primitive into text first. -
1.15.A.5 Joining a
Stringwith any object makes Java call that object'stoStringmethod, which returns text describing it. Every class hastoStringbecause all classes inherit fromObject. Many classes override it (replace it with their own version). Writing your own override is not required in this course.
1.15.B — Call methods on String objects and predict what each call returns.
-
1.15.B.1 A
Stringhas index positions starting at 0 and ending one before its length. Using an index outside that range causes aStringIndexOutOfBoundsException. -
1.15.B.2 These
Stringmethods are on the reference sheet.int length()gives how many characters theStringhas.String substring(int from, int to)gives the part starting at indexfromand stopping at indexto - 1.String substring(int from)gives everything from indexfromto the end, the same assubstring(from, length()).int indexOf(String str)gives the index wherestrfirst appears, or -1 if it is not found.boolean equals(Object other)givestrueif both have exactly the same characters, otherwisefalse.int compareTo(String other)gives a negative number if this one comes first alphabetically, zero if they are equal, and a positive number if it comes later. Comparing aStringto a non-Stringwithequalsis not required here.
-
1.15.B.3 To get a one-character
Stringfrom positionindex, callsubstring(index, index + 1).