Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

Java Strings

A string is a piece of text: a name, a sentence, a line read from a file. In Java, text has the type String. You have printed strings since the first topic; now you will learn to measure them, join them, take them apart and compare them.

Creating and joining strings

A string is written between double quotes. The + operator joins (concatenates) strings, and it also turns numbers into text when one side is a string:

public class Main {
    public static void main(String[] args) {
        String first = "Grace";
        String last = "Hopper";
        String full = first + " " + last;
        System.out.println(full);
        System.out.println("Born in " + 1906);
        System.out.println("Sum: " + 2 + 3);
        System.out.println("Sum: " + (2 + 3));
    }
}
Output
Grace Hopper
Born in 1906
Sum: 23
Sum: 5

Look at the last two lines. Java works from left to right: "Sum: " + 2 is already text, so + 3 adds the character 3 instead of adding numbers. Brackets make Java add the numbers first.

Length and characters

length() gives the number of characters. charAt(i) gives the character at position i. Positions start at 0, so the last character is at length() - 1:

public class Main {
    public static void main(String[] args) {
        String word = "Kolkata";
        System.out.println(word.length());
        System.out.println(word.charAt(0));
        System.out.println(word.charAt(word.length() - 1));
    }
}
Output
7
K
a

Asking for a position that does not exist, such as charAt(7) here, stops the program with a StringIndexOutOfBoundsException.

Comparing strings

To check whether two strings contain the same text, use equals. equalsIgnoreCase ignores the difference between capital and small letters. Do not use == for text: it checks whether both variables point to the very same object, which can be false even when the text is the same.

public class Main {
    public static void main(String[] args) {
        String a = "java";
        String b = new String("java");
        System.out.println(a.equals(b));
        System.out.println(a == b);
        System.out.println("JAVA".equalsIgnoreCase(a));
        System.out.println("apple".compareTo("banana") < 0);
    }
}
Output
true
false
true
true

compareTo compares in alphabetical order: it gives a negative number when the first string comes first, zero when they are equal, and a positive number otherwise.

Strings never change

A String object is immutable: its text can never change. Methods such as toUpperCase do not change the string; they return a new string. To keep the result, store it:

public class Main {
    public static void main(String[] args) {
        String city = "delhi";
        city.toUpperCase();
        System.out.println(city);
        city = city.toUpperCase();
        System.out.println(city);
    }
}
Output
delhi
DELHI

The first call creates "DELHI" and throws it away. When you build a long text piece by piece in a loop, use a StringBuilder instead; see the StringBuilder topic. The next topic, String Methods, shows the many useful methods strings have.

Practice exercises

  1. Store your first and last name in two variables and print the full name and its length.
  2. Print the first and the last character of the word "Bengaluru".
  3. Compare "Hello" and "hello" with equals and with equalsIgnoreCase and print both results.