2.1 Writing comments and its type
Comments are text notes added to the program to provide explanatory information about the source code. They are ignored by the compiler. There are three types of comments in Java:
- Single-line comment: Starts with //. Everything from // to the end of the line is treated as a comment. [cite: 3925]
- // This is a single-line comment
- int a = 10;
- Multi-line comment: Enclosed between /* and */. It can span multiple lines. [cite: 3925]
- /* This is a
- multi-line
- comment */
- Documentation comment: Enclosed between /** and */. Used to create API documentation.
2.2 Java token: keywords, identifier, literal, operators and separators
Tokens are the smallest individual units in a Java program. They include:
- Keywords: Reserved words that have a specific meaning to the compiler and cannot be used as variable names (e.g., public, class, static, void, int, double). [cite: 3936]
- Identifiers: Names given to variables, classes, methods, etc., to identify them uniquely. [cite: 3939] Must begin with a letter and cannot contain mathematical operators. [cite: 3946, 3947]
- Literals: Fixed values assigned to variables.
- Operators: Symbols that perform specific operations on operands (e.g., +, -, *, /, %).
- Separators: Symbols used to separate different parts of the code (e.g., ( ), { }, ;, ,).
2.3 Data types: primitive and user-defined data type
Data types specify the size and type of values that can be stored in a variable.
Primitive Data Types


User-Defined Data Types
These are created by the programmer (e.g., Classes, Interfaces, Arrays).
2.4 Variable declaration and assignment, expression
A variable is a memory location that holds a value. [cite: 3944] Its value can change during program execution. [cite: 3934]
type variableName = value;
Example:
int a = 12; // Declaration and assignment
int b; // Declaration
b = 13; // Assignment
An expression is a combination of variables, operators, and method calls that evaluates to a single value (e.g., a + b).
2.5 Control statements: selection statements, looping statement and jump statements
Control statements manage the flow of program execution based on conditions. [cite: 4102]
Selection Statements
- if-else: Routes the execution path in two directions. [cite: 4106]
- if (condition) {
- // block 1
- } else {
- // block 2
- }
- if-else if-else: Tests a series of conditions. [cite: 4267, 4268]
- if (condition1) {
- // block 1
- } else if (condition2) {
- // block 2
- } else {
- // default block
- }
- switch-case: Tests a variable against multiple values. [cite: 4316]
- switch(variable) {
- case value1:
- // code
- break;
- default:
- // default code
- }
Looping Statements
Execute a block of code repeatedly until a condition is met. [cite: 4372]
- while loop: Pretest loop. Evaluates condition before executing the loop body. [cite: 4393, 4451]
- while (condition) {
- // loop body
- }
- for loop: Pretest loop. Initialization, condition, and update are in one line. [cite: 4425, 4451]
- for (initialization; condition; update) {
- // loop body
- }
- do-while loop: Posttest loop. Executes loop body at least once before evaluating condition. [cite: 4446, 4449, 4451]
- do {
- // loop body
- } while (condition);
Jump Statements
- break: Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. [cite: 4333]
- continue: Skips the remaining code in the current iteration and proceeds to the next iteration of the loop.
2.6 Arrays: single dimension array, multi-dimensional array (Rectangular and Jagged)
An array is a static data structure that stores a collection of related data items of the same type. [cite: 4891] Once created, its length is fixed. [cite: 4892]
Single Dimension Array
dataType[] arrayName = new dataType[size];
int[] numbers = new int[10]; [cite: 4905]
Multi-Dimensional Array
Arrays of arrays. [cite: 5096]
Rectangular Array
A 2D array where each row has the same number of columns.
int[][] matrix = new int[3][4]; // 3 rows, 4 columns
Jagged Array
An array of arrays where each sub-array (row) can have a different length.
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[2]; // Row 0 has 2 columns
jaggedArray[1] = new int[4]; // Row 1 has 4 columns
jaggedArray[2] = new int[3]; // Row 2 has 3 columns
2.7 Type conversion and casting
Converting a variable from one data type to another.
Widening (Implicit Type Conversion)
Automatically performed when converting a smaller primitive type to a larger primitive type (e.g., byte to int). [cite: 4038]
byte b = 10;
int i = b; // Widening [cite: 4047]
Narrowing (Explicit Type Conversion / Casting)
Must be done manually when converting a larger primitive type to a smaller primitive type (e.g., double to int). [cite: 4053] It may result in data loss.
double d = 10.5;
int i = (int) d; // Narrowing [cite: 4056, 4058]
2.8 Garbage Collection
Garbage collection in Java is the automated process of deleting unused objects from memory (the heap) to free up space and prevent memory leaks. The JVM automatically tracks object references. When an object is no longer reachable (e.g., its reference is set to null or it goes out of scope), the Garbage Collector reclaims the memory occupied by that object.
2.9 String: creation, concatenation, comparison, modification, changing case and searching
A string is a sequence of characters. [cite: 4512] In Java, strings are objects of the String class and are immutable (their values cannot be changed after creation).
- Creation:
- String s1 = "Hello"; [cite: 4516]
- String s2 = new String("World");
- Concatenation: Joining strings using + or concat() method. [cite: 4564]
- String s3 = s1 + " " + s2;
- String s4 = s1.concat(" Class!"); [cite: 4570]
- Comparison: Using equals(), equalsIgnoreCase(), or compareTo(). [cite: 4574]
- s1.equals(s2); // Returns false [cite: 4579]
- s1.compareTo(s2); // Returns integer value
- Modification (Substring): Extracting a part of a string using substring(). [cite: 4547]
- String sub = s1.substring(1, 4); // "ell" [cite: 4550]
- Changing Case:
- s1.toUpperCase(); // "HELLO" [cite: 4536]
- s1.toLowerCase(); // "hello" [cite: 4541]
- Searching: Finding the index of a character or substring using indexOf(). [cite: 4551]
- s1.indexOf('e'); // Returns 1
2.10 String Buffer Class
The StringBuffer class is used to create mutable (modifiable) strings. Unlike the String class, operations like append and insert can modify the string buffer's content and length without creating a new object. StringBuffer is thread-safe (synchronized), meaning it is safe for use by multiple threads concurrently.
Example operations:
StringBuffer sb = new StringBuffer("start");
sb.append("le"); // "startle"
sb.insert(4, "le"); // "starlet"