- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Strings
In Java, strings are not just simple pieces of text; they are powerful objects that represent sequences of characters. Unlike some other languages where strings might be simple arrays of bytes, Java treats every string as an instance of the String class, providing a rich set of tools for text manipulation.
String Class:
- The
java.lang.Stringclass is built directly into the core Java library, meaning you don't need to import anything special to use it. - Immutability: This is the most critical concept to understand about Java Strings. Once a
Stringobject is created, its content cannot be modified. When you perform an operation that seems to "change" a string, Java actually creates a brand-newStringobject in memory with the new value.
String Literals:
- A string literal is created by enclosing characters in double quotes (").
- Example:
"Hello, Java!". - Java optimizes memory usage by using a String Pool. If you create two string literals with the same content, Java often points both variables to the same memory location to save space.
new keyword to create strings, such as String s = new String("example");. This forces Java to create a new object outside the String Pool, which is inefficient and almost never necessary.
String Concatenation:
- You can join multiple strings together using the + operator. This is convenient for building messages or logging output.
- Example:
"Hello" + ", " + "Java!"results in the single string"Hello, Java!". - Java also allows you to concatenate strings with other data types (like integers or booleans). The non-string value will automatically be converted to its string representation.
+ operator inside a large for or while loop to build a long string. Because strings are immutable, every single loop iteration creates a new object, which can drastically slow down your application.
StringBuilder. It is designed for mutable character sequences and is much faster for intensive text processing.
String Methods:
The String class is packed with utility methods that handle common tasks. Instead of writing complex logic yourself, you can use these built-in functions:
length(): Returns the number of characters in the string.charAt(index): Retrieves the character at a specific position (starting from 0).substring(start, end): Extracts a portion of the string.indexOf(str): Finds the first occurrence of a character or substring.toUpperCase()/toLowerCase(): Changes the casing of the entire string.trim(): Removes leading and trailing whitespace, which is excellent for cleaning up user input.
String Comparison:
- To compare the actual text content inside two strings, you must use the
equals()method. - For sorting or "dictionary order" (lexicographical) comparison, use
compareTo(). It returns 0 if they are equal, a negative number if the first string comes first, and a positive number otherwise.
== to compare strings. The == operator checks if two variables point to the same memory location (reference equality), not if they contain the same characters. Always use .equals() for content verification.
Example
public class StringsExample {
public static void main(String[] args) {
// Creating a string via literal
String message = "Hello, Java!";
// Concatenation: Building a dynamic string
String greeting = "Hello";
String name = "Java";
String sentence = greeting + ", " + name + "!"; // Result: "Hello, Java!"
// Practical usage of String methods
int length = message.length(); // 12
char firstChar = message.charAt(0); // 'H'
String substring = message.substring(7); // "Java!"
int index = message.indexOf(","); // 5
// Validation examples (Real-world use case)
boolean startsWithHello = message.startsWith("Hello"); // true
boolean endsWithJava = message.endsWith("Java!"); // true
// Comparison: The right way vs the wrong way
String str1 = "Apple";
String str2 = new String("Apple");
boolean isContentSame = str1.equals(str2); // true (The content is the same)
boolean isRefSame = (str1 == str2); // false (They are different objects in memory)
int comparisonResult = message.compareTo("Hello, World!"); // Returns a negative value
}
}
Summary
Java strings are represented by the String class and are unique because they are immutable. While they look like simple variables, they are objects with a wide array of built-in methods for searching, formatting, and slicing text. By mastering the String Pool, understanding the difference between == and .equals(), and knowing when to switch to StringBuilder, you will write more efficient and bug-free Java code.