Overloading, scope, null and De Morgan
Small rules that cost marks
- This lesson collects five rules that the multiple-choice questions test again and again: overloading, scope, null, De Morgan's laws, and preconditions.
- Each one is short. Learn the rule, then trace the example until the output makes sense.
Overloading: one name, several methods
- A class can have several methods with the same name if their parameter lists differ: a different number of parameters, different types, or a different order.
- Java looks at the arguments in the call and picks the method that matches.
- A different return type alone is not enough:
int f(int x)anddouble f(int x)cannot both exist.

public class Overload {
public static int area(int side) {
return side * side;
}
public static int area(int width, int height) {
return width * height;
}
public static double area(double radius) {
return 3.14 * radius * radius;
}
public static void main(String[] args) {
System.out.println(area(4));
System.out.println(area(4, 5));
System.out.println(area(2.0));
}
}
- The output is
16,20,12.56. Constructors can be overloaded in the same way.
Scope: where a variable exists
- A variable exists only inside the
{ }braces where it is declared, from the declaration to the closing brace. - A loop variable such as
iinfor (int i = ...)is gone after the loop. - A parameter is a new local variable. For a primitive (
int,double,boolean), it holds a copy of the argument, so changing it does not change the caller's variable.

public class Scope {
public static void addOne(int n) {
n = n + 1;
System.out.println("inside: " + n);
}
public static void main(String[] args) {
int score = 10;
addOne(score);
System.out.println("outside: " + score);
int total = 0;
for (int i = 1; i <= 3; i++) {
int doubled = i * 2;
total += doubled;
}
// i and doubled no longer exist here
System.out.println("total: " + total);
}
}
- The output is
inside: 11,outside: 10,total: 12.
null: a reference to nothing
- A
String, an array or any other object variable holds a reference. It can point at an object, or holdnull, which means "no object". - Calling a method on
null(name.length()) stops the program with aNullPointerException. - Test
name != nullfirst and join the tests with&&. Short-circuit evaluation then skipsname.length()whennameisnull. - A new array of objects,
new String[3], starts full ofnull.

public class Nulls {
public static void main(String[] args) {
String name = null;
if (name != null && name.length() > 0) {
System.out.println("Hello, " + name);
} else {
System.out.println("No name yet");
}
name = "Ada";
if (name != null && name.length() > 0) {
System.out.println("Hello, " + name);
}
String[] seats = new String[3];
System.out.println(seats[0]);
}
}
- The output is
No name yet,Hello, Ada,null.
De Morgan's laws
- To write the opposite of a condition joined by
&&or||: put!on each part and swap&&with||. !(a && b)is the same as!a || !b.!(a || b)is the same as!a && !b.- With comparisons, flip each test: the opposite of
x > 3isx <= 3, notx < 3.

public class Morgan {
public static void main(String[] args) {
int x = 5;
int y = 1;
boolean a = !(x > 3 && y > 3);
boolean b = x <= 3 || y <= 3;
System.out.println(a + " " + b);
boolean c = !(x > 3 || y > 3);
boolean d = x <= 3 && y <= 3;
System.out.println(c + " " + d);
}
}
- Each line prints two equal values (
true true, thenfalse false): the expressions on each line are the same test, written two ways.
Preconditions and postconditions
- A precondition is something that must be true before a method is called, for example "
ahas at least one value". The method does not have to check it; the caller must make sure of it. - A postcondition is what is true after the method runs, for example "returns the largest value in
a". - Free-response questions write them as comments above the method. Read them: they tell you which cases you can ignore.
/**
* Precondition: nums.length > 0
* Postcondition: returns the largest value in nums; nums is unchanged
*/
public static int largest(int[] nums)
Common mistakes
| Wrong | Right | Why |
|---|---|---|
| two methods that differ only by return type | change the parameter list | Java picks a method by its parameters only |
using i after for (int i = ...) |
declare i before the loop if you need it later |
the loop variable's scope ends with the loop |
expecting a method to change an int argument |
return the new value | the parameter is a copy |
name.length() > 0 && name != null |
name != null && name.length() > 0 |
the null test must come first |
!(x > 3) is x < 3 |
!(x > 3) is x <= 3 |
3 itself is not greater than 3 |
| checking a precondition the question gives | trust it | the caller guarantees it, and extra code can lose time |
Now you try
- For each task, first decide which rule from this lesson it tests.
- Press Check answer to test your code.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Write two overloaded methods named greet hacia adentro Greeter. greet(String name) retornos "Hello, " plus the name. greet(String name, int times) returns that greeting repeated times times, joined by single spaces.
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
Complete safeLength(String s). Return the length of s, or 0 when s es null. It must never throw a NullPointerException.
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 3/5 · 200 monedas + un pequeño bono en el primer intento de hoy
isOutside(int x, int low, int high) should return true when x es no from low hasta high (both included). In other words, it returns !(x >= low && x <= high). Use De Morgan's laws to write that test without cualquier !.
Haz clic en Ejecutar para ver la salida aquí.
Dificultad 2/5 · 140 monedas + un pequeño bono en el primer intento de hoy
addBonus should give a score 5 extra points, but the caller's score never changes because the parameter is a copy. Change addBonus so it retornos the new score, and change main to store the result, so the program prints 15.
Haz clic en Ejecutar para ver la salida aquí.