Java String — Interesting features with examples.
String is immutable Object in java. It means once we create the new Object it cannot be modified. If any modification performed it always return new Object, the source string remains same.
public String toUpperCase() && public String toLoweCase():
Converts the input string to upper case or lower case using default locale. Since String is immutable in Java, always above methods creates new Objects .
String testString = “Hello World”;
System.out.println(“UpperCase :: “+testString.toUpperCase());
System.out.println(“LoweCase :: “+testString.toLowerCase());
public String concat() and “+” Operator
concat the specific string to the end of the string.
“+” Operator takes any number of arguments at left and right side and appends it.
String str = “Hello”;
String newString = str.concat(“ World”);
System.out.println(“with concat :: “+newString);
System.out.println(“with + operator :: “+”New “+str+ “ World”);
public boolean isBlank() and public boolean isEmpty()
isBlank returns true if it is empty or it contains only blank space. — Java 11
isEmpty returns true, if and only if String length is zero.
String str = “ “;
System.out.println(str.isBlank());
System.out.println(str.isEmpty());
System.out.println(str.trim().isEmpty());
public boolean equals(String another)
Compare the String to the specified Object, if char sequence is equal returns true else return false.
String str1 = “Hello“;
String str2 = “Hello“;
System.out.println(str1.equals(str2));
public String repeat(int count) — Java 11
This method repeats the string as many times it specified in the count.
String str = “Hello“;
System.out.println(str.repeat(3));
public String indent (int count) — Java 12
Adjusts the indentation by n for every line
count - add or remove number of leading white space characters.
returns - string with indentation adjusted and line endings normalized.
String str = "Hello \n"+
"Hello again";
System.out.println(str);
System.out.println(str.indent(5));
public Stream<String> lines() — Java 11
This method returns stream of lines extracted from the input string, it separated by line terminators.
String testString = “First Hello World \n”+
“Second Hello World \n”+
“Third Hello World …”;
Stream<String> lines = testString.lines();
lines.forEach(line -> System.out.println(line));
IntStream chars() — Java 9
Returns a stream of int zero-extending the char values from this sequence.
String str = “Hello World”;
IntStream stream = str.chars();
stream.forEach(ch -> System.out.println((char)ch));
IntStream codePoints() — Java 12
Returns a stream of code point values from this sequence.
String str = “Hello World”;
IntStream stream = str.codePoints();
stream.forEach(ch -> System.out.println((char)ch));
public String intern()
When the intern method is invoked, if the pool already contains a string equal to this String
object as determined by the equals(Object)
method, then the string from the pool is returned. Otherwise, this String
object is added to the pool and a reference to this String
object is returned.
String s = “Hello”;
String t = “Hello”;
System.out.println(t.intern() == s.intern());
Above examples are present in the below git repository.