jshell prints the value of the variable for "var declaration" snippets.
$ jshell
| Welcome to JShell -- Version 19
| For an introduction type: /help intro
jshell> import java.lang.reflect.*;
jshell> var ba = Array.newInstance(byte.class, 10)
ba ==> byte[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
jshell> var s = "hello"
s ==> "hello"
It may be useful to print inferred type and the value for "var" declarations.
Much like the output from /vars command
jshell> /vars ba
| Object ba = byte[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
jshell> /vars s
| String s = "hello"
Without the inferred type, user may sometimes get confused about the type of the var (inferred type of the var vs runtime type of the value). S/he may think "I've got a byte array here and I can pass to a method expecting byte[] "
jshell> var ba = Array.newInstance(byte.class, 10)
ba ==> byte[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
jshell> new String(ba)
| Error:
| no suitable constructor found for String(java.lang.Object)
| constructor java.lang.String.String(java.lang.String) is not applicable
| (argument mismatch; java.lang.Object cannot be converted to java.lang.String)
| constructor java.lang.String.String(char[]) is not applicable
| (argument mismatch; java.lang.Object cannot be converted to char[])
| constructor java.lang.String.String(byte[]) is not applicable
| (argument mismatch; java.lang.Object cannot be converted to byte[])
| constructor java.lang.String.String(java.lang.StringBuffer) is not applicable
| (argument mismatch; java.lang.Object cannot be converted to java.lang.StringBuffer)
| constructor java.lang.String.String(java.lang.StringBuilder) is not applicable
| (argument mismatch; java.lang.Object cannot be converted to java.lang.StringBuilder)
| new String(ba)
| ^------------^
I'm filing this enhancement based on the following github interaction with Bruno Borges.
https://gist.github.com/brunoborges/2cace368fd01ebb7bbfcdc4c8fa10d80
https://gist.github.com/brunoborges/2cace368fd01ebb7bbfcdc4c8fa10d80?permalink_comment_id=4316538#gistcomment-4316538
$ jshell
| Welcome to JShell -- Version 19
| For an introduction type: /help intro
jshell> import java.lang.reflect.*;
jshell> var ba = Array.newInstance(byte.class, 10)
ba ==> byte[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
jshell> var s = "hello"
s ==> "hello"
It may be useful to print inferred type and the value for "var" declarations.
Much like the output from /vars command
jshell> /vars ba
| Object ba = byte[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
jshell> /vars s
| String s = "hello"
Without the inferred type, user may sometimes get confused about the type of the var (inferred type of the var vs runtime type of the value). S/he may think "I've got a byte array here and I can pass to a method expecting byte[] "
jshell> var ba = Array.newInstance(byte.class, 10)
ba ==> byte[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
jshell> new String(ba)
| Error:
| no suitable constructor found for String(java.lang.Object)
| constructor java.lang.String.String(java.lang.String) is not applicable
| (argument mismatch; java.lang.Object cannot be converted to java.lang.String)
| constructor java.lang.String.String(char[]) is not applicable
| (argument mismatch; java.lang.Object cannot be converted to char[])
| constructor java.lang.String.String(byte[]) is not applicable
| (argument mismatch; java.lang.Object cannot be converted to byte[])
| constructor java.lang.String.String(java.lang.StringBuffer) is not applicable
| (argument mismatch; java.lang.Object cannot be converted to java.lang.StringBuffer)
| constructor java.lang.String.String(java.lang.StringBuilder) is not applicable
| (argument mismatch; java.lang.Object cannot be converted to java.lang.StringBuilder)
| new String(ba)
| ^------------^
I'm filing this enhancement based on the following github interaction with Bruno Borges.
https://gist.github.com/brunoborges/2cace368fd01ebb7bbfcdc4c8fa10d80
https://gist.github.com/brunoborges/2cace368fd01ebb7bbfcdc4c8fa10d80?permalink_comment_id=4316538#gistcomment-4316538