Java Strings

A String in Java is used to store a sequence of characters, such as a name, word, sentence, or message. Strings are one of the most commonly used types in Java programs.

Unlike int, double, char, and boolean, String is not a primitive data type. It is a class provided by Java in the java.lang package, so you can use it without importing anything.

A String is written inside double quotation marks.

String name = “John”;

Here, name stores the text "John".

Creating Strings in Java

You can create a String by assigning text to a String variable.

public class Main {
    public static void main(String[] args) {
        String name = "John";
        String city = "Hyderabad";
        String message = "Welcome to Java";

        System.out.println(name);
        System.out.println(city);
        System.out.println(message);
    }
}

Output:

John
Hyderabad
Welcome to Java

A String can contain letters, numbers, spaces, and special characters.

String mobile = “9876543210”;

Here, the number is stored as text, not as an integer.

String and char

A char stores a single character, while a String can store multiple characters.

char letter = ‘A’;

String word = “Java”;

Notice that char uses single quotes, while String uses double quotes.

TypeStoresExample
charOne characterchar grade = ‘A’;
StringMultiple charactersString name = “John”;

String Concatenation

You can combine two or more Strings using the + operator. This process is called string concatenation.

public class Main {
    public static void main(String[] args) {
        String firstName = "John";
        String lastName = "Smith";

        String fullName = firstName + " " + lastName;

        System.out.println(fullName);
    }
}

Output:

John Smith

You can also combine Strings with numbers.

int age = 25;
String name = "John";

System.out.println(name + " is " + age + " years old.");

Output:

John is 25 years old.

When a String is combined with a number using +, the result becomes a String.

Finding the Length of a String

The length() method returns the number of characters in a String.

String text = “Java”;

text.length(); returns 4.

For example:

String name = "Karim";

System.out.println(name.length());

Output:

5

Spaces are also counted as characters.

Converting String Case

Java provides methods for converting text to uppercase or lowercase.

String text = “Java Programming”;

text.toUpperCase(); converts the text to uppercase.

text.toLowerCase(); converts the text to lowercase.

Example:

String text = "Java Programming";

System.out.println(text.toUpperCase());
System.out.println(text.toLowerCase());

Output:

JAVA PROGRAMMING
java programming

Accessing Characters in a String

The charAt() method is used to access a character at a specific index.

Java String indexing starts from 0.

For example, in "Java":

IndexCharacter
0J
1a
2v
3a

You can access a character using:

String text = “Java”;

text.charAt(0); returns J.

Example:

String text = "Java";

System.out.println(text.charAt(0));
System.out.println(text.charAt(2));

Output:

J
v

Comparing Strings

The equals() method is commonly used to compare the contents of two Strings.

String first = "Java";
String second = "Java";

System.out.println(first.equals(second));

Output:

true

You can also use equalsIgnoreCase() when you want to ignore differences in uppercase and lowercase.

first.equalsIgnoreCase(second);

This is useful when comparing user input where capitalization may vary.

Checking Text with contains()

The contains() method checks whether a String contains a particular sequence of characters.

String message = "Welcome to Java";

System.out.println(message.contains("Java"));

Output:

true

If the specified text is not found, it returns false.

Extracting Part of a String

The substring() method can be used to extract part of a String.

String text = "Java Programming";

System.out.println(text.substring(0, 4));

Output:

Java

The starting index is included, while the ending index is excluded.

Replacing Text

The replace() method can replace characters or sequences of characters.

String text = "I like Python";

String result = text.replace("Python", "Java");

System.out.println(result);

Output:

I like Java

Removing Extra Spaces

The trim() method removes leading and trailing whitespace from a String.

String text = "   Java   ";

System.out.println(text.trim());

Output:

Java

For newer Java code, methods such as strip() can also be used for Unicode-aware whitespace handling.

String Immutability

Strings in Java are immutable. This means that once a String object is created, its contents cannot be changed.

For example:

String text = "Hello";

text = text + " Java";

System.out.println(text);

The original String "Hello" is not modified. Java creates a new String containing "Hello Java" and assigns it to text.

String immutability makes Strings safer to use and allows Java to optimize String handling internally.

String Methods

Java provides many useful methods for working with Strings.

MethodPurpose
length()Returns the number of characters
charAt()Returns a character at an index
equals()Compares String contents
equalsIgnoreCase()Compares Strings ignoring case
toUpperCase()Converts text to uppercase
toLowerCase()Converts text to lowercase
contains()Checks whether text exists
substring()Extracts part of a String
replace()Replaces text
trim()Removes leading and trailing whitespace

String vs StringBuilder

For normal text storage and manipulation, String is usually sufficient. However, when a program repeatedly modifies text, StringBuilder can be more efficient because it is designed for mutable sequences of characters.

For example:

StringBuilder message = new StringBuilder("Hello");

message.append(" Java");

System.out.println(message);

Output:

Hello Java