Java Strings

A Java string is a sequence of characters, such as a name, message, or sentence. Strings are written inside double quotes and stored with the String type.

Java treats strings as objects, which means they have useful methods for working with text.

Create A String

The most common way to create a string is to assign quoted text to a String variable.

public class Main {
    public static void main(String[] args) {
        String name = "Maya";
        String language = "Java";
        int lessons = 3;

        System.out.println("Name: " + name);
        System.out.println("Course: " + language);
        System.out.println(name + " is learning " + language + ".");
        System.out.println("Lessons completed: " + lessons);
    }
}

Output:

Name: Maya
Course: Java
Maya is learning Java.
Lessons completed: 3

Joining Strings

The + operator joins strings together. This is called concatenation.

If one side of + is a string, Java converts the other value to text and joins the result. That is why "Lessons completed: " + lessons prints the number as part of the message.

Common String Methods

Because String values are objects, you can call methods on them with dot syntax.

Method What it does
length() Returns the number of characters in the string.
charAt(index) Returns the character at a position.
toUpperCase() Returns an uppercase version of the string.
toLowerCase() Returns a lowercase version of the string.
equals(other) Checks whether two strings contain the same text.
public class Main {
    public static void main(String[] args) {
        String word = "Java";
        String sameWord = "Java";
        String differentCase = "java";

        System.out.println("Length: " + word.length());
        System.out.println("First character: " + word.charAt(0));
        System.out.println("Uppercase: " + word.toUpperCase());
        System.out.println("Same text: " + word.equals(sameWord));
        System.out.println("Same case: " + word.equals(differentCase));
    }
}

Output:

Length: 4
First character: J
Uppercase: JAVA
Same text: true
Same case: false

String Indexes Start At 0

In Java, character positions start at 0, not 1. In the string "Java", charAt(0) is 'J', charAt(1) is 'a', and charAt(3) is 'a'.

Using an index outside the string causes an error when the program runs. For example, "Java" has four characters, so the last valid index is 3.

Compare Strings With equals()

Use equals() to compare the text inside strings. Do not rely on == for text comparison, because == checks whether two variables refer to the same object.

String comparison is case-sensitive. "Java" and "java" are different because J and j are different characters.

Strings Cannot Be Changed

Java strings are immutable, which means the text inside a string object cannot be changed after it is created. Methods like toUpperCase() return a new string; they do not edit the original string.

If you want to keep the changed value, assign the result to a variable, such as String loud = word.toUpperCase();.

Takeaway: use String for text, join values with +, and use string methods like length(), charAt(), and equals() to inspect and compare text.