Lesson 17 of 50 – Java Strings
34%

Java Strings

A String in Java is a sequence of characters used to store text. For example, names, addresses, messages, and course names can all be stored in String variables.

1. What Is a String in Java?

Java provides the String class to represent text. A String value is written inside double quotation marks.

public class Main {
    public static void main(String[] args) {
        String name = "Rahul";
        String city = "Aurangabad";
        String course = "Java Programming";

        System.out.println(name);
        System.out.println(city);
        System.out.println(course);
    }
}
Output:
Rahul
Aurangabad
Java Programming

The String type is a reference type in Java, not a primitive data type.

2. Creating String Variables

You can declare a String variable and assign text to it.

String firstName = "Amit";
String lastName = "Kumar";

System.out.println(firstName);
System.out.println(lastName);
Output:
Amit
Kumar

You can also declare a variable before assigning a value.

String message;
message = "Welcome to Java";

System.out.println(message);
Output:
Welcome to Java
String literals use double quotes, such as "Hello". Single quotes are used for individual character literals, such as 'A'.
3. Joining Strings Using the + Operator

The + operator can join two or more strings. This operation is called string concatenation.

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

        String fullName = firstName + " " + lastName;

        System.out.println(fullName);
    }
}
Output:
Rahul Kumar

The space inside " " separates the first name and last name.

4. Joining Strings and Numbers

The + operator can also combine text with numeric values.

public class Main {
    public static void main(String[] args) {
        String name = "Aman";
        int age = 20;

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Next year age: " + (age + 1));
    }
}
Output:
Name: Aman
Age: 20
Next year age: 21
Use parentheses when you want to perform arithmetic before joining a number with text.
System.out.println("Result: " + 10 + 5);
System.out.println("Result: " + (10 + 5));
Output:
Result: 105
Result: 15
5. Finding the Length of a String

The length() method returns the number of UTF-16 code units in a String. For basic English text, this usually matches the number of characters.

public class Main {
    public static void main(String[] args) {
        String name = "Rahul";

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

        String message = "Hello Java";
        System.out.println(message.length());
    }
}
Output:
5
10

Spaces are also counted. In "Hello Java", there are five letters, one space, and four letters.

6. Changing Letter Case

Java provides methods to convert a String to uppercase or lowercase.

Method Purpose
toUpperCase() Converts text to uppercase.
toLowerCase() Converts text to lowercase.
public class Main {
    public static void main(String[] args) {
        String name = "Soopro Pathshala";

        System.out.println(name.toUpperCase());
        System.out.println(name.toLowerCase());
    }
}
Output:
SOOPRO PATHSHALA
soopro pathshala
7. Comparing Strings

Use equals() to compare the contents of two strings. String comparisons using equals() are case-sensitive.

public class Main {
    public static void main(String[] args) {
        String a = "Java";
        String b = "Java";
        String c = "java";

        System.out.println(a.equals(b));
        System.out.println(a.equals(c));
        System.out.println(a.equalsIgnoreCase(c));
    }
}
Output:
true
false
true

The equalsIgnoreCase() method compares strings without considering letter case.

Do not use == when you want to compare String contents. For content comparison, use equals(). The == operator checks whether two references refer to the same object.
8. Searching Inside a String

The contains() method checks whether a String contains a particular sequence of characters. The indexOf() method finds the first matching position.

public class Main {
    public static void main(String[] args) {
        String message = "I am learning Java";

        System.out.println(message.contains("Java"));
        System.out.println(message.contains("Python"));
        System.out.println(message.indexOf("Java"));
    }
}
Output:
true
false
13

String positions start at index 0. If a searched substring is not found, indexOf() returns -1.

9. Extracting Part of a String

The substring() method extracts a portion of a String.

public class Main {
    public static void main(String[] args) {
        String text = "Programming";

        System.out.println(text.substring(0, 7));
        System.out.println(text.substring(3));
    }
}
Output:
Program
gramming

In substring(start, end), the start index is included, but the end index is excluded.

Method Result
text.substring(0, 7) Program
text.substring(3) gramming
10. Replacing Text

The replace() method returns a new String in which matching characters or character sequences are replaced.

public class Main {
    public static void main(String[] args) {
        String message = "I like Python";

        String newMessage = message.replace("Python", "Java");

        System.out.println(message);
        System.out.println(newMessage);
    }
}
Output:
I like Python
I like Java

The original String remains unchanged because Strings in Java are immutable.

11. Removing Extra Spaces

The trim() method removes leading and trailing characters that have a code point at or below U+0020. The strip() method, available from Java 11, removes leading and trailing Unicode whitespace.

public class Main {
    public static void main(String[] args) {
        String name = "   Rahul Kumar   ";

        System.out.println(name);
        System.out.println(name.trim());
        System.out.println(name.strip());
    }
}
Output:
   Rahul Kumar
Rahul Kumar
Rahul Kumar

These methods do not remove spaces between words. For example, the space between Rahul and Kumar remains.

12. Other Useful String Methods
Method Purpose
charAt(index) Returns the character at an index.
startsWith(text) Checks the beginning of a String.
endsWith(text) Checks the end of a String.
isEmpty() Checks whether the length is zero.
isBlank() Checks whether a String is empty or contains only whitespace (Java 11+).
concat(text) Joins another String to the current String.
public class Main {
    public static void main(String[] args) {
        String text = "Java Programming";

        System.out.println(text.charAt(0));
        System.out.println(text.startsWith("Java"));
        System.out.println(text.endsWith("ing"));
        System.out.println("".isEmpty());
        System.out.println("   ".isBlank());
        System.out.println("Hello ".concat("Students"));
    }
}
Output:
J
true
true
true
true
Hello Students
13. Practical Example: Student Information

This program combines String variables and methods to display student information.

public class Main {
    public static void main(String[] args) {
        String firstName = "Aman";
        String lastName = "Kumar";
        String course = "java programming";

        String fullName = firstName + " " + lastName;

        System.out.println("Student Name: " + fullName);
        System.out.println("Name Length: " + fullName.length());
        System.out.println("Course: " + course.toUpperCase());
        System.out.println("Contains Java: " +
                course.toLowerCase().contains("java"));
        System.out.println("First Name: " +
                fullName.substring(0, 4));
    }
}
Output:
Student Name: Aman Kumar
Name Length: 10
Course: JAVA PROGRAMMING
Contains Java: true
First Name: Aman
Lesson Summary:
  • Strings store text in Java.
  • + joins strings.
  • length() returns the number of UTF-16 code units.
  • equals() compares String contents.
  • toUpperCase() and toLowerCase() change letter case in the returned String.
  • substring() extracts part of a String.
  • replace() returns a String with replacements.
  • Strings are immutable.

🧠 Quick Quiz

Q1. Which method returns the length of a String?



Q2. Which method compares String contents?



Q3. What does "Java".toUpperCase() return?