Unit 1 · Java setup and first programs
A literal is a value written directly in source code, such as 42, 3.14, or "Java". A constant is a named value that should not change during execution. Java conventionally writes constant names in uppercase with underscores.
final int PASS_MARK = 50;
final double TAX_RATE = 0.05;
int mark = 72;
boolean passed = mark >= PASS_MARK;
System.out.printf("Mark: %d, passed: %b%n", mark, passed);The final modifier prevents reassignment after initialization. Use names such as weeklyMinutes, not vague names such as x. Classes use PascalCase; variables and methods use camelCase.
String concatenation converts values to text, while printf gives controlled formatting. Be aware that "Total: " + 2 + 3 produces "Total: 23"; parentheses make the intended calculation clear.
x, y, and magic numbers into named values.printf.final prevent?"A" + 1 + 2 evaluate to?Mastery task: Build a formatted course summary whose output is readable without seeing the source code. Replace every unexplained literal with a named constant.
1. Which operator compares two primitive values?
===!=:=== compares values; = assigns a value.2. What modifier prevents reassignment?
final prevents a variable from being assigned again after initialization.3. What is "Total: " + 2 + 3?
"Total: 23", because concatenation proceeds left to right after the String is encountered.Rewrite a pass check that uses mark >= 50 so the meaning of 50 is explicit.
final int PASS_MARK = 50;
boolean passed = mark >= PASS_MARK;The name makes the rule easy to find and change.Predict and then run: System.out.println("A" + 1 + 2); and System.out.println("A" + (1 + 2));.
A12 and A3. Parentheses force arithmetic before concatenation.printf with one decimal place for a rate.final int PASS_MARK = 50;
final int MAX_MARK = 100;
final int WEEKLY_TARGET = 300;
double rate = 225.0 / WEEKLY_TARGET;
System.out.printf("Progress: %.1f%%%n", rate * 100);A strong answer explains each constant and avoids names such as x or n when they do not communicate intent.