
- 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 a String in JShell in Java 9?
JShell is Java’s first official REPL application introduced in Java 9. It is a tool that helps in executing and evaluating simple java programs, and small logics such as statements, simple programs, loops, expressions, etc. Java REPL can provide a simple programming environment in a command-line prompt. It reads the input, evaluates it, and prints the output.
In the below example, we can implement a string with pre-defined methods of String class.
Example
jshell> String str = "{abcd}"; str ==> "{abcd}" jshell> str.substring(2, str.length() - 1) $7 ==> "bcd" jshell> String s1 = new String("abcd"); s1 ==> "abcd" jshell> String s2 = new String("abcd"); s2 ==> "abcd" jshell> s1 == s2 $10 ==> false jshell> s1.equals(s2) $11 ==> true jshell> String s3 = "abcd"; s3 ==> "abcd" jshell> String s4 = "abcd"; s4 ==> "abcd" jshell> s3 == s4 $14 ==> true jshell> s3.equals(s4) $15 ==> true jshell> s1 == s3 $16 ==> false jshell> s1.equals(s3) $17 ==> true jshell> String s5 = "a" + "bcd"; s5 ==> "abcd" jshell> s3 == s5 $19 ==> true jshell> "abcd".getBytes() $20 ==> byte[4] { 97, 98, 99, 100 } jshell> "abcd".getBytes("UTF-16") $22 ==> byte[10] { -2, -1, 0, 97, 0, 98, 0, 99, 0, 100 } jshell> String raw = "1|2|3|4"; raw ==> "1|2|3|4" jshell> raw.split("\|") $24 ==> String[4] { "1", "2", "3", "4" }
- Related Articles
- How to implement String utility and immutability in JShell in Java 9?
- How to implement a lambda expression in JShell in Java 9?
- How to implement a Set interface in JShell in Java 9?
- How to implement java.time.LocalDate using 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 can we implement a map in JShell in Java 9?
- How to implement the encapsulation concept 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 relational and logical operators in JShell in Java 9?
- How to implement HashMap, LinkedHashMap, and TreeMap in JShell in Java 9?
- How to check a string is palindrome or not in Jshell in Java 9?
- How to debug JShell in Java 9?
- How to create a thread in JShell in Java 9?

Advertisements