This summary covers the essential building blocks of Java programming, from displaying information and handling user input to working with data, performing calculations, and using advanced mathematical tools. Mastering these concepts is crucial for success in AP Computer Science A.
An application's ability to communicate with the user is fundamental. This involves both displaying information (output) and receiving information (input).
System.out.println(): Prints the given data to the console and moves the cursor to the
next line.
System.out.print(): Prints the given data to the console and leaves the cursor on
the same line. This is useful for prompting the user for input.
The Scanner class is the standard tool for reading keyboard input, making programs
interactive. Using it involves three steps:
import java.util.Scanner; to the top of your file.
Scanner input = new Scanner(System.in);.
input.nextLine(): Reads an entire line of text as a String.
input.nextInt(): Reads the next token of input as an int.
input.nextDouble(): Reads the next token of input as a double.
The Scanner Issue: A common bug occurs when mixing numeric and string input. Methods like
nextInt() and nextDouble() read the number but leave the newline (\n) character in the input stream. A subsequent call to nextLine() will read this leftover newline as an empty string.
Java is a statically-typed language, meaning all data must have a defined type that cannot be changed.
Primitive Types: Store simple, single values. The core primitives for the AP exam are:
int: Whole numbers (100, -5).
double: Numbers with decimal points (98.6, -0.5).
boolean: Logical values, true or false (covered in more detail later).
Objects: Store more complex data and can have associated behaviors (methods).
String: A sequence of characters enclosed in double quotes (").
Variable: A named container for storing a value. The value can be changed.
int score; score = 0; score = 100; Constant: A variable whose value cannot be changed after its initial assignment. It is declared with the final keyword.
final double TAX_RATE = 0.07; playerScore, lastName).
MAX_HEALTH,
TAX_RATE).
Operators are special symbols used to perform operations on variables and values.
Arithmetic: + (add), - (subtract), * (multiply), / (divide), %
(modulus/remainder).
Integer Division: When / is used with two integers, the result is truncated (the decimal part
is discarded). 10 / 3 is 3.
String Concatenation: The + operator joins strings. If one operand is a String, the other is converted
to a String. Use parentheses () to control the order of operations:
"Result: " + (5 + 10) → Result: 15
"Result: " + 5 + 10 → Result: 510
These operators provide a concise way to modify a variable's value.
Compound Assignment: score += 5; is shorthand for score = score + 5;. This works for +, -, *, /, and %.
Increment/Decrement: ++ and -- add or subtract 1 from a variable. Their behavior depends on their
position:
++x): The value is changed before it is used in an expression.
x++): The original value is used in the expression, and then it is changed.
Use comments (//) to document your code and make it understandable for yourself
and others.
Type casting is the explicit conversion of one data type to another.
The syntax is (type) value.
double -> int): Truncates the decimal. (int) 9.99 becomes 9.
int -> double): Forces floating-point division. (double) 10 / 3 becomes 3.333....
A built-in Java library for common mathematical functions. No import is needed.
Math.PI: The constant π.
Math.abs(x): Returns the absolute value of x.
Math.pow(base, exp): Returns base raised to the power of exp.
Math.sqrt(x): Returns the square root of x.
Math.round(x): Rounds a double to the nearest whole number (long).
Math.random(): Returns a random double value in the range [0.0,1.0). To generate a random integer in a specific range, use the
formula:
(int)(Math.random() * range) + startValue Special character sequences inside a String literal.
\n Newline
\" Double quote
\\ Backslash
\t Tab