Java Strings

Agenda
-
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.

String class
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 pool

String pool
String s1 = "abc";
String pool

String pool
String s1 = "abc";
String pool
"abc"

String pool
String s1 = "abc";
String pool
"abc"
s1

String pool
String s1 = "abc";
String s2 = "abc";
String pool
"abc"
s1

String pool
String pool
"abc"
s1
s2
String s1 = "abc";
String s2 = "abc";

String pool
String pool
"abc"
s1
s2
String s1 = "abc";
String s2 = "abc";

String pool
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
String pool
"abc"
s1
s2

String pool
String pool
"abc"
s1
s2
"def"
s3
String s1 = "abc";
String s2 = "abc";
String s3 = "def";

String pool
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
s2 = "hello world";
String pool
"abc"
s1
s2
"def"
s3

String pool
String pool
"abc"
s1
s2
"def"
s3
"hello world"
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
s2 = "hello world";

String pool
String pool
"abc"
s1
s2
"def"
s3
"hello world"
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
s2 = "hello world";

String pool
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.

Strings using new
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 pool
String s1 = "abc";
String s2 = "abc";
String s3 = "def";
s2 = "hello world";
String pool
"abc"
s1
s2
"def"
s3
"hello world"
Heap memory

String pool
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 pool
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 pool
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 methods
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.

charAt
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.

length
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.

indexOf
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.

equals
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

contains
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.

toLowerCase, toUpperCase
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.

replace
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.

substring
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"
}
}
- Concatenation is the process of appending one string to the end of another string.
- Java strings can be added by using the '+' operator.
- When a string is concatenated with primitive types, they are automatically typecasted to strings.
- When a string is concatenated with non-primitive types, the toString() on that object is called automatically.

String concatenation
class Main {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";
String str3 = str1 + str2;
System.out.println(str3);
}
}

Reverse a string
Given a string as input, print its reverse
Sample Input
hello
Sample Output
olleh
Java StringBuilder class is used to create mutable (modifiable) String.

StringBuilder
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
}
}

Is it Palindrome?
Given a string, check whether it is a palindrome.
Sample Input
tenet
Sample Output
Yes

Is it Palindrome?
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();
}
}

Is it Palindrome?
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();
}
}

Is it Palindrome?
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();
}
}

Is it Palindrome?
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.

Strings and char arrays
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 vs Char arrays
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 |
Strings - Java
By Tarun Luthra
Strings - Java
- 151