Arrays and strings are among the most frequently used data structures in Java. Arrays allow storing multiple values of the same type in a single variable, while strings are used to handle text data. Java provides powerful built-in classes such as String, StringBuilder, and StringBuffer to perform efficient text manipulation. Understanding these concepts is essential for real-world Java programming, problem solving, and interviews.
A single-dimensional array is a collection of elements of the same data type stored in contiguous memory locations. Each element can be accessed using an index, starting from 0.
dataType[] arrayName = new dataType[size];
int[] marks = new int[5];
marks[0] = 85;
marks[1] = 90;
marks[2] = 78;
marks[3] = 88;
marks[4] = 92;
System.out.println(marks[2]);
The array marks stores 5 integer values. The value at index 2 is printed.
A multi-dimensional array is an array of arrays. The most commonly used type is the two-dimensional array, which is represented in rows and columns like a table or matrix.
dataType[][] arrayName = new dataType[rows][columns];
int[][] matrix = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(matrix[1][2]);
The value at row index 1 and column index 2 is printed, which is 6.
Java provides the Arrays class in java.util package to perform common operations.
int[] numbers = {10, 20, 30, 40};
for (int num : numbers) {
System.out.println(num);
}
import java.util.Arrays;
int[] nums = {5, 2, 8, 1};
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));
int index = Arrays.binarySearch(nums, 8);
System.out.println(index);
int[] copy = Arrays.copyOf(nums, nums.length);
| Method | Description |
|---|---|
sort() | Sorts array |
binarySearch() | Searches element |
copyOf() | Copies array |
equals() | Compares arrays |
toString() | Prints array |
The String class is used to represent a sequence of characters. In Java, strings are immutable, meaning once created, their values cannot be changed.
equals() for comparisonString name = "Java";
name = name.concat(" Programming");
System.out.println(name);
String text = "Java Programming";
| Method | Example | Result |
|---|---|---|
length() | text.length() | Length |
toUpperCase() | text.toUpperCase() | Uppercase |
toLowerCase() | text.toLowerCase() | Lowercase |
charAt() | text.charAt(2) | Character |
substring() | text.substring(5) | Substring |
contains() | text.contains("Java") | true/false |
replace() | text.replace("Java", "Core Java") | Replaced |
StringBuilder is used to create mutable strings. It is faster than String because it does not create new objects during modification.
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb);
StringBuffer is similar to StringBuilder but is thread-safe.
StringBuffer sbf = new StringBuffer("Java");
sbf.append(" Course");
System.out.println(sbf);
| Feature | String | StringBuilder | StringBuffer |
|---|---|---|---|
| Mutability | Immutable | Mutable | Mutable |
| Thread Safety | Yes | No | Yes |
| Performance | Slow | Fast | Medium |
| Use Case | Fixed text | Single thread | Multi-thread |
String manipulation refers to operations performed on strings to modify, analyze, or extract data.
String input = "Java";
StringBuilder rev = new StringBuilder(input);
System.out.println(rev.reverse());
String s = "Programming";
System.out.println(s.length());
String str = "madam";
String reversed = new StringBuilder(str).reverse().toString();
if (str.equals(reversed)) {
System.out.println("Palindrome");
} else {
System.out.println("Not Palindrome");
}
String sentence = "Java is powerful";
System.out.println(sentence.replace(" ", ""));
java
public class ArraysAndStrings {
public static void main(String[] args) {
// Arrays
int[] numbers = {10, 20, 30, 40, 50};
System.out.println("First element: " + numbers[0]);
// Loop through array
for (int num : numbers) {
System.out.print(num + " ");
}
System.out.println();
// 2D Array
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println("Element at [1][2]: " + matrix[1][2]);
// Strings
String text = "Java Programming";
System.out.println("Length: " + text.length());
System.out.println("Uppercase: " + text.toUpperCase());
System.out.println("Substring: " + text.substring(0, 4));
System.out.println("Contains 'Java': " + text.contains("Java"));
// StringBuilder for efficient string manipulation
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
sb.insert(5, ",");
System.out.println(sb.toString());
}
}