为什么以及如何使用JShell? [英] Why and how do you use JShell?

查看:250
本文介绍了为什么以及如何使用JShell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历了一些有关JShell的教程,但我仍然不明白最重要的部分:为什么我什至还要使用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?

这是Oracle所说的:

Here what Oracle says:

您可以测试单个语句,尝试使用不同的变体形式 方法,并在JShell中试验不熟悉的API 会话".

"You can test individual statements, try out different variations of a method, and experiment with unfamiliar APIs within the JShell session".

是的,很好,但是对于所有这些事情,我都可以使用IDE.我确实知道,REPL应该使代码评估更快.但是,使用Sysout在虚拟Hello World项目中的IDE中测试代码片段肯定不会慢吗?

Yes, great, but for all those things I can just use an IDE. I do understand, that REPL should make code evaluation faster. However, testing a snippet of code in IDE in a dummy Hello World project with Sysout is for sure not slower?

实际上,IDE提供了自动完成功能,可以及早发现错误,并且我可以通过单击鼠标来检出方法/类的实现.这一切应该使在IDE中进行代码测试比在JShell中进行代码测试更快?

In fact, IDEs provide autocomplete, detect errors early and I can checkout an implementation of a method/class with a mouse click. This all should make code testing in IDE faster than in JShell?

我想念什么? 有人可以给我几个用例吗?使用JShell比使用IDE更好吗? 你们如何使用JShell?

What am I missing? Can someone give me a couple of use cases, where using JShell is better then using IDE? How do you guys use JShell?

推荐答案

尽管我会鼓励您继续阅读

Though I would really encourage you to go ahead and read The Java Shell motivation and goals.

还需要一点点动手就能回答您的问题,实际上,我很高兴知道您在IDE上尝试该操作所花费的时间和遵循的步骤.所以,只有少数几个,您可以开始:-

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. So, with few those, here you go:-

1.最近,我知道Java 9引入了用于集合的重载的便利工厂方法,然后对它们的作用以及如何使用它们感到好奇.

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.

步骤

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..如何保留代码的语法?如果您在上图中注意到List的初始化,它甚至不必理会要存储在其中的变量,终止分号等.

2. How about persisting the syntax of the code? 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.

3..如何迅速找出要实现的某些代码引发的异常?所以,在这里是一个用例,我想创建一个Map字符串(水果的名称)到字符串(其颜色),然后在以后的阶段中添加更多的水果,并忽略那些我不确定的颜色关于.然后,最终获得所有红色水果的清单.当然,由于这些天我正在学习Java 9,因此我将尝试使用与Java9一样多的API.

3. How quickly can you find out the exceptions thrown by some piece of code that you are about to implement? 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. 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.

当您尝试在IDE中添加所有(^^)代码时,您自己可以注意到最终需要花费时间来实现

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.

PS :

PS:

  • Robert has presented Jshell here which is a detailed view of how all can the JShell be effective.

当您完成以上所有操作时,请记住,成为IDE并不是Jshell的目标.

As you do all of the above, do keep in mind that it's not a goal of Jshell to be an IDE.

通常,所有这些方法都会绕过代码->编译->执行"循环的编译阶段,对语言进行简单的实验.

And all of that, in general, does simple experimentation with a language by bypassing the compile stage of the "code -> compile -> execute" cycle. read-eval-print-loop

这篇关于为什么以及如何使用JShell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆