Skip to content

Lesson 2: String Handling and Arrays

Overview:

In Lesson 2, we delve into the intricacies of string handling and arrays in Java. Strings are fundamental data types, and arrays provide a convenient way to work with collections of elements. Understanding how to manipulate strings and work with arrays is crucial for a variety of Java applications.

Key Concepts:

  1. String Handling:
    • String Class:
      • Definition: The String class in java.lang represents a sequence of characters.
      • Common Operations:
        • Concatenation: Combining two strings using the + operator.
        • Length: Getting the length of a string using the length() method.
        • Substrings: Extracting a portion of a string using the substring() method.
        • Equality: Comparing strings using the equals() method.
    • StringBuilder Class:
      • Definition: The StringBuilder class in java.lang is a mutable sequence of characters, providing a more efficient way to manipulate strings when concatenation is required.
// Example of String handling
String greeting = "Hello, ";
String name = "Java";
String message = greeting + name; // Concatenation
int length = message.length();    // Length
String substring = message.substring(0, 5);  // Substring
boolean isEqual = greeting.equals("Hello");   // Equality

// Example of StringBuilder
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Hello, ");
stringBuilder.append("Java");
String result = stringBuilder.toString();

2. Working with Arrays:

  • Definition: An array is a data structure that stores a fixed-size sequential collection of elements of the same type.
  • Common Operations:
    • Declaration: Specifying the type and size of an array.
    • Initialization: Assigning values to array elements.
    • Accessing Elements: Retrieving values using index notation.
    • Iterating: Traversing through array elements using loops.
// Example of working with arrays
int[] numbers = new int[5];  // Declaration and initialization
numbers[0] = 1;              // Assigning values
numbers[1] = 2;
numbers[2] = 3;

int element = numbers[1];    // Accessing elements

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

Example:

Let’s create a program that demonstrates string handling and arrays. We’ll manipulate strings and perform common operations on an array of numbers.

public class StringAndArrayExample {

    public static void main(String[] args) {
        // String handling example
        String greeting = "Hello, ";
        String name = "Java";
        String message = greeting + name; // Concatenation
        int length = message.length();    // Length
        String substring = message.substring(0, 5);  // Substring
        boolean isEqual = greeting.equals("Hello");   // Equality

        System.out.println("String Handling Example:");
        System.out.println("Message: " + message);
        System.out.println("Length: " + length);
        System.out.println("Substring: " + substring);
        System.out.println("Equality: " + isEqual);

        // Working with arrays example
        int[] numbers = new int[5];  // Declaration and initialization
        numbers[0] = 1;              // Assigning values
        numbers[1] = 2;
        numbers[2] = 3;

        int element = numbers[1];    // Accessing elements

        System.out.println("\nWorking with Arrays Example:");
        System.out.println("Element at index 1: " + element);

        // Iterating through array elements
        System.out.println("Array elements:");
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
        }
    }
}

In this example, we manipulate strings and perform common array operations. This includes concatenating strings, finding the length and substring of a string, and working with arrays by assigning values, accessing elements, and iterating through the array. Understanding these concepts is essential for handling data in Java applications.