Java > Core Java > Variables and Data Types > Reference Data Types (String, Arrays, Objects)
String Manipulation and Array Access in Java
This code snippet demonstrates the use of String (a reference data type) and arrays in Java, showcasing how to manipulate strings and access array elements.
Code Example
This code creates a String variable, converts it to uppercase, and then creates an integer array, accesses its elements, modifies an element, and prints the length of the array. Finally, it also shows how to use a custom Object (Person class) and access its members.
public class ReferenceDataTypes {
public static void main(String[] args) {
// String example
String message = "Hello, World!";
System.out.println("Original message: " + message);
// String manipulation
String upperCaseMessage = message.toUpperCase();
System.out.println("Uppercase message: " + upperCaseMessage);
// Array example
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("First element of the array: " + numbers[0]);
// Accessing array length
System.out.println("Length of the array: " + numbers.length);
// Modifying an array element
numbers[2] = 10;
System.out.println("Modified third element: " + numbers[2]);
//Object example
Person person = new Person("Alice", 30);
System.out.println("Person's name: " + person.getName());
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Concepts Behind the Snippet
This snippet demonstrates fundamental concepts:
Real-Life Use Case
Consider a scenario where you're processing user input from a form. You might use Strings to store the user's name, email, and address. You might use arrays to store a list of items they've selected from a shopping cart. Objects would be used to represent complex entities like users, products, or orders.
Best Practices
Interview Tip
Be prepared to discuss the difference between primitive data types (like `int`, `boolean`) and reference data types (like `String`, arrays, objects). Understand how memory is managed for each type (stack vs. heap).
When to Use Them
Memory Footprint
Strings and arrays, being reference types, are stored on the heap. The variable holding the String or array contains a reference (memory address) to the actual data on the heap. Objects are also stored in heap memory. The size of an object depends on the number and type of its fields.
Alternatives
Pros
Cons
FAQ
-
What happens if I try to access an array element with an index that is out of bounds?
You will get an `ArrayIndexOutOfBoundsException` at runtime. -
Are Strings mutable or immutable in Java?
Strings are immutable in Java. Once a String object is created, its value cannot be changed. -
What is the difference between `String`, `StringBuilder`, and `StringBuffer`?
`String` is immutable, while `StringBuilder` and `StringBuffer` are mutable. `StringBuffer` is thread-safe, making it suitable for multi-threaded environments, whereas `StringBuilder` is not thread-safe but generally faster.