home
0%
Progress
CBSE Code 802 | Unit 3: Java Programming

04. Arrays & String Methods in Java

Array indexing, String immutability, length, charAt, substring, equals vs ==, indexOf.

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 index length - 1.
  • Length is fixed upon creation and accessed via the .length property (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)

Entity Details

Create New Item

help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.