您好,欢迎访问一九零五行业门户网

如何在Java 9的JShell中实现整数类型转换?

jshell 是java 9版本中引入的命令行交互工具,允许程序员执行简单的语句、表达式、变量、方法、类、接口等.. 无需声明 ma​​in() 方法。
在 jshell 中,编译器通过抛出错误来警告程序员有关类型转换问题。但是,如果程序员意识到这一点,则需要显式转换。如果我们需要将较小的数据值存储为较大的类型转换,则需要隐式转换。
有两种各种整数类型转换:
文字到变量赋值: 例如,短s1 = 123456,数据超出范围。它在编译时已知,并且编译器会标记错误。变量到变量赋值:例如,s1 = i1 。该阶段int存储的值为:4567,完全在short类型的范围之内,编译器不会抛出任何错误。可以通过显式转换 s1 = (short) i1 来抢占它。在下面的代码片段中,我们可以实现隐式转换和显式类型转换。
c:\users\user>jshell| welcome to jshell -- version 9.0.4| for an introduction type: /help introjshell> 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 = 3456s1 ==> 3456jshell> int i1 = 4567;i1 ==> 4567jshell> s1 = i1;| error:| incompatible types: possible lossy conversion from int to short| s1 = i1;| ^^jshell> s1 = (short) i1;s1 ==> 4567jshell> int num = s1;num ==> 4567
以上就是如何在java 9的jshell中实现整数类型转换?的详细内容。
其它类似信息

推荐信息