A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Language Basics

Learn Java

Easy

Java is the language of enterprise software and a very common interview choice: everything lives inside classes, the Collections framework is excellent, and the compiler catches many mistakes early. This lesson is an orientation — the class skeleton every program shares, the two ways to read input, the core syntax for this sheet, and what to learn next.

The class skeleton

A Java program is a class with a main method; the file name must match the public class name. There is no way around this boilerplate, so memorise the frame once and reuse it forever. System.out.println prints a line; System.out.print stays on the same line.

import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long sum = 0; for (int i = 0; i < n; i++) { sum += sc.nextInt(); } System.out.println(sum); } }

Reading input: Scanner vs BufferedReader

Scanner is simple — nextInt(), nextLong(), next() for a word, nextLine() for a full line — and is fine for learning and small inputs. Its trap: after nextInt(), the newline stays in the buffer, so a following nextLine() returns an empty string; add a throwaway nextLine() call after numeric reads when you mix the two.

For large inputs Scanner is slow, and competitive code switches to BufferedReader with StringTokenizer, parsing numbers manually. Know that the faster route exists; adopt it when a problem actually times out, not before.

The core syntax tour

Types: int, long (append L to big literals), double, char, boolean, and String — capital S, because String is a class, which is also why you compare strings with .equals(), never ==. Loops and conditions mirror C++ closely, and methods are just functions inside the class — declare helpers static so main can call them directly.

Arrays are objects with a .length; the flexible list is ArrayList<Integer> — note the capitalised wrapper classes (Integer, Long) because collections cannot hold raw primitives. Strings are immutable: building one with += in a loop creates a new string each time, so use StringBuilder when assembling long output or reversed strings.

Your Java road map for this sheet

In order: (1) the skeleton, loops and methods until patterns feel mechanical; (2) arrays and strings, including StringBuilder; (3) recursion; (4) the Java Collections lesson in this module — ArrayList, HashMap, HashSet, PriorityQueue, Deque and the Collections utility methods. Object-oriented design (inheritance, interfaces) matters for development interviews but can wait until your DSA base is built.

Key points

  • Java frame: public class matching the file name, static main, import java.util.* — memorise it once.
  • Scanner is easy but slow and has the leftover-newline trap after nextInt(); BufferedReader is the fast route.
  • Compare strings with .equals(), never ==; build long strings with StringBuilder, not += in a loop.
  • Collections need wrapper types (Integer, not int) — ArrayList<Integer> is your default dynamic array.
  • Road map: loops/methods → arrays/strings → recursion → Collections.

Suggested questions

Java Collections The companion lesson: ArrayList, HashMap, HashSet, PriorityQueue and Collections utilities.
Pattern 1 — Rectangular Star Pattern First reps with nested loops inside the class skeleton.
Count all Digits of a Number ↗ while-loop and % practice with a Scanner-driven main.
Basic Maths The concept tour behind the digit, divisor and GCD problems ahead.

Source note: this lesson follows the structure of Striver's public A2Z DSA course (takeUforward). Each problem links to its original platform, and all problems and content belong to their respective owners — STUDY-BUDDY does not claim ownership of the sheet or its problems, and no permission beyond public linking is claimed.