What are Strings?
A String is simply text in Java. It can be a single word, a sentence, or even an entire book. But in Java, Strings are not just plain text — they are objects. The class responsible for handling Strings is called String.
Example of creating a String:
String greeting = "Hello, world!";
Why are Strings a reference type?
When you create a String in Java, the variable (like greeting
) doesn’t store the text directly. Instead, it stores a reference to the memory location where the actual text is kept.
Think of it like saving your friend’s phone number in your contact list. The contact doesn’t store your friend — it just points you to their phone number.
Important Features of Strings
Strings are immutable (unchangeable)
Once a String is created, its content cannot be changed. If you “change” a String, Java creates a new String object, and the old one stays the same.
Example:
String text = "Hello!";
text = "Goodbye!"; // A new String object is created, "Hello!" remains unchanged in memory.
Strings are objects
Since Strings are objects, they come with useful built-in methods. These methods let you:
- Find the length of a String.
- Convert all characters to uppercase.
- Combine multiple Strings.
- And much more.
How to work with Strings?
To interact with a String, you call its methods. A method is a built-in action you can perform on an object. You call a method by adding a dot (.
) after the variable that stores the String.
Examples:
Find the length of a String
The .length()
method returns the number of characters in the String.
String text = "Welcome!";
System.out.println(text.length()); // Outputs: 8
Here:
- The
.length()
method is called on the variabletext
. - The method calculates the length of the String and returns
8
. - The result is displayed in the console using
System.out.println
.
Change the case of a String
The .toUpperCase()
method converts all letters in a String to uppercase.
String text = "Welcome!";
System.out.println(text.toUpperCase()); // Outputs: WELCOME!
Combine two Strings
The .concat()
method joins two Strings together.
String first = "Hello, ";
String second = "world!";
String result = first.concat(second);
System.out.println(result); // Outputs: Hello, world!
Get a character at a specific position
The .charAt()
method returns the character at a given index (starting from 0).
String text = "Java";
System.out.println(text.charAt(1)); // Outputs: a
Common String Methods
Here are some frequently used methods:
Method | Description | Example |
---|
length() | Returns the number of characters in a String. | "Example".length() → 7 |
toUpperCase() | Converts all characters to uppercase. | "java".toUpperCase() → "JAVA" |
toLowerCase() | Converts all characters to lowercase. | "JAVA".toLowerCase() → "java" |
concat(String s) | Combines two Strings. | "Hello, ".concat("world!") → "Hello, world!" |
charAt(int index) | Returns the character at the specified index. | "Java".charAt(2) → 'v' |
contains(String s) | Checks if a String contains another String. | "Java".contains("va") → true |
Real-Life Example
Imagine a String is a letter stored in an envelope. The variable (like greeting
) is the label on the envelope that tells you where to find the letter.
If you want to know how many words are in the letter, you open the envelope and count. Similarly, when you want to make changes, you don’t edit the original letter. Instead, you write a new one and put it in a new envelope.
Why Strings are Important?
Strings are everywhere in programming. Whether it’s user input, error messages, or text displayed to users, you’ll work with Strings constantly. Understanding how Strings work gives you a solid foundation to handle text in your programs.
Now you know that Strings in Java are not just text — they are powerful tools for managing and manipulating data!
Review Questions on “Reference Types: Strings in Java”
What is a string (String) in Java?
a) A data type for storing numbers.
b) A simple sequence of characters.
c) An object representing text.
d) A method for working with text.
Correct answer (click here):
c
Why are strings in Java considered immutable?
a) They cannot be deleted from the program.
b) Changes create a new object instead of modifying the existing one.
c) They are protected from changes by built-in methods.
d) Their length is fixed.
Correct answer (click here):
d
What does the .length()
method do, and how is it used?
a) Returns the number of characters in a string.
b) Changes the length of the string.
c) Multiplies the characters in the string.
d) Converts the string to uppercase.
Correct answer (click here):
a
What will the following code output?
Remember: in Java, character numbering starts at zero. For example, P is at position 0.
String text = "Programming";
System.out.println(text.charAt(3));
Correct answer (click here):
g
Why?
In the string "Programming"
:
- P is at position 0.
- r is at position 1.
- o is at position 2.
- g is at position 3.
When the .charAt(3)
method is called, it returns the character at position 3, which is g.
Important to remember:
If you try to access an index beyond the string’s length (e.g., text.charAt(15)
for a string with 11 characters), the program will throw an error: StringIndexOutOfBoundsException.
Indexing starts at zero: The first character of a string has index 0.