Default Values in AP Computer Science A
In AP Computer Science A, default values are the values Java automatically gives array elements and instance variables that you don't explicitly initialize: 0 for int, 0.0 for double, false for boolean, and null for any reference type (like String or other objects).
What are Default Values?
Default values are Java's "blank slate" settings. When Java creates storage for you but you haven't put anything in it yet, it doesn't leave random garbage there. It fills the slot with a predictable starting value based on the data type.
There are four you need to know cold:
| Type | Default value |
|---|---|
int | 0 |
double | 0.0 |
boolean | false |
any reference type (String, objects, arrays) | null |
The CED states these exact values for arrays. When you create a 1D array (EK 4.3.A.3) or a 2D array (EK 4.11.A.2) with the keyword new, every element starts at the default for its type. So new int[5] gives you five zeros, and new String[3][4] gives you a grid of twelve nulls. The same idea applies to instance variables in a class. If a constructor doesn't assign an instance variable a value, that variable holds its default. One catch: local variables (declared inside a method) do not get default values. Java makes you assign them before you use them, or the code won't compile.
Why Default Values matter in AP® Computer Science A
Default values show up in two units, which is exactly why they're worth a dedicated look.
- Unit 3: Class Creation (Topic 3.4 Constructors). Learning objective 3.4.A asks you to declare instance variables and initialize them in constructors. EK 3.4.A.2 says a constructor sets an object's initial state, "which should include initial values for all instance variables." Default values are what you get when a constructor skips that job, or when you write no constructor at all and Java supplies the default no-parameter one.
- Unit 4: Data Collections (Topics 4.3 and 4.11). Learning objectives 4.3.A and 4.11.A both build on the rule that
newfills every element with its type's default. If you know a freshint[]is all zeros, counting and accumulator patterns make sense. If you know a freshString[]is allnull, you can predict why calling a method on an element blows up.
The big idea is simple. Every piece of storage Java creates has some value from the moment it exists. Reading code on the exam means knowing what that value is before your code ever touches it.
Keep studying AP® Computer Science A Unit 4
How Default Values connect across the course
Constructors and Object State (Unit 3)
A constructor's whole job is to replace default values with meaningful ones. If a BankAccount constructor sets balance but forgets ownerName, that object is born with ownerName equal to null. Think of default values as the factory settings that a constructor is supposed to overwrite.
1D Array Creation with new (Unit 4)
new double[10] hands you ten slots of 0.0. You get a ready-made, zeroed-out array, so a tally array like int[] counts = new int[26]; works right away without a loop to reset it.
2D Arrays as Arrays of Arrays (Unit 4)
A 2D array is just an array of 1D arrays (EK 4.11.A.1), so the default rule applies to every cell in the grid. new boolean[3][3] is nine falses. That's the starting point for things like a game board where nothing has been marked yet.
null and Reference Types (Units 3-4)
For objects, the default isn't an empty object. It's null, meaning "no object here yet." A new String[] has no empty strings in it, only nulls. Calling .length() on one of them throws a NullPointerException.
Are Default Values on the AP® Computer Science A exam?
Multiple choice. Default values usually hide inside a code trace. Common setups include:
- An array is created with
new, only some elements get assigned, and you're asked what the whole array contains. The untouched slots hold0,0.0,false, ornull. - A class has instance variables, and a constructor assigns only some of them. Practice questions like the
BankAccountandRectangleexamples ask which constructor correctly initializes all instance variables. That means spotting which one leaves a field at its default. - A class has no constructor at all, and you're asked what happens when an object is created. Java provides a default no-parameter constructor, and the instance variables keep their default values. You may also need the name of that constructor, which is the default constructor.
Free response. Default values matter most when you write constructors and build arrays. The 2019 FRQ Q2 (StepTracker) had you design a class whose state tracks fitness data over time, so the constructor needed to set up its instance variables with sensible starting values. The 2018 FRQ Q3 (StringChecker) involved writing a class that implements an interface, where setting up instance variables in constructors was part of the work. On FRQs, explicitly initializing every instance variable in your constructor is the safe move. Relying on Java's defaults can work for a counter starting at 0, but writing it out shows the grader you meant it.
Default Values vs Default constructor
A default value is the data Java puts in a slot (like 0 or null). The default constructor is a method Java writes for you, a no-parameter constructor, but only if your class has no constructors at all. They're linked because the default constructor doesn't assign anything, so every instance variable ends up at its default value. If you write even one constructor yourself, Java no longer supplies the default constructor.
Key things to remember about Default Values
The default value for int is 0, for double is 0.0, for boolean is false, and for any reference type is null.
When you create a 1D or 2D array with the keyword new, every element starts at the default value for its type.
Instance variables that a constructor doesn't assign keep their default values, so a forgotten String field ends up null.
If a class has no constructor, Java provides a default no-parameter constructor, and all instance variables keep their defaults.
Local variables declared inside a method do not get default values and must be assigned before use, or the code won't compile.
Calling a method on a null array element or instance variable causes a NullPointerException, which is a common trap in code-tracing questions.
Frequently asked questions about Default Values
What are default values in Java for AP CSA?
They're the values Java automatically gives array elements and unassigned instance variables. Those are 0 for int, 0.0 for double, false for boolean, and null for reference types like String. The CED lists these exact values in EK 4.3.A.3 and EK 4.11.A.2.
Do local variables get default values in Java?
No. Only instance variables and array elements get default values. A local variable declared inside a method has to be assigned before you use it, or the compiler gives you an error.
Is the default value of a String an empty string?
No, it's null. String is a reference type, so an unassigned String instance variable or a slot in new String[5] holds null, not "". Calling a method like .length() on it throws a NullPointerException.
What's the difference between a default value and a default constructor?
A default value is the starting data in a variable or array slot, like 0 or null. A default constructor is the no-parameter constructor Java writes for you when your class has no constructors, and it leaves every instance variable at its default value.
What does new int[3][4] contain in Java?
It's a 3-row, 4-column grid with all 12 elements set to 0. A 2D array is an array of arrays, so the default rule from 1D arrays applies to every cell (EK 4.11.A.2).
Keep studying AP Computer Science A
Connect this key term to the AP exam workflow: review the course, practice questions, and check related study tools.
AP Computer Science A hub
Review units, study guides, and course resources.
AP-style practice
Check this vocabulary in multiple-choice context.
FRQ practice
Apply key concepts in written AP responses.
Score calculator
Estimate the exam score you are working toward.
Cheatsheets
Review the highest-yield facts before practice.
Practice exam
Put the full course together before test day.