How to implement String utility and immutability in JShell in Java 9?

JavaObject Oriented ProgrammingProgramming

JShell is an interactive command-line tool used to implement simple statements like expressions, classes, methods, fields, interfaces, and etc. String class is part of the built-in java.lang package and provides several methods for common text processing.

1) String Utility: String provides several built-in utility methods. The methods like indexOf(), lastIndexOf(), startsWith(), endsWith(), isEmpty(), equals(), equalsIgnoreCase() are that part of string utility.

In the below code snippet, we have implemented the string utility methods in the JShell tool.

Snippet-1

jshell> String str = "JShell is a new feature in Java9";
str ==> "JShell is a new feature in Java9"

jshell> str.indexOf("new")
$4 ==> 12

jshell> str.charAt(7)
$5 ==> 'i'

jshell> str.indexOf('i')
$6 ==> 7

jshell> str.lastIndexOf('i')
$7 ==> 24

jshell> str.contains("feature")
$8 ==> true

jshell> str.startsWith("JShell")
$9 ==> true

jshell> str.startsWith("Java9")
$10 ==> false

jshell> str.endsWith("Java9")
$11 ==> true

jshell> str.endsWith("a9")
$12 ==> true

jshell> str.endsWith("a9java")
$13 ==> false

jshell> String str1 = "value"
str1 ==> "value"

jshell> str1.equals("value")
$15 ==> true

jshell> str1.equals("VALUE")
$16 ==> false

jshell> str1.equalsIgnoreCase("VALUE")
$17 ==> true


2) String Immutability: String objects are immutable, which means that we can't change their value after they are created.

In the below code snippet, the method concat() of String class joins the contents of two String objects into one. However, the original value referred by "str" remains unchanged. The concat() method will create a new String object. Just like concat() method, other String methods such as toUpperCase(), toLowerCase(), and trim() methods return new String objects.

Snippet-2

jshell> String str = "Tutorialspoint";
str ==> "Tutorialspoint"

jshell> str.concat(" is e-learning app");
$3 ==> "Tutorialspoint is e-learning app"

jshell> str
str ==> "Tutorialspoint" ^

jshell> String str1 = str.concat(".")
str1 ==> "Tutorialspoint."

jshell> str1
str1 ==> "Tutorialspoint."

jshell> String str = str.concat(" is e-learning app");
str ==> "Tutorialspoint is e-learning app"

jshell> str
str ==> "Tutorialspoint is e-learning app"

jshell> String str1 = "Tutorialspoint";
str1 ==> "Tutorialspoint"

jshell> str1.concat(" is e-learning app");
$2 ==> "Tutorialspoint is e-learning app"

jshell> str1
str1 ==> "Tutorialspoint"

jshell> String str2 = str1.concat(" is e-learning app");
str2 ==> "Tutorialspoint is e-learning app"

jshell> str1
str1 ==> "Tutorialspoint"

jshell> String str3 = str2.concat(".");
str3 ==> "Tutorialspoint is e-learning app."

jshell> str1
str1 ==> "Tutorialspoint"

jshell> str2
str2 ==> "Tutorialspoint is e-learning app"

jshell> String s = "Tutorialspoint is e-learning app."
s ==> "Tutorialspoint is e-learning app."

jshell> s.toUpperCase()
$10 ==> "TUTORIALSPOINT IS E-LEARNING APP."

jshell> s.toLowerCase()
$11 ==> "tutorialspoint is e-learning app."
raja
Published on 27-Apr-2020 21:31:09
Advertisements