DSA Day 1: Getting Started and Learning the Basics of Java
Welcome to Day 1 of my Data Structures and Algorithms (DSA) journey! Today, I am laying down the ultimate foundation. I will cover all the core basics of Java that you need to be familiar with before solving complex algorithms.
This guide maps directly to the standard topics for Step 1.1: Things to know in Java (similar to C++ basics).
1. Input & Output in Java
To interact with console inputs and outputs, Java offers standard utilities.
Standard Output
System.out.print() outputs text, while System.out.println() outputs text and moves the cursor to a new line.
Standard Input (Scanner Class)
The Scanner class in java.util package is commonly used to read different types of inputs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner;
public class BasicIO {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer: ");
int x = sc.nextInt();
System.out.print("Enter a string (single word): ");
String str = sc.next();
System.out.println("Output: " + x + " and " + str);
sc.close();
}
}
2. Java Basics & Data Types
Java is a statically typed language. Every variable must be declared with a data type.
| Type | Size | Range / Description |
|---|---|---|
int |
4 bytes | $-2^{31}$ to $2^{31}-1$ |
long |
8 bytes | $-2^{63}$ to $2^{63}-1$ (used for large values) |
float |
4 bytes | Single-precision fractional numbers |
double |
8 bytes | Double-precision fractional numbers (preferred for precision) |
char |
2 bytes | Single character (e.g., 'a', '5') |
boolean |
1 bit | true or false |
Note: In competitive programming, be mindful of overflows. If the answer can exceed $2 \times 10^9$, use long instead of int.
3. If-Else / Conditional Statements
Conditional statements are used to make decisions in your code.
1
2
3
4
5
6
7
8
9
int marks = 85;
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 80) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}
4. Switch Case
The switch statement executes one statement from multiple conditions. It is cleaner than nested if-else when checking a single variable against specific constants.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday"); // Output
break;
default:
System.out.println("Weekend");
}
Note: Always remember the break statement to prevent fall-through behavior.
5. What are Arrays and Strings?
Arrays
An array is a linear data structure containing a fixed number of elements of the same data type.
1
2
3
4
5
6
7
8
// Declaration & Initialization
int[] arr = new int[5];
arr[0] = 10;
arr[1] = 20;
// Directly initializing
int[] primeNumbers = {2, 3, 5, 7, 11};
System.out.println("First prime: " + primeNumbers[0]);
Strings
A String in Java is an object that represents a sequence of characters. Strings in Java are immutable (cannot be modified after creation).
1
2
3
4
String s = "Hello";
String joined = s + " World"; // Creates a new String object
System.out.println("Length: " + joined.length());
System.out.println("Char at index 1: " + joined.charAt(1)); // 'e'
6. Loops
For Loops
Use a for loop when you know the number of iterations in advance.
1
2
3
for (int i = 0; i < 5; i++) {
System.out.println("Iteration: " + i);
}
While Loops
Use a while loop when the number of iterations is determined by a condition.
1
2
3
4
5
int count = 5;
while (count > 0) {
System.out.println("Count: " + count);
count--;
}
7. Functions (Pass by Value vs Pass by Reference)
This is a critical topic in Java and is frequently tested in interviews.
Rule: Java is strictly Pass-by-Value.
However, what is passed depends on the data type:
A. Primitives (Passed by Value)
When you pass a primitive variable to a method, a copy of the actual value is passed. Changes inside the method do not affect the original variable.
1
2
3
4
5
6
7
8
9
10
11
public class PrimitiveExample {
public static void modifyValue(int num) {
num = 100; // Only changes local copy
}
public static void main(String[] args) {
int x = 10;
modifyValue(x);
System.out.println("x is still: " + x); // Output: 10
}
}
B. Objects & Arrays (References Passed by Value)
When you pass an object or an array, Java passes a copy of the reference (memory address). Both the original variable and the parameter point to the same object in memory.
- Modifying the object’s properties or array indices will affect the original.
- Reassigning the parameter to a new object inside the method will not affect the original reference.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Arrays;
public class ReferenceExample {
public static void modifyArray(int[] arr) {
arr[0] = 999; // Modifies the actual object heap memory
}
public static void reassignArray(int[] arr) {
arr = new int[]{1, 2, 3}; // Points parameter to a new address; original is untouched
}
public static void main(String[] args) {
int[] myArr = {10, 20, 30};
modifyArray(myArr);
System.out.println(Arrays.toString(myArr)); // Output: [999, 20, 30]
reassignArray(myArr);
System.out.println(Arrays.toString(myArr)); // Output: [999, 20, 30]
}
}
🏁 Day 1 Action Items
To practice these concepts, here are the problems I’m coding:
- Find Factorial: Write a function using a loop.
- Reverse a String: Construct a new string by reading character by character backward.
- Swap Two Variables: Write a helper function (and think about how reference types allow you to modify array elements to simulate swapping).
Stay tuned for Day 2, where I’ll explore Time Complexity Analysis (Big O notation) and dive deep into Arrays!