
- 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 define expressions, variables, and methods in JShell in Java 9?
JShell is a Read-Evaluate-Print Loop (REPL) that evaluates declarations, statements, and expressions as we have entered and immediately shows the results. This tool is run from the command prompt.
In the below, we can define expressions, variables, and methods in JShell.
Expression
We can type any valid Java expression in JShell. The expression is either an arithmetic operation, string manipulation, and method call and evaluates immediately. All the results automatically assigned to a variable created by JShell. These variables have prefixed with $ symbol.
Example
jshell> 10 * 5 $1 ==> 50 jshell> 77 % 3 $2 ==> 2 jshell> $1 + $2 $3 ==> 52 jshell>
Variable
The Variables defined in JShell are the same as defined in a Java program. Once a variable is defined, it is present in the scope.
Example
jshell> String str = "Tutorialspoint" str ==> "Tutorialspoint" jshell> str str ==> "Tutorialspoint" jshell>
Method
We can define methods in JShell the same as how we can define in Java classes. Once a method has created in a JShell session, we can call it anytime until quitting that session.
Example
jshell> int sum(int x, int y) { ...> return x + y; ...> } | created method sum(int,int) jshell> sum(10,20) $2 ==> 30 jshell>
- Related Articles
- How to create scratch variables in JShell in Java 9?
- Using Variables in JShell of Java 9
- How to define control flow statements in JShell in Java 9?
- How to define a switch statement in JShell in Java 9?
- How to debug JShell in Java 9?
- JShell in Java 9?
- How to get JShell documentation in Java 9?
- How to create a class and object in JShell in Java 9?
- How to get the date and time 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 to create JShell instance programmatically in Java 9?
- How to reset the JShell session in Java 9?
- How to implement java.time.LocalDate using JShell in Java 9?
