How to create and modify function using Jshell in Java CLI

To use the jShell effectively, You must be able to create function so that you can test your functionality quickly and it will be really helpful when you are developing a complex logic in Java.

This tutorial will show you how to create a small function and call it from Jshell CLI and then How to modify the same function.

  1. Create a function using Jshell CLI also known as Java REPL

jshell-function-1

In above image,

Step 1: is initializing jshell

Step 2: We are creating normal java function with return value and argument and greeting function name and welcome message as function body, It will show us the output as

| created method greeting(String)

Step 3: This will call the function which we have just created and Output shows the name which we have passed is appended to the Welcome message.

$2 ==> "Welcome to Jshell function creation tutorial :-> yoUVcode"

2. Modify the existing function which we have just created.

jshell-function-2

As highlighted with yellow underline in above image, When you write function with same signature then it is not creating a new function, While it will modify the existing function and display the output respectively.

If you want to know, How to start Jshell please refer Java 9 : JShell (REPL) feature.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s