The building blocks of Java: how code gets compiled and run, the three kinds of errors, int, double and boolean variables, arithmetic (including the integer-division trap), casting and overflow, calling methods, the Math class, creating objects, and the String methods on the Java Quick Reference.
An algorithm is a step-by-step process, written in words or diagrams, done in sequence. Programmers write code in an IDE; the compiler checks it and catches syntax errors before it can run. Logic errors give wrong results and are found by testing. Run-time errors, including exceptions, stop a running program.
Data types are primitive (int, double, boolean) or reference (objects). A variable has a name and a type. println moves to a new line; print doesn't. String literals use double quotes and escape sequences `\"`, `\\` and `\n`. An int op int gives an int, so 7 / 2 is 3; any double makes the result a double. % gives the remainder; *, / and % come before + and -.
The value on the right of = is stored in the variable on the left; a variable must be initialized before use, and reference variables can hold null. Scanner reads input. (int) truncates a double; (double) widens an int. ints are limited to Integer.MIN_VALUE…MAX_VALUE, and going past overflows. Doubles have round-off error. +=, -=, *=, /=, %=, ++ and -- update a variable.
Libraries are collections of classes grouped into packages, and an API specification tells you how to use them. A class defines a reference type: attributes (data, in variables) and behaviors (methods). Comments (//, /* */, /** */) are ignored by the compiler. Preconditions must be true before a method runs; postconditions are true after.
A method is a named block of code that runs when called; procedural abstraction lets you use it without knowing how it works. A signature is the name plus the ordered parameter types. Void methods return nothing; non-void return values must be stored or used. Arguments are passed by value. Overloaded methods share a name with different signatures. Class (static) methods are called with the class name.
Math is in java.lang and has only class methods: abs, pow, sqrt and random. Math.random() returns a double from 0.0 up to but not including 1.0; multiply, add and cast to get random ints or doubles in a range.
A class is the blueprint; an object is a specific instance with its own attributes. Subclasses inherit from superclasses, and every class is a subclass of Object. A reference variable holds an object's address or null. Objects are created with new and a constructor, which has the class's name; constructors can be overloaded and receive arguments by value.
Instance methods are called on an object with the dot operator. Calling a method on a null reference throws a NullPointerException.
Strings are immutable objects in java.lang. + and += concatenate, converting primitives (and objects, via toString) to text. Indices run from 0 to length() − 1; going outside throws StringIndexOutOfBoundsException. Quick Reference methods: length, substring (two forms), indexOf (−1 if missing), equals and compareTo.