
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to implement integer type conversion in JShell in Java 9?
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:
- Literal-to-variable assignment: For instance, short s1 = 123456, the data is out of range. It is known at compile-time, and the compiler flags an error.
- Variable-to-variable assignment: For instance, s1 = i1. The value stored in int at that stage: 4567, which is well within the range of the short type, and the compiler doesn't throw any error. It can be pre-empted with an explicit casting s1 = (short) i1.
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
- Related Articles
- How to implement java.time.LocalDate using JShell in Java 9?
- How to implement a String in JShell in Java 9?
- How to implement an ArrayList using JShell in Java 9?
- How to implement JShell using JavaFX in Java 9?\n
- How to implement the encapsulation concept in JShell in Java 9?
- How to implement a lambda expression in JShell in Java 9?
- How to implement the Fibonacci series in JShell in Java 9?
- How to implement a Set interface in JShell in Java 9?
- How to implement relational and logical operators in JShell in Java 9?
- How to implement String utility and immutability in JShell in Java 9?
- How to implement HashMap, LinkedHashMap, and TreeMap in JShell in Java 9?
- How can we implement a map in JShell in Java 9?
- How to debug JShell in Java 9?
- JShell in Java 9?
- How to get JShell documentation in Java 9?

Advertisements