Arrays and Strings in Java: Understanding the Basics
Introduction#
So far, we have seen how to store values using variables.
For example:
int age = 20;
String name = "Ravi";
But what if we need to store multiple values of the same type?
For example, suppose we want to store the marks of five students.
We could create five variables:
int mark1 = 80;
int mark2 = 75;
int mark3 = 90;
int mark4 = 85;
int mark5 = 88;
This works, but it becomes difficult to manage when we have many values.
Java provides arrays to store multiple values of the same type.
For text and words, Java provides the String class.
Arrays#
An array is a collection of values of the same data type stored under one variable name.
For example:
int[] marks = {80, 75, 90, 85, 88};
Here, marks stores five integer values.
Instead of creating separate variables, we can store all the values in one array.
Creating an Array#
There are different ways to create an array.
Creating and Initializing an Array#
int[] numbers = {10, 20, 30, 40, 50};
The array contains five values.
Creating an Empty Array#
We can also specify the size first:
int[] numbers = new int[5];
This creates an integer array that can store five values.
We can then assign values:
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
Array Index#
Each element in an array has an index.
In Java, array indexing starts from 0.
For example:
Index: 0 1 2 3 4
↓ ↓ ↓ ↓ ↓
Values: 10 20 30 40 50
So:
numbers[0]
gives:
10
and:
numbers[4]
gives:
50
The last index is always:
array length - 1
Accessing Array Elements#
We can access an element using its index.
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers[0]);
System.out.println(numbers[2]);
System.out.println(numbers[4]);
Output
10
30
50
Changing an Array Element#
Array elements can be changed using their index.
int[] numbers = {10, 20, 30};
numbers[1] = 50;
System.out.println(numbers[1]);
Output
50
The original value 20 was replaced with 50.
Finding the Size of an Array#
We can use the length property to find the size of an array.
int[] numbers = {10, 20, 30, 40, 50};
System.out.println(numbers.length);
Output
5
Notice that we use:
numbers.length
not:
numbers.length()
length is a property of an array, not a method.
Traversing an Array#
We can use a loop to access all the elements of an array.
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
Output
10
20
30
40
50
Here, the variable i represents the index of the array.
Strings#
A String is used to store text.
For example:
String name = "Ravi";
Other examples:
String city = "Vijayawada";
String message = "Welcome to Java";
Unlike primitive data types such as int and double, String is a class in Java.
Creating a String#
The most common way is:
String name = "Ravi";
We can also create a String using the new keyword:
String name = new String("Ravi");
For beginners, the first form is the most commonly used:
String name = "Ravi";
Joining Strings#
We can join strings using the + operator.
String firstName = "Ravi";
String lastName = "Kumar";
String fullName = firstName + " " + lastName;
System.out.println(fullName);
Output
Ravi Kumar
This is called string concatenation.
Finding the Length of a String#
We can use the length() method to find the number of characters in a String.
String name = "Ravi";
System.out.println(name.length());
Output
4
Here we use:
name.length()
because length() is a method of the String class.
Remember:
Array → length
String → length()
Accessing Characters in a String#
We can use the charAt() method to access a character at a particular index.
String name = "Ravi";
System.out.println(name.charAt(0));
System.out.println(name.charAt(2));
Output
R
v
Just like arrays, String indexing starts from 0.
Index: 0 1 2 3
↓ ↓ ↓ ↓
String: R a v i
Comparing Strings#
We can compare the contents of two Strings using the equals() method.
String name1 = "Ravi";
String name2 = "Ravi";
System.out.println(name1.equals(name2));
Output
true
If the contents are different:
String name1 = "Ravi";
String name2 = "Priya";
System.out.println(name1.equals(name2));
Output
false
For comparing String contents, use:
name1.equals(name2)
rather than using ==.
Useful String Methods#
Some commonly used String methods are:
| Method | Purpose |
|---|---|
length() |
Returns the number of characters |
charAt(index) |
Returns the character at an index |
equals() |
Compares String contents |
toUpperCase() |
Converts to uppercase |
toLowerCase() |
Converts to lowercase |
Example:
String name = "Ravi";
System.out.println(name.toUpperCase());
System.out.println(name.toLowerCase());
Output
RAVI
ravi
Arrays vs Strings#
Arrays and Strings are different, even though both can contain multiple values or characters.
| Feature | Array | String |
|---|---|---|
| Used for | Multiple values | Text |
| Example | int[] numbers |
String name |
| Indexing | Starts from 0 |
Starts from 0 |
| Size | array.length |
string.length() |
| Access element/character | array[index] |
string.charAt(index) |
Check Your Understanding#
Predict the Output
What will this program print?
int[] numbers = {10, 20, 30, 40};
System.out.println(numbers[1]);
System.out.println(numbers.length);
Find the Problem
What is wrong with this code?
String name = "Ravi";
System.out.println(name.length);
How should it be corrected?
Analyze the String
What will be the output?
String name = "Java";
System.out.println(name.charAt(1));
System.out.println(name.equals("Java"));
Think and Answer
Suppose you need to store:
- Names of 5 students
- Marks of 5 students
- The name of the school
Which should you use for each: an array or a String?
Explain your choice.