如何在 Java 9 中在 JShell 中实现字符串?
JShell 是 Java 在 Java 9 中引入的第一个官方 REPL 应用程序。这是一个有助于执行和评估简单的 java 程序和小型逻辑(如 语句、简单程序、循环、表达式等)的工具。Java REPL 可以在 命令行提示符中提供一个简单的编程环境。它读取输入,对其进行评估并打印输出。
以下示例中,我们可以使用 String 类的 预定义方法来实现一个字符串。
示例
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" }
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP