
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to implement String utility and immutability in JShell in Java 9?
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."
- Related Articles
- How to implement a String in JShell in Java 9?
- How to implement java.time.LocalDate using JShell in Java 9?
- How to implement relational and logical operators in JShell in Java 9?
- How to implement HashMap, LinkedHashMap, and TreeMap in JShell in Java 9?
- How to implement an ArrayList using JShell in Java 9?
- How to implement JShell using JavaFX in Java 9?\n
- How to implement the encapsulation concept in JShell in Java 9?
- How to implement a lambda expression in JShell in Java 9?
- How to implement the Fibonacci series in JShell in Java 9?
- How to implement integer type conversion in JShell in Java 9?
- How to implement a Set interface in JShell in Java 9?
- How can we implement a map in JShell in Java 9?
- How to debug JShell in Java 9?
- JShell in Java 9?
- How to get JShell documentation in Java 9?
