Home > Java > javaTutorial > body text

How to implement integer type conversion in JShell in Java 9?

WBOY
Release: 2023-09-13 17:09:03
forward
1167 people have browsed it

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

JShell is a command line interactive tool introduced in Java 9 version, allowing programmers to execute simple statements, expressions, variables, methods, classes , interface, etc.. No need to declare the main() method.

In JShell, the compiler warns the programmer about type conversion issues by throwing errors. However, if the programmer is aware of this, explicit conversion will be required. If we need to store a smaller data value into a larger type conversion, implicit conversion is required.

There are two kinds of integerType conversion:

  • ##Literal to variable assignment: For example,Shorts1 = 123456, the data is out of range. It is known at compile time, and the compiler will flag the error.
  • Variable to variable assignment: For example, s1 = i1. The value stored in int at this stage is: 4567, which is completely within the range of the short type, and the compiler will not throw any errors. It can be preempted by explicit conversion s1 = (short) i1.
In the following code snippet, we can implement implicit conversion and explicit type conversion.

<strong>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</strong>
Copy after login

The above is the detailed content of How to implement integer type conversion in JShell in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template