Common mistakes and practice
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Most beginner errors with variables and types are small: a wrong quote, a missing L or f, a name used twice, or a number that quietly comes out wrong. Find them first, then check the fixes.
Find the mistake
Each program below has one wrong line. Some do not compile; some run and print the wrong thing. Pick the line before you look at the answer: spotting a mistake fast is the skill that saves you the most time.
The sections after this one list every mistake with its fix and the lesson that teaches it.
Find the bug
Spot the bug
Select the line that is wrong.
This should print Marks: 0.
Round 1 of 6Compile errors: values and names
These mistakes stop the program from compiling. Read the error message: it names the problem and the line.
variable marks might not have been initialized→ give a local variable a value before you use it:int marks = 0;.variable score is already defined→ write the type only once; change the value withscore = 20;.cannot assign a value to final variable PASS_MARKS→ a constant never changes; dropfinalonly if the value really must change.
Compile errors: quotes, L and f
Each type has its own way to write a literal. Mix them up and Java refuses the line; here are four such lines, fixed.
Example 1 · Four mistakes, fixed
public class Fixed { public static void main(String[] args) { char grade = 'A'; String name = "Ravi"; long views = 3000000000L; float rating = 4.5f; System.out.println(name + " " + grade); System.out.println(views + " views, rating " + rating); }}Ravi A 3000000000 views, rating 4.5
What this code does
char grade = "A";does not compile: acharneeds single quotes,'A'.String name = 'Ravi';does not compile: aStringneeds double quotes,"Ravi".3000000000is too big for anintliteral, so thelongneedsL:3000000000L.4.5is adoubleliteral, so afloatneedsf:4.5f.
Surprises that compile
These are harder to catch: the program runs, but a number comes out wrong. Check your output against what you expected.
457 / 5gives91→ cast before dividing:(double) 457 / 5gives91.4.(int) 99.99gives99→ a cast cuts toward zero; it never rounds.0.1 + 0.2gives0.30000000000000004→ decimals are close, not exact."Total: " + 450 + 50givesTotal: 45050→ add brackets:"Total: " + (450 + 50).
Overflow: when an int gets too big
A video has 2147483647 views, the largest int. One more view and the count shows -2147483648, with no error. This is overflow: an int that goes past its limit wraps round to the other end, like an old odometer.
Example 2 · int overflow and the long fix
public class Overflow { public static void main(String[] args) { int big = 2147483647; System.out.println(big); big = big + 1; // wraps round, no error System.out.println(big); long safe = 2147483647L; safe = safe + 1; System.out.println(safe); }}2147483647 -2147483648 2147483648
What this code does
bigstarts at the largestint. Adding 1 makes it jump to the smallestint, with no error.safeis along, which has room for the bigger number, so the answer is right.
When a whole number can grow very large, like total views or an amount in paise, use long. Press + in the lab and watch x + 1 jump.
Experiment
Overflow lab
Press + to move x up by 1. Watch what happens past 2147483647.
- x
- 2147483640
- x + 1 (int)
- 2147483641
- (long) x + 1
- 2147483641
- largest int
- 2147483647
Finish up
Mark complete & continue
Next: Arithmetic operators