JShell is a command-line interactive tool introduced in Java 9 version that allows the programmer to execute simple statements, expressions, variables, methods, classes, interfaces, etc.. without declaring the main() method.
In JShell, the compiler warns the programmer about typecasting issues by throwing errors. However, if the programmer is aware of it, then explicit casting will be required. If we need to store a smaller data value into a larger type conversion, then implicit casting will be required.
There are two kinds of integer typecasting:
In the below code snippet, we can implement both implicit and explicit type conversions.
C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> byte b = 128; | Error: | incompatible types: possible lossy conversion from int to byte | byte b = 128; | ^-^ jshell> short s = 123456; | Error: | incompatible types: possible lossy conversion from int to short | short s = 123456; | ^----^ jshell> short s1 = 3456 s1 ==> 3456 jshell> int i1 = 4567; i1 ==> 4567 jshell> s1 = i1; | Error: | incompatible types: possible lossy conversion from int to short | s1 = i1; | ^^ jshell> s1 = (short) i1; s1 ==> 4567 jshell> int num = s1; num ==> 4567