This means that there is a null value or object which is trying to execute some method.
Dog d = null;
d.bark();//throws nullpointer exception
You are trying to assign an object or primitive to a variable of a different type.
Cat c = new Dog();
This happens if you don't cast a double to an int.
int x = 4.0;
These are errors that are caught before the program runs; these are usually syntax errors or invalid access to private instance variables.
return 4 //no semicolon causes error
These are errors that are missed by the compiler. Nullpointer exception is an example of this.
This type of error isn't an actual Java exception - it refers to how certain numbers aren't properly represented in binary. If you see an AP question that asks why these numbers aren't equal even though they are equal in non-Java math, the right answer is probably the one that talks about some roundoff error or how binary can't represent all numbers properly (e.g. .1 in decimal is .1111111.... in binary).
Errors aren't tested extensively on the AP exam but are important for debugging.
compareTo is a method used to compare objects (but not to compare primitives). For custom classes, you write the compareTo method and use it to compare objects. compareTo returns a positive int if the object calling the method is greater than the parameter. If both objects are equal then it returns 0. If the parameter is greater than the object calling the method, it returns a negative int.
Integer a = new Integer(5);
Integer b = new Integer(7);
Integer c = new Integer(7);
a.compareTo(b);//returns a negative int as 5 < 7
b.compareTo(a);//returns a positive int as 7 > 5
b.compareTo(c);//returns zero as 7 = 7
Here is a sample compareTo method for the Dog class:
public int compareTo(Dog d){
if(this.size > d.getSize()){
return 1;
}
else if(this.size < d.getSize()){
return -1;
}
else{
return 0;
}
}
This method can be condensed to one line:
public int compareTo(Dog d){
return this.size() - d.getSize();
}
When you compare strings, the larger string will be the one that is "alphabetically" later in the dictionary. For example, dog is larger than cat because dog comes later in the dictionary. For non-words, such as "abcd" and "sadlfkn", "sadlfkn" is larger because s comes later in the dictionary than a.
The toString method is used when you have to get info about an object. If you try printing an object, let's say our Dog object, this is what you may get:
Dog@7452e781
What Java is printing is the object's location on the memory. However, this isn't entirely useful for debugging. What you can do is override the default toString method:
public String toString(){
return this.breed + " size: " + this.size;
}
When you print out the object, you don't have to add the toString() method:
Dog d = new Dog(10, "German Shepherd");
System.out.println(d);
Console:
German Shepherd size: 10
A scope refers to the region bounded by curly braces {}
. Variables are limited to a scope:
public class Dog{
private int size;
private String breed;
public Dog(String s, int x){
size = x; //variables x and s can only be used inside these curly braces
breed = s;
}
}
The only variables that can be accessed by other methods in the class are the instance variables. Scopes are also present in loops.
for(int i =0; i < 10; i++){...}
i = 7;//doesn't work - i was instantiated in the for loop only
Convert an int to a string: just add an empty string.
String num = 5 + "";
Convert String to int:
Integer.parseInt(stringValue);