Unit 3: Java Programming
Chapter 4: Arrays and String Methods in Java
1. Arrays in Java
An array is an indexed collection of fixed-size homogeneous elements (elements of the same data type) stored in contiguous memory locations.
Array Characteristics
- Zero-indexed: First element is at index
0, last element at indexlength - 1. - Length is fixed upon creation and accessed via the
.lengthproperty (without parentheses).
Declaration, Instantiation, and Initialization
// Method 1: Declare and allocate size
int[] marks = new int[5];
marks[0] = 85;
marks[1] = 92;
// Method 2: Declare and initialize with values directly
int[] numbers = {10, 20, 30, 40, 50};
// Finding array length
System.out.println("Array Size: " + numbers.length); // 5
// Traversing using enhanced for loop (for-each)
for (int num : numbers) {
System.out.println(num);
}
2. Strings in Java
In Java, strings are objects belonging to the java.lang.String class.
- Immutability: Strings in Java are immutable. Once a String object is created, its sequence of characters cannot be altered. Any modification returns a new String object.
3. High-Yield String Methods for Board Exams
| Method Signature | Description | Example (String s = "Computer";) |
Output |
|---|---|---|---|
int length() |
Total character count | s.length() |
8 |
char charAt(int index) |
Character at specific index | s.charAt(3) |
'p' |
String toUpperCase() |
Converts all letters to uppercase | s.toUpperCase() |
"COMPUTER" |
String toLowerCase() |
Converts all letters to lowercase | s.toLowerCase() |
"computer" |
String substring(int beginIndex) |
Extract substring from start index | s.substring(4) |
"uter" |
String substring(int begin, int end) |
Extract slice [begin, end) |
s.substring(0, 4) |
"Comp" |
int indexOf(char ch) |
First occurrence index of char | s.indexOf('t') |
5 |
String concat(String str) |
Appends given string to end | s.concat(" IT") |
"Computer IT" |
String trim() |
Removes leading and trailing spaces | " Hi ".trim() |
"Hi" |
String replace(char old, char new) |
Replaces character occurrences | s.replace('e', 'a') |
"Computar" |
boolean equals(String another) |
Checks equality of string contents | s.equals("Computer") |
true |
boolean equalsIgnoreCase(String str) |
Content equality ignoring case | s.equalsIgnoreCase("computer") |
true |
int compareTo(String another) |
Lexicographical comparison | "A".compareTo("B") |
-1 |
4. == vs. equals() Comparison
Important
==tests reference equality (whether both variables point to the exact same memory address)..equals()tests value/content equality (whether the sequences of characters inside the strings are identical).
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2); // FALSE (different heap objects)
System.out.println(s1.equals(s2)); // TRUE (identical character content)
5. Board Exam Practice & Output Questions
Given:
String s = "Information Technology";
Find the result of:
1. s.length() $ o$ 22 (including the space)
2. s.substring(12) $ o$ "Technology"
3. s.substring(0, 4) $ o$ "Info"
4. s.indexOf('o') $ o$ 3 (first occurrence in "Information")
5. s.charAt(7) $ o$ 't' (0:I, 1:n, 2:f, 3:o, 4:r, 5:m, 6:a, 7:t)