String class & objects
String methods
String using new
String pool
Extracting Characters
String Builder
char arrays vs Strings
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
Strings in Java are immutable.
class Main {
public static void main(String[] args) {
String str = "abc";
}
}
The String pool is the area in the heap memory where string literals are stored
String s1 = "abc";
String pool
String s1 = "abc";
String pool
"abc"
String s1 = "abc";
String pool
"abc"
s1
String s1 = "abc";
String s2 = "abc";
String pool
"abc"
s1
String pool
"abc"
s1
s2
String s1 = "abc";
String s2 = "abc";
String pool
"abc"
s1
s2
String s1 = "abc";
String s2 = "abc";
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
String pool
"abc"
s1
s2
String pool
"abc"
s1
s2
"def"
s3
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
s2 = "hello world";
String pool
"abc"
s1
s2
"def"
s3
String pool
"abc"
s1
s2
"def"
s3
"hello world"
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
s2 = "hello world";
String pool
"abc"
s1
s2
"def"
s3
"hello world"
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
s2 = "hello world";
String pool
"abc"
s1
s2
"def"
s3
"hello world"
Heap memory
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
s2 = "hello world";
We can also create new strings using the new keyword. These strings do not reside in the String pool, instead they are created and stored in the heap memory.
class Main {
public static void main(String args[]) {
String s = "abc";
String s2 = new String("abc");
if (s == s2) {
System.out.println("This will not work");
}
if (s.equals(s2)) {
System.out.println("This will work");
}
}
}
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
s2 = "hello world";
String pool
"abc"
s1
s2
"def"
s3
"hello world"
Heap memory
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
s2 = "hello world";
String s4 = new String("abc");
String pool
"abc"
s1
s2
"def"
s3
"hello world"
Heap memory
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
s2 = "hello world";
String s4 = new String("abc");
String pool
"abc"
s1
s2
"def"
s3
"hello world"
Heap memory
s4
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
s2 = "hello world";
String s4 = new String("abc");
String pool
"abc"
s1
s2
"def"
s3
"hello world"
Heap memory
s4
"abc"
String
String class offers several methods out of the box. We will be looking at some of them in this section.
charAt method allows us to access the character at the specified index.
String str = "Hello World";
System.out.println(str.charAt(3)); // l
System.out.println(str.charAt(8)); // r
The length() method returns the length of the string.
class Main {
public static void main(String[] args) {
String str1 = "abc";
System.out.println(str1.length()); // 3
String str2 = "Hello World";
System.out.println(str2.length()); // 11
}
}
indexOf methods returns the index of the first occurrence of the specified char or string in the given string.
If it is present, then it returns -1.
class Main {
public static void main(String[] args) {
String str = "Hello World";
System.out.println(str.indexOf('W')); // 6
System.out.println(str.indexOf("ll")); // 2
System.out.println(str.indexOf("abc")); // -1
}
}
Equals method is used to compare whether the two strings contain the same sequence of characters.
class Main {
public static void main(String[] args) {
String str = "Hello World";
String str2 = new String("Hello World");
System.out.println(str.equals(str2)); // true
System.out.println(str == str2); // false
}
}
Returns true if and only if this string contains the specified sequence of char values
class Main {
public static void main(String[] args) {
String str = "Hello World";
System.out.println(str.contains("ll")); // true
System.out.println(str.contains("abc")); // false
}
}
Returns new strings after changing the case.
Original string remains intact.
class Main {
public static void main(String[] args) {
String str = "Hello World";
System.out.println(str.toLowerCase()); // hello world
System.out.println(str.toUpperCase()); // HELLO WORLD
System.out.println(str); // Hello World
}
}
Replaces the target string (first) with the given replacement string (second) and returns a new string.
class Main {
public static void main(String[] args) {
String str = "I love Programming";
String str2 = str.replace("Programming", "Java");
System.out.println(str); // I love Programming
System.out.println(str2); // I love Java
}
}
Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
If endIndex is not given, it is taken as string length.
class Main {
public static void main(String[] args) {
String str = "I love Programming";
// Start at index=7 and extract till the end
String sub = str.substring(7);
System.out.println(sub); // "Programming"
// Start at index=2 and extract chars till index<6
String sub2 = str.substring(2, 6);
System.out.println(sub2); // "love"
}
}
class Main {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";
String str3 = str1 + str2;
System.out.println(str3);
}
}
Given a string as input, print its reverse
Sample Input
hello
Sample Output
olleh
Java StringBuilder class is used to create mutable (modifiable) String.
class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb); // Hello World
sb.insert(5, '_');
System.out.println(sb); // Hello_ World
sb.replace(5, sb.length(), " Java");
System.out.println(sb); // Hello Java
}
}
Given a string, check whether it is a palindrome.
Sample Input
tenet
Sample Output
Yes
Given a string, check whether it is a palindrome.
Sample Input
tenet
Sample Output
Yes
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
}
}
Given a string, check whether it is a palindrome.
Sample Input
tenet
Sample Output
Yes
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
StringBuilder temp = new StringBuilder(str);
temp.reverse();
}
}
Given a string, check whether it is a palindrome.
Sample Input
tenet
Sample Output
Yes
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
StringBuilder temp = new StringBuilder(str);
temp.reverse();
String reverse = temp.toString();
}
}
Given a string, check whether it is a palindrome.
Sample Input
tenet
Sample Output
Yes
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
StringBuilder temp = new StringBuilder(str);
temp.reverse();
String reverse = temp.toString();
if (str.equals(reverse)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
Both Strings and character arrays allows us to store and handle sequences of characters. But they are different in some properties.
class Main {
public static void main(String[] args) {
String str = "hello";
char[] arr = {'h', 'e', 'l', 'l', 'o'};
System.out.println(str);
System.out.println(arr);
String str2 = new String(arr);
System.out.println(str2);
char[] arr2 = str.toCharArray();
System.out.println(arr2);
}
}
Strings | Character Arrays |
---|---|
Sequence of chars represented as a single data type. | Sequential collection of char data type. |
Immutable | Mutable |
Offers several builtin methods. | No builtin methods |
Stored in the String pool | Stored in the Heap |