Practice Core Java Basics in Java

Learn fundamental Java concepts including variables, data types, operators, and basic I/O operations.

Work through 26 exercises with starter code, expected output, test cases, and hints.

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

  1. 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.
  2. Expressions and conversion: Work through arithmetic, precedence, integer division, remainders, casts, and the promotion that occurs when different numeric types meet.
  3. 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.
  4. 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.5

Change and run this example in the Java compiler

Common 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.

Core Java Basics exercises

  1. Declare Variables Java exercise — Declare and print different variable types
  2. Swap Two Values Java exercise — Swap the values of two variables
  3. Type Casting Java exercise — Convert between different data types
  4. Arithmetic Operations Java exercise — Perform basic math operations
  5. String Concatenation Java exercise — Combine strings together
  6. Modulus Operator Java exercise — Use modulus to get remainder
  7. Sum of Digits Java exercise — Calculate the sum of all digits in a number
  8. Reverse a Number Java exercise — Reverse the digits of an integer
  9. Count Digits Java exercise — Count the number of digits in an integer
  10. Calculate Power Java exercise — Calculate base raised to exponent without Math.pow
  11. Calculate Factorial Java exercise — Calculate factorial of a number
  12. Palindrome Number Check Java exercise — Check if a number is a palindrome
  13. Greatest Common Divisor Java exercise — Find GCD of two numbers
  14. Least Common Multiple Java exercise — Find LCM of two numbers
  15. Armstrong Number Check Java exercise — Check if a number is an Armstrong number
  16. Perfect Number Check Java exercise — Check if a number is perfect
  17. Number to Words Java exercise — Convert a single digit to its English word
  18. Triangle Type Checker Java exercise — Determine if sides form a valid triangle and its type
  19. Happy Number Check Java exercise — Check if a number is a happy number
  20. Strong Number Check Java exercise — Check if a number is a strong number
  21. Automorphic Number Check Java exercise — Check if a number is automorphic
  22. Binary to Decimal Java exercise — Convert a binary string to its decimal equivalent
  23. Decimal to Binary Java exercise — Convert a decimal number to its binary representation
  24. Prime Factorization Java exercise — Find all prime factors of a number
  25. Harshad Number Check Java exercise — Check if a number is divisible by sum of its digits
  26. Neon Number Check Java exercise — Check if sum of digits of square equals the number

Browse the complete Java practice path · Open the online Java compiler