java 15: 用jshell体验交互式java编程
一,jshell查看帮助
[lhdop@blog ~]$ jshell --help Usage: jshell <option>... <load-file>... where possible options include: --class-path <path> Specify where to find user class files --module-path <path> Specify where to find application modules --add-modules <module>(,<module>)* …
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/06/01/java-15-yong-jshell-ti-yan-jiao-hu-shi-java-bian-cheng/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
或: https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,jshell的使用
1, -v : 详细模式启动JShel
[lhdop@blog ~]$ jshell -v | Welcome to JShell -- Version 15 | For an introduction type: /help intro jshell>
2,退出:
jshell> /exit
| Goodbye
3,一个整数相加的例子:
jshell> int x = 35; x ==> 35 | created variable x : int jshell> int y = 33; y ==> 33 | created variable y : int jshell> int z = x + y; z ==> 68 | created variable z : int jshell> System.out.println(z); 68
4,jshell中使用函数
jshell> Math.pow(2,3) $1 ==> 8.0 | created scratch variable $1 : double
5,jshell中使用HashMap
jshell> HashMap<Integer,String> mapOne = new HashMap<Integer,String>(); mapOne ==> {} | created variable mapOne : HashMap<Integer, String> jshell> mapOne.put(35,"thirty five"); $3 ==> null | created scratch variable $3 : String jshell> mapOne.get(35); $4 ==> "thirty five" | created scratch variable $4 : String
6,jshell中自定义函数
jshell> String glue(String a,String b) { ...> return a+","+b; ...> } | created method glue(String,String) jshell> glue("laoliu","good") $6 ==> "laoliu,good" | created scratch variable $6 : String
7,jshell中的内置命令:
/vars 查看变量
jshell> /vars | double $1 = 8.0 | HashMap<Integer, String> mapOne = {35=thirty five} | String $3 = null | String $4 = "thirty five”
/list 列出执行过的代码片段
jshell> /list 1 : Math.pow(2,3) 2 : HashMap<Integer,String> mapOne = new HashMap<Integer,String>(); 3 : mapOne.put(35,"thirty five"); 4 : mapOne.get(35);
/methods 列出自定义的方法
jshell> /methods
| String glue(String,String)
/ 列出所有命令
jshell> / | Command: '/' is ambiguous: /list, /edit, /drop, /save, /open, /vars, /methods, /types, /imports,
/exit, /env, /reset, /reload, /history, /debug, /help, /set, /?, /! | Type /help for help.
/help 详细的帮助
jshell> /help | Type a Java language expression, statement, or declaration. | Or type one of the following commands: | /list [<name or id>|-all|-start] | list the source you have typed | /edit <name or id> | edit a source entry | /drop <name or id> | delete a source entry ...
三,查看java的版本:
[lhdop@blog tools]$ java --version java 15 2020-09-15 Java(TM) SE Runtime Environment (build 15+36-1562) Java HotSpot(TM) 64-Bit Server VM (build 15+36-1562, mixed mode, sharing)