Close

JShell - Tab Completion for snippets

[Last Updated: Oct 14, 2017]

In this tutorial, we will see how JShell can do auto completion for the code snippets that we enter.

Starting JShell

C:\>jshell
| Welcome to JShell -- Version 9
| For an introduction type: /help intro

Auto completion for Classes

Pressing tab key after an incomplete class name will display a set of possibilities:

jshell> BigTab
BigDecimal BigInteger

jshell> Big

We still have 'Big' as incomplete command followed by the active cursor, so now we are going to enter an 'I' followed by another Tab key (in the available list above, we are targeting BigInteger).

jshell> BigITabnteger
BigInteger

jshell> BigInteger

In above output, the part followed by the Tab key (i.e. nteger) is automatically inserted by JShell.

Let's enter the tab key one more time

jshell> BigIntegerTab
BigInteger

Signatures:
java.math.BigInteger

<press tab again to see documentation>

jshell> BigInteger

The above output shows the class synopsis (a short description). Entering Tab one more time will show the corresponding help documentation:

jshell> BigIntegerTab
java.math.BigInteger
Immutable arbitrary-precision integers.All operations behave as if BigIntegers
were represented in two's-complement notation (like Java's primitive integer
types). BigInteger provides analogues to all of Java's primitive integer
operators, and all relevant methods from java.lang.Math. Additionally,
BigInteger provides operations for modular arithmetic, GCD calculation,
primality testing, prime generation, bit manipulation, and a few other
miscellaneous operations.
Semantics of arithmetic operations exactly mimic those of Java's integer
arithmetic operators, as defined in The Java Language Specification . For
example, division by zero throws an ArithmeticException , and division of a
negative by a positive yields a negative (or zero) remainder. All of the details
in the Spec concerning overflow are ignored, as BigIntegers are made as large as
necessary to accommodate the results of an operation.
Semantics of shift operations extend those of Java's shift operators to allow
for negative shift distances. A right-shift with a negative shift distance
results in a left shift, and vice-versa. The unsigned right shift operator ( >>>
) is omitted, as this operation makes little sense in combination with the
"infinite word size" abstraction provided by this class.
Semantics of bitwise logical operations exactly mimic those of Java's bitwise

<press tab again to see next page>

jshell> BigInteger

The help contents are extracted from Java API doc. We can navigate (paginate) the help content further by pressing Tab more times.

Autocompletion for class members

After a valid class name, entering a dot (.) followed by a Tab key will show all declared static methods/variables of that class:

jshell> BigInteger.Tab
ONE TEN TWO ZERO
class probablePrime( valueOf(

jshell> BigInteger.

It works for variable references as well, we will see that shortly.

Let's narrow down to what we want to enter (say we are targeting TWO) :

jshell> BigInteger.TTab
TEN TWO

jshell> BigInteger.T
jshell> BigInteger.TWTabO
TWO

jshell> BigInteger.TWO

Now we are going to hit enter (notice that we have not used 'enter key' since the beginning)

jshell> BigInteger.TWOEnter
$1 ==> 2

The variable declaration of an Object

JShell provides a very convenient way to create an object. First we just need to initialize the object with new operator:

jshell> new BigITabnteger(
BigInteger(

jshell> new BigInteger(

Above expression can be transformed to a variable declaration. First provide the constructor parameter value and close the parenthesis. After that we need to press Shift+Tab keys simultaneously and then release them at the same time and then pressing the letter v:

jshell> new BigInteger("10")Shift + Tabv

Above action will automatically insert the left part:

jshell>BigInteger = new BigInteger("10")

Now we need to enter the variable name (where the cursor is) and hit the enter key:

jshell> BigInteger bIntEnter = new BigInteger("10")
bInt ==> 10

Auto completion for instance methods/variables

Let's use our variable 'bInt' which we created above. A Tab key followed by the dot will give us all the instance methods/variables list:

jshell> bInt.Tab
abs() add( and(
andNot( bitCount() bitLength()
byteValue() byteValueExact() clearBit(
compareTo( divide( divideAndRemainder(
doubleValue() equals( flipBit(
floatValue() gcd( getClass()
getLowestSetBit() hashCode() intValue()
intValueExact() isProbablePrime( longValue()
longValueExact() max( min(
mod( modInverse( modPow(
multiply( negate() nextProbablePrime()
not() notify() notifyAll()
or( pow( remainder(
setBit( shiftLeft( shiftRight(
shortValue() shortValueExact() signum()
sqrt() sqrtAndRemainder() subtract(
testBit( toByteArray() toString(
wait( xor(

jshell> bInt.

Say we want to use the method divide():

jshell> bInt.dTab
divide( divideAndRemainder( doubleValue()

jshell> bInt.d
jshell> bInt.diTabvide
divide( divideAndRemainder(

jshell> bInt.divide

Pressing tab after the left parentheses will show the method synopsis:

jshell> bInt.divide(Tab
$1 bInt

Signatures:
BigInteger BigInteger.divide(BigInteger val)

<press tab again to see documentation>

jshell> bInt.divide(

Pressing tab one more time, will show the help document:

jshell> bInt.divide(Tab
BigInteger BigInteger.divide(BigInteger val)
Returns a BigInteger whose value is (this / val) .

Parameters:
val - value by which this BigInteger is to be divided.

Returns:
this / val

Thrown Exceptions:
ArithmeticException - if val is zero.

<press tab again to see all possible completions; total possible completions: 546>

jshell> bInt.divide(

Let's enter a parameter value:

jshell> bInt.divide(BigITabnteger
jshell> bInt.divide(BigInteger
jshell> bInt.divide(BigInteger.TWTabO
TWO

jshell> bInt.divide(BigInteger.TWO
jshell> bInt.divide(BigInteger.TWO)Enter
$3 ==> 5

Auto Import

jshell> LocalTimeTab
jshell> LocalTime

In above output, no suggestion/help shown after Tab key, that's because LocalTime is not imported by default.

We can auto import a class by pressing Shift + Tab keys, then releasing them and then enter the letter i

jshell> LocalTimeShift + Tabi
0: Do nothing
1: import: java.time.LocalTime
Choice: 

The above output asks to enter a choice for the import. Let's enter 1:

jshell> LocalTime
0: Do nothing
1: import: java.time.LocalTime
Choice: 1
Imported: java.time.LocalTime

Now we can use LocalTime:

jshell> LocalTime.Tab
MAX MIDNIGHT MIN NOON
class from( now( of(
ofInstant( ofNanoOfDay( ofSecondOfDay( parse(

jshell> LocalTime.
jshell> LocalTime.now()Enter
$5 ==> 02:16:40.516568300

See Also