How to learn Core Java Basics
Core Java basics are the small rules that every later program depends on: values have types, expressions produce results, methods group work, and the JVM begins a simple application in main. The goal is not to memorize every primitive range. It is to predict what a line does before running it and to notice when Java converts a value for you.
Use short programs while learning this topic. Change one value or operator at a time, predict the output, and then run the program. That loop makes integer division, numeric promotion, overflow, and String concatenation much easier to remember than a table of rules.
Recommended learning order
- Values and variables: Start with declarations, assignment, primitive types, String, and descriptive names. Learn that a variable has a declared type even when its value changes.
- Expressions and conversion: Work through arithmetic, precedence, integer division, remainders, casts, and the promotion that occurs when different numeric types meet.
- Input, output, and methods: Read values with Scanner, format output, pass arguments to methods, and return a result instead of placing every statement in main.
- Edge cases: Try zero, negative numbers, large integers, and decimal values. These cases reveal assumptions that friendly sample inputs hide.
Tested example: Calculate an exact average
Casting the total before division makes the operation floating-point division. Without the cast, 7 / 2 is evaluated as the integer 3 before it can be assigned to a double.
public class Main {
static double average(int first, int second) {
return (double) (first + second) / 2;
}
public static void main(String[] args) {
int first = 3;
int second = 4;
System.out.printf("Average: %.1f%n", average(first, second));
}
}Expected output
Average: 3.5Common mistakes
- Expecting integer division to retain a fractional part; at least one operand must be a floating-point value before division.
- Casting a value that is already out of range. Assigning 300 to a byte with a cast does not preserve 300; it wraps to a different value.
- Combining text and arithmetic without parentheses, so "Total: " + 2 + 3 produces "Total: 23".
- Keeping all logic in main instead of extracting a small method whose input and result can be checked independently.
How to practice
Complete the declaration and arithmetic exercises first. Then solve number reversal and digit-sum problems, which combine division and remainder in a loop. Re-run each solution with zero and a negative input and write down whether your contract supports them.
Exercises to do first
- Declare variables — Represent a learner name, completed-problem count, and average score with suitable types.
- Type casting — Compare integer division with an explicit floating-point conversion.
- Sum of digits — Use division and remainder to process a number one digit at a time.