In Java, Strings are objects that represent sequences of characters. Strings are immutable, meaning once a String object is created, its value cannot be changed.
Creation:
1. Using String literal:
java
String str = “Hello”;
2. Using new keyword:
java
String str = new String(“Hello”);
Common Methods:
– length(): Returns the length of the string.
java
str.length();
– charAt(int index): Returns the character at a specified index.
java
str.charAt(0); // ‘H’
– substring(int beginIndex, int endIndex): Returns a substring.
java
str.substring(0, 2); // “He”
– equals(Object obj): Compares two strings for equality.
java
str.equals(“Hello”);
– toUpperCase() / toLowerCase(): Converts to uppercase/lowercase.
java
str.toUpperCase();
Strings are widely used for storing and manipulating text, and Java provides a rich set of methods in the String class for handling them efficiently.