Arrays and Strings in Java

Chapter 6: Arrays and Strings in Java

Chapter 6: Arrays and Strings in Java

Welcome to Chapter 6 of our Java Programming Course! In this chapter, we'll explore two of the most fundamental data structures in Java: Arrays and Strings.

🔹 What is an Array in Java?

An Array is a collection of elements, all of the same type, stored in contiguous memory locations. It allows you to store multiple values in a single variable.

👉 Declaration and Initialization

int[] numbers = new int[5]; // Declaration
numbers[0] = 10;
numbers[1] = 20;
// ... up to index 4

👉 Shortcut Initialization

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

👉 Accessing Array Elements

System.out.println(numbers[2]); // Output: 30

👉 Array Length

System.out.println("Length: " + numbers.length);

🔹 Multidimensional Arrays

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

🔹 Looping Through Arrays

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

🔹 Enhanced For Loop

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

🔹 What is a String in Java?

A String is a sequence of characters enclosed in double quotes. In Java, strings are objects of the String class.

👉 Declaration and Initialization

String message = "Hello, World!";

👉 String Concatenation

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

👉 Common String Methods

System.out.println("Length: " + message.length());
System.out.println("UpperCase: " + message.toUpperCase());
System.out.println("LowerCase: " + message.toLowerCase());
System.out.println("Contains Hello? " + message.contains("Hello"));
System.out.println("Substring: " + message.substring(0, 5));

👉 Comparing Strings

String a = "Hello";
String b = "Hello";
System.out.println(a.equals(b)); // true

👉 StringBuilder for Performance

StringBuilder sb = new StringBuilder();
sb.append("Java");
sb.append(" Programming");
System.out.println(sb.toString());

💡 Key Differences: Arrays vs Strings

  • Arrays store multiple values of the same data type; Strings store sequences of characters.
  • Arrays are fixed in size; Strings are immutable and can be manipulated using built-in methods.

✅ Summary

  • Arrays are used to store multiple values.
  • Strings are objects used to handle text.
  • Use loops and methods to manipulate arrays and strings effectively.

❓ FAQs with Answers

Q1: What is the default value of an array in Java?

A: It depends on the data type. For example, int[] defaults to 0, boolean[] to false, and String[] to null.

Q2: Can we increase array size in Java after declaration?

A: No, array size is fixed. You need to create a new array and copy data manually or use collections like ArrayList.

Q3: What is the difference between == and .equals() for Strings?

A: == checks reference equality (memory location), while .equals() checks content equality.

Q4: What is String immutability in Java?

A: Once a String object is created, it cannot be changed. Any modification results in a new String object.

Q5: How is StringBuilder different from String?

A: StringBuilder is mutable, meaning its contents can be modified without creating new objects, which improves performance.


📌 Related Links: Complete Java Course

📌 Labels

Java, Java Programming, Arrays in Java, Strings in Java, Java Tutorial for Beginners

Comments

Popular posts from this blog

Best AI Tools for Students and Professionals in 2025 | Boost Learning & Productivity

Introduction to Java

Top AI Tools for Content Creators: Bloggers, YouTubers & Writers (2025)