public class SomeClass { public static void main(String[] args) { System.out.println("I run!"); } }
System.out.print("I print out! "); System.out.println("I'm on the same line!"); System.out.println("I'm on the next line!");
Scanner scanner = new Scanner(System.in); ... String text = scanner.nextLine();
Scanner fileIn = new Scanner(new File("somefile.txt")); while(fileIn.hasNextLine()) { String line = fileIn.nextLine(); ... }
PrintWriter fileOut = new PrintWriter(new FileWriter("somefile.txt")); ... fileOut.println("some text"); ... fileOut.close();
Code | Behavior |
---|---|
String[] names; | Declares names to be of type String[]. names is currently null, but is capable of referring to an array of Strings. |
names = new String[3]; | names refers to a new array of size 3. The array contains 3 nulls, but it is capable of referring to 3 Strings. |
names.length | Evaluates to 3--the length of the names array |
names[0] = "Dave"; | The first position in the array names now refers to the String "Dave". |
copy = names; | Does not create a new array. copy refers to the same array as names |
names[1] = "fberg"; | The second position in the array names, and therefore copy, now refers to the String "fberg". |
copy[1] | Evaluates to "fberg" |
double[] roots = {1.0, 1.4142, 1.7321, 2.0}; | Declares and initializes roots to refer to an array of the given 4 doubles. |
public void giveSnack(Food snack) { friend.eat(snack); }Suppose that friend's eat method may throw the compile-time exception AllergicToPeanutsException. Java won't let you compile your giveSnack method until you explicitly handle the AllergicToPeanutsException. There are two ways to handle a compile-time exception.
public void giveSnack(Food snack) throws AllergicToPeanutsException { friend.eat(snack); }Your code will compile, but now anyone who calls your giveSnack method will need to handle the AllergicToPeanutsException.
public void giveSnack(Food snack) { try { friend.eat(snack); } catch(AllergicToPeanutsException e) { friend.starve(); } }
public interface Player { int removeFrom(int total); } public class SimplePlayer implements Player { public int removeFrom(int total) { return 1; } }
public abstract class Player { private String name; public Player(String n) { name = n; } public String getName() { return name; } public abstract int removeFrom(int total); } public class SimplePlayer extends Player { public SimplePlayer(String name) { super(name); } public int removeFrom(int total) { return 1; } }
Error Message | What It Usually Means |
---|---|
something expected | The parser was surprised by a symbol you wrote at or just before this point. |
cannot find symbol -- class | You need to import that class. |
cannot find symbol -- method | You got the method name wrong, or you called it on the wrong class. |
class is public, should be declared in a file named | Your class name and file name must match exactly. |
incompatible types -- expected type | Make sure you understand why it found what it did, and why it expected what it did. |
incompatible types -- expected type | Make sure you understand why it found what it did, and why it expected what it did. |
incompatible types -- expected variable | You used = instead of ==. |
missing method body | Your method declaration line has a semicolon. |
missing return statement | The compiler found a pathway through your non-void method that does not reach a return statement. |
non-static method cannot be referenced from a static context | You called a method on a class, instead of on a specific instance. |
possible loss of precision | You assigned a double value to an int variable. |
variable might not have been initialized | The compiler found a pathway through your method where you access the value of a variable before you've assigned anything to it. |
Error Message | What It Usually Means |
---|---|
My program freezes. | You have an infinite for/while loop. |
ArrayIndexOutOfBoundsException | You tried to access an array element with an index that was too high or too low. |
ClassCastException | You lied when you promised that some object was an instance of a specific class. |
NoSuchMethodError: main | You tried to run a class without a main method. |
NullPointerException | You tried to call a method on null (or access an array element of null). |
OutOfMemoryError | You are constructing new objects in an infinite for/while loop. |
StackOverflowError | You have an infinite recursion. |
StringIndexOutOfBoundsException | You tried to access a character in a String with an index that was too high or too low. |