简体   繁体   English

为什么以及如何使用JShell?

[英]Why and how do you use JShell?

I went through a couple of tutorials for JShell and I still do not understand the most important part: why would I even use JShell? 我经历了几个关于JShell的教程,我仍然不了解最重要的部分:为什么我甚至会使用JShell?

Here what Oracle says: 这就是Oracle所说的:

"You can test individual statements, try out different variations of a method, and experiment with unfamiliar APIs within the JShell session". “您可以测试单个语句,尝试不同的方法变体,并在JShell会话中尝试不熟悉的API”。

Yes, great, but for all those things I can just use an IDE. 是的,很棒,但对于所有这些事情,我可以使用IDE。 I do understand, that REPL should make code evaluation faster. 我明白,REPL应该能够更快地进行代码评估。 However, testing a snippet of code in IDE in a dummy Hello World project with Sysout is for sure not slower? 但是,在带有Sysout的虚拟Hello World项目中在IDE中测试一段代码肯定不会慢一些?

In fact, IDEs provide autocomplete, detect errors early and I can checkout an implementation of a method/class with a mouse click. 事实上,IDE提供自动完成,早期检测错误,我可以通过鼠标点击检查方法/类的实现。 This all should make code testing in IDE faster than in JShell? 这一切都应该使IDE中的代码测试比JShell更快?

What am I missing? 我错过了什么? Can someone give me a couple of use cases, where using JShell is better then using IDE? 有人可以给我一些用例,使用JShell比使用IDE更好吗? How do you guys use JShell? 你们是如何使用JShell的?

Though I would really encourage you to go ahead and read The Java Shell motivation and goals. 虽然我真的鼓励你继续阅读Java Shell的动机和目标。

Yet to answer your question with a slight hands-on and I would actually be glad to know about the time you take and procedure you would follow while trying this out on an IDE. 然而,通过轻微的动手回答你的问题,我真的很高兴知道你在IDE上尝试这个时所采取的步骤和程序。 So, with few those, here you go:- 那么,只有少数,你去: -

1. I recently got to know that Java 9 introduced overloaded Convenience Factory Methods for Collections and then was curious about what do they do and how do I make use of them. 1.我最近知道Java 9引入了重载的Convenience Factory方法收集,然后很好奇他们做了什么以及如何使用它们。

Steps 脚步

-> Type 'jshell' on command prompt (assuming JAVA_HOME is set to JDK9's bin)
-> Type 'List.o'
-> Press Tab (completes the `of`) 
-> Tab displays all signatures 
-> Tab starts reading the documentation from first

在此输入图像描述

2. How about persisting the syntax of the code? 2.如何保持代码的语法? If you would have noticed in the above image the initialization of the List didn't even bother to care about the variable to store it in, terminating semi-colon etc. 如果您在上面的图像中注意到List的初始化甚至不关心存储它的变量,终止分号等。

3. How quickly can you find out the exceptions thrown by some piece of code that you are about to implement? 3.您能多快找出您要实施的某些代码抛出的异常? So, a use case here, I want to create a Map of String(name of fruits) to String(their color), then add some more fruits to it in the later phase and leave out those colors which I am not certain about. 因此,这里使用的情况下,我想创建一个Map的字符串(如水果的名称)的字符串(它们的颜色),那么一些水果在后期添加到它,离开了那些我不能确定的颜色。 Then finally get a list of all those fruits which are Red. 然后最终获得所有那些红色水果的清单。 And of course, since I am learning Java 9 these days, I would try to use as much of APIs as from Java9. 当然,既然我现在正在学习Java 9,我会尝试使用与Java9一样多的API。

在此输入图像描述

And while you would try adding all of that(^^) code in your IDE, you can yourself notice the time you would eventually take to realize all those characteristics of Immutable Map Static Factory Methods. 虽然您可以尝试在IDE中添加所有(^^)代码,但您可以自己注意到最终用于实现不可变映射静态工厂方法的所有特性的时间

PS : PS

  • Robert has presented Jshell here which is a detailed view of how all can the JShell be effective. Robert 在这里介绍了Jshell,它详细介绍了JShell如何有效。

  • As you do all of the above, do keep in mind that it's not a goal of Jshell to be an IDE. 正如您所做的所有这些,请记住,Jshell的目标不是成为IDE。

  • And all of that, in general, does simple experimentation with a language by bypassing the compile stage of the "code -> compile -> execute" cycle. 总的来说,所有这一切都通过绕过“代码 - >编译 - >执行”循环的编译阶段对语言进行简单的实验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM