Spex3
    

Core Java Concepts: Arrays and Strings


    

    

Core Java Concepts: Arrays and Strings
1. Arrays in Java
An array is a collection of elements of the same type, stored in contiguous memory locations.

1.1 Declaring and Initializing Arrays


// Declare and initialize an array
int[] numbers = {10, 20, 30, 40, 50};

// Access elements
System.out.println(numbers[0]); // Output: 10
System.out.println(numbers[2]); // Output: 30


1.2 Creating an Array with Fixed Size

int[] arr = new int[5]; // Array of size 5

// Assigning values
arr[0] = 5;
arr[1] = 10;
arr[2] = 15;

System.out.println(arr[1]); // Output: 10


1.3 Looping Through an Array
✅ Using a for loop


int[] numbers = {1, 2, 3, 4, 5};

for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}


✅ Using a foreach loop


for (int num : numbers) {
System.out.println(num);
}


2. Multi-Dimensional Arrays
A multi-dimensional array is an array of arrays.

2.1 Declaring and Initializing a 2D Array

int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

// Accessing elements
System.out.println(matrix[0][1]); // Output: 2
System.out.println(matrix[2][2]);

// Output: 9



2.2 Looping Through a 2D Array

for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println(); // New line
}


💡 Output:


1 2 3
4 5 6
7 8 9

3. Strings in Java
A String in Java is an immutable sequence of characters.

3.1 Declaring Strings

String str1 = "Hello"; // String literal
String str2 = new String("World"); // Using new keyword


3.2 Common String Methods
Method Description Example
length() Returns the string length "Java".length() → 4
charAt(index) Returns the character at the given index "Java".charAt(2) → 'v'
toUpperCase() Converts to uppercase "java".toUpperCase() → "JAVA"
toLowerCase() Converts to lowercase "JAVA".toLowerCase() → "java"
substring(start, end) Extracts a substring "Hello".substring(1,4) → "ell"
indexOf(str) Returns the index of a character or substring "Hello".indexOf("l") → 2
replace(old, new) Replaces characters or substrings "Java".replace("a", "o") → "Jovo"
trim() Removes leading and trailing spaces " Java ".trim() → "Java"

✅ Example of String Manipulation


public class StringExample {
public static void main(String[] args) {
String s = " Hello Java ";
System.out.println("Original: " + s);
System.out.println("Trimmed: " + s.trim());
System.out.println("Uppercase: " + s.toUpperCase());
System.out.println("Substring: " + s.substring(1, 6));
}
}


💡 Output:


Original: Hello Java
Trimmed: Hello Java
Uppercase: HELLO JAVA
Substring: ello

4. StringBuilder and StringBuffer
4.1 Why Use StringBuilder or StringBuffer?
Strings are immutable: Modifying a String creates a new object.

StringBuilder and StringBuffer are mutable, meaning they allow modifications without creating new objects.

4.2 Differences Between StringBuilder and StringBuffer
Feature StringBuffer StringBuilder
Thread Safety Yes (synchronized) No (faster but not thread-safe)
Performance Slower (due to thread safety) Faster
4.3 Using StringBuilder


public class StringBuilderExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb); // Output: Hello World

sb.insert(5, " Java");
System.out.println(sb); // Output: Hello Java World

sb.replace(6, 10, "C++");
System.out.println(sb); // Output: Hello C++ World

sb.delete(6, 10);
System.out.println(sb); // Output: Hello World

sb.reverse();
System.out.println(sb); // Output: dlroW olleH
}
}


4.4 Using StringBuffer (Thread-Safe Alternative)

public class StringBufferExample {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("Java");
sb.append(" Programming");
System.out.println(sb); // Output: Java Programming
}
}


    Date: 2025-03-28 00:00:00.000000