Welcome to The Coding College! Strings are an essential part of Java programming, allowing you to handle and manipulate text effectively. In this guide, you’ll learn about Java Strings, their methods, and practical examples to boost your coding skills.
What is a String in Java?
In Java, a String is a sequence of characters. It is a non-primitive data type and is widely used for representing text. Strings are objects of the String
class, which belongs to the java.lang
package.
How to Create a String
There are two primary ways to create a String in Java:
- Using String Literals
- Using the
new
Keyword
Example:
public class StringExample {
public static void main(String[] args) {
// Using String Literal
String literalString = "Hello, World!";
// Using new Keyword
String newString = new String("Welcome to Java");
System.out.println(literalString);
System.out.println(newString);
}
}
String Characteristics
- Immutable: Strings cannot be changed after creation. Any modification creates a new String object.
- Stored in String Pool: Java optimizes memory by storing literal strings in a pool.
Common String Methods
The String
class provides several methods to perform operations on strings.
Method | Description | Example |
---|---|---|
length() | Returns the length of the string | "Java".length() |
toUpperCase() | Converts all characters to uppercase | "java".toUpperCase() |
toLowerCase() | Converts all characters to lowercase | "JAVA".toLowerCase() |
charAt(index) | Returns the character at the specified index | "Java".charAt(1) |
substring(start, end) | Extracts a substring between start and end | "Java".substring(0, 2) |
equals() | Compares two strings for equality | "Java".equals("JAVA") |
equalsIgnoreCase() | Compares two strings, ignoring case | "Java".equalsIgnoreCase("java") |
contains() | Checks if the string contains a specific sequence | "Java".contains("av") |
concat() | Concatenates two strings | "Java".concat(" Programming") |
replace(old, new) | Replaces all occurrences of a substring | "Java".replace("a", "o") |
trim() | Removes leading and trailing whitespace | " Java ".trim() |
Examples of String Methods
Example 1: Length and Case Conversion
public class StringMethods {
public static void main(String[] args) {
String str = "Hello, Java!";
System.out.println("Length: " + str.length());
System.out.println("Uppercase: " + str.toUpperCase());
System.out.println("Lowercase: " + str.toLowerCase());
}
}
Example 2: Substring and Character Access
public class SubstringExample {
public static void main(String[] args) {
String str = "Programming";
System.out.println("Substring: " + str.substring(0, 6));
System.out.println("Character at index 4: " + str.charAt(4));
}
}
Example 3: Equality and Replace
public class EqualityExample {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "JAVA";
System.out.println("Equals: " + s1.equals(s2));
System.out.println("Equals Ignore Case: " + s1.equalsIgnoreCase(s2));
System.out.println("Replace 'a' with 'o': " + s1.replace('a', 'o'));
}
}
String Concatenation
Java allows you to concatenate strings using:
- The
+
Operator - The
concat()
Method
Example:
public class ConcatenationExample {
public static void main(String[] args) {
String part1 = "Hello";
String part2 = "World";
// Using +
String result1 = part1 + " " + part2;
// Using concat()
String result2 = part1.concat(" ").concat(part2);
System.out.println(result1);
System.out.println(result2);
}
}
String Immutability
Strings in Java are immutable, meaning you cannot change the content of a string once created.
Example:
public class ImmutabilityExample {
public static void main(String[] args) {
String str = "Hello";
str.concat(" World");
System.out.println(str); // Output: Hello
}
}
To modify strings, consider using StringBuilder
or StringBuffer
.
Practice Exercises
- Write a program to check if two strings are anagrams.
- Create a function to reverse a string using
charAt()
. - Extract the first name and last name from a full name string and print them separately.
Conclusion
Strings are a powerful tool in Java for handling text. Mastering string manipulation will open up many possibilities in programming, from data processing to user interaction.
For more programming tutorials, visit The Coding College. Keep practicing and elevate your coding skills! 🚀