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.
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);
}
}
Rahul
Aurangabad
Java Programming
The String type is a reference type in Java,
not a primitive data type.
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);
Amit
Kumar
You can also declare a variable before assigning a value.
String message;
message = "Welcome to Java";
System.out.println(message);
Welcome to Java
"Hello". Single quotes are used for
individual character literals, such as 'A'.
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);
}
}
Rahul Kumar
The space inside " " separates the first
name and last name.
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));
}
}
Name: Aman
Age: 20
Next year age: 21
System.out.println("Result: " + 10 + 5);
System.out.println("Result: " + (10 + 5));
Result: 105
Result: 15
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());
}
}
5
10
Spaces are also counted. In "Hello Java",
there are five letters, one space, and four letters.
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());
}
}
SOOPRO PATHSHALA
soopro pathshala
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));
}
}
true
false
true
The equalsIgnoreCase() method compares
strings without considering letter case.
== when you want to compare
String contents. For content comparison, use
equals(). The == operator
checks whether two references refer to the same object.
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"));
}
}
true
false
13
String positions start at index 0. If a searched
substring is not found, indexOf()
returns -1.
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));
}
}
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 |
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);
}
}
I like Python
I like Java
The original String remains unchanged because Strings in Java are immutable.
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());
}
}
Rahul Kumar
Rahul Kumar
Rahul Kumar
These methods do not remove spaces between words. For example, the space between Rahul and Kumar remains.
| 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"));
}
}
J
true
true
true
true
Hello Students
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));
}
}
Student Name: Aman Kumar
Name Length: 10
Course: JAVA PROGRAMMING
Contains Java: true
First Name: Aman
+ 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.Q1. Which method returns the length of a String?
Q2. Which method compares String contents?
Q3. What does "Java".toUpperCase() return?