In Java, a `String` is a sequence of characters used to represent text. It is a fundamental data type and also an object in Java, belonging to the `java.lang` package. Here are some key points about `String` in Java:
1. **String Declaration:**
- You can declare a `String` variable like this:
```java
String str;
```
- You can also initialize it with a value:
```java
String greeting = "Hello, World!";
```
2. **Immutable Nature:**
- In Java, `String` objects are immutable, meaning once created, their values cannot be changed. Operations on `String` objects (like concatenation) actually create new `String` objects.
3. **String Pool:**
- Java maintains a "String pool" (or "intern pool") to store `String` literals. This allows for efficient reuse of `String` objects with the same value.
4. **Creating Strings:**
- Besides literals, you can create `String` objects using the `new` keyword:
```java
String str = new String("Java");
```
However, using literals is more common and efficient due to string interning.
5. **String Concatenation:**
- You can concatenate `String` objects using the `+` operator:
```java
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName; // "John Doe"
```
- Alternatively, you can use the `concat` method:
```java
String fullName = firstName.concat(" ").concat(lastName); // "John Doe"
```
6. **String Methods:**
- `String` class provides various methods for manipulating strings such as:
- `length()`: Returns the length of the string.
- `charAt(int index)`: Returns the character at the specified index.
- `substring(int beginIndex)`: Returns a substring starting from `beginIndex`.
- `substring(int beginIndex, int endIndex)`: Returns a substring from `beginIndex` to `endIndex`.
- `equals(Object obj)`: Compares two strings for equality.
- `equalsIgnoreCase(String anotherString)`: Compares two strings, ignoring case considerations.
- `indexOf(String str)`: Returns the index within the string of the first occurrence of the specified substring.
- `startsWith(String prefix)`, `endsWith(String suffix)`: Checks if the string starts or ends with the specified prefix or suffix.
- `toUpperCase()`, `toLowerCase()`: Converts the string to uppercase or lowercase.
7. **String Literal vs. String Object:**
- String literals are stored in the string pool and are shared among multiple clients.
- String objects created using `new` are stored in the heap memory.
8. **String Buffer and String Builder:**
- `StringBuffer` and `StringBuilder` are mutable counterparts of `String`, useful when you need to perform a lot of modifications on strings without creating multiple intermediate `String` objects.
9. **Unicode Support:**
- Java `String` supports Unicode characters, allowing representation of characters from various languages and symbols.
10. **String Handling Best Practices:**
- Use `StringBuilder` when concatenating multiple strings in a loop for better performance.
- Prefer `equals()` method for comparing content equality of strings instead of `==` operator, which compares references.
In summary, `String` in Java is a powerful and fundamental data type, extensively used for representing and manipulating textual data in Java programs.