In Java, a String is a sequence of characters used to represent text. Understanding how to work with strings is fundamental in any programming language because almost every application deals with some form of textual data. In this tutorial, we'll explore various aspects of Java strings, including string methods such as length, toUpperCase, and indexOf, string concatenation, and escape sequences.
Strings are immutable sequences of characters. This means that once a string is created, its value cannot be changed. Instead, any operation that modifies a string actually creates a new string object. Understanding this immutability is crucial for efficient memory management in Java applications.
In this tutorial, we'll cover the following topics:
length, toUpperCase, and indexOf.+ operator and the concat method.The length() method returns the number of characters in a string.
1public class StringLength {2public static void main(String[] args) {3String text = "Hello, World!";4int length = text.length();5System.out.println("The length of the string is: " + length);6};7}
The length of the string is: 13
The toUpperCase() method converts all characters in a string to uppercase.
1public class StringToUpper {2public static void main(String[] args) {3String text = "Hello, World!";4String upperCaseText = text.toUpperCase();5System.out.println("Uppercase: " + upperCaseText);6}7}
Uppercase: HELLO, WORLD!
The indexOf() method returns the index of the first occurrence of a specified character or substring.
1public class StringIndexOf {2public static void main(String[] args) {3String text = "Hello, World!";4int index = text.indexOf('o');5System.out.println("Index of 'o': " + index);6}7}
Index of 'o': 4
String concatenation is the process of joining strings together.
+ OperatorThe + operator can be used to concatenate strings.
1public class StringConcatPlus {2public static void main(String[] args) {3String firstName = "John";4String lastName = "Doe";5String fullName = firstName + " " + lastName;6System.out.println("Full Name: " + fullName);7}8}
Full Name: John Doe
concat MethodThe concat() method is another way to concatenate strings.
1public class StringConcatMethod {2public static void main(String[] args) {3String firstName = "John";4String lastName = "Doe";5String fullName = firstName.concat(" ").concat(lastName);6System.out.println("Full Name: " + fullName);7}8}
Full Name: John Doe
Escape sequences are special characters that represent certain non-printable or special characters in strings.
| Escape Sequence | Description |
|---|---|
\n | New line |
\t | Tab |
\ | Backslash |
\' | Single quote |
\" | Double quote |
1public class StringEscapeSequences {2public static void main(String[] args) {3String text = "Hello,4World! This is a backslash: ";5System.out.println(text);6}7}
Hello, World! This is a backslash:
Let's create a simple program that takes two strings, concatenates them with a space in between, converts the result to uppercase, and prints it.
1public class StringPracticalExample {2public static void main(String[] args) {3String firstName = "john";4String lastName = "doe";56// Concatenate with a space7String fullName = firstName + " " + lastName;89// Convert to uppercase10String upperCaseFullName = fullName.toUpperCase();1112System.out.println("Uppercase Full Name: " + upperCaseFullName);13}14}
Uppercase Full Name: JOHN DOE
length(), toUpperCase(), and indexOf() are commonly used string methods.+ operator or the concat method.Now that you've learned about Java strings, it's time to explore mathematical operations and calculations with numbers. In the next tutorial, we'll dive into Java Math, where you'll learn how to perform various arithmetic operations and use math-related classes and methods. Stay tuned!