Skip to content

πŸ“¦ Arrays and Strings in Java – Handling Collections of Data

πŸ”– Introduction

Real-world applications often deal with multiple values – like a list of names, scores, or dates. Java provides powerful tools to handle these:

  • Arrays – fixed-size collections of the same type.
  • Strings – sequences of characters (like words or sentences).

Let’s dive into both with simple examples and tips!

πŸ”’ Java Arrays – Store Multiple Values


πŸ“Œ What is an Array?

An array is a container object that holds a fixed number of values of the same type.


βœ… Declaring and Initializing Arrays

int[] numbers = new int[5]; // creates an array with 5 elements

Or directly initialize:

int[] numbers = {10, 20, 30, 40, 50};

πŸ” Accessing Array Elements

Arrays are zero-indexed: the first element is at index 0.

System.out.println(numbers[0]); // prints 10
numbers[1] = 25; // updates second value

πŸ” Looping Through Arrays

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

πŸ’‘ Enhanced For-Loop (For-Each)

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

πŸ“Œ Common Array Example: Find Maximum

int[] nums = {3, 7, 2, 9, 4};
int max = nums[0];

for (int n : nums) {
    if (n > max) max = n;
}
System.out.println("Max: " + max);

✨ Java Strings – Working with Text

πŸ“Œ What is a String?

A String in Java is an object that represents a sequence of characters.

String name = "Raaz";

Java stores Strings as immutable objects β€” their values cannot be changed after creation.


πŸ§ͺ Common String Methods

String s = "Hello World";

System.out.println(s.length());           // 11
System.out.println(s.toUpperCase());      // HELLO WORLD
System.out.println(s.toLowerCase());      // hello world
System.out.println(s.charAt(0));          // H
System.out.println(s.substring(6));       // World
System.out.println(s.contains("World"));  // true
System.out.println(s.equals("hello"));    // false

πŸ”€ Concatenation

String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName); // John Doe

πŸ”„ Converting Between String and Numbers

String str = "100";
int num = Integer.parseInt(str); // string β†’ int

int age = 25;
String ageStr = String.valueOf(age); // int β†’ string

πŸ§ͺ Practice Challenge

Create a program that counts how many vowels are in a given string:

public class VowelCounter {
    public static void main(String[] args) {
        String text = "Hello Java World!";
        int count = 0;

        for (char c : text.toLowerCase().toCharArray()) {
            if ("aeiou".indexOf(c) != -1) {
                count++;
            }
        }

        System.out.println("Vowel count: " + count);
    }
}

βœ… What You Learned

  • Arrays are used to store multiple values of the same type
  • Looping through arrays and finding data
  • Strings are powerful and immutable
  • Common string methods and conversions

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *