Java Tutorial
- Java - Home
- Java - Overview
- Java - History
- Java - Features
- Java vs C++
- Java Virtual Machine (JVM)
- Java - JDK vs JRE vs JVM
- Java - Hello World Program
- Java - Environment Setup
- Java - Basic Syntax
- Java - Variable Types
- Java - Data Types
- Java - Type Casting
- Java - Unicode System
- Java - Basic Operators
- Java - Comments
Java Control Statements
- Java - Loop Control
- Java - Decision Making
- Java - If-else
- Java - Switch
- Java - For Loops
- Java - For-Each Loops
- Java - While Loops
- Java - do-while Loops
- Java - Break
- Java - Continue
Object Oriented Programming
- Java - OOPs Concepts
- Java - Object & Classes
- Java - Class Attributes
- Java - Class Methods
- Java - Methods
- Java - Variables Scope
- Java - Constructors
- Java - Access Modifiers
- Java - Inheritance
- Java - Aggregation
- Java - Polymorphism
- Java - Overriding
- Java - Method Overloading
- Java - Dynamic Binding
- Java - Static Binding
- Java - Instance Initializer Block
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java - Inner Classes
- Java - Static Class
- Java - Anonymous Class
- Java - Singleton Class
- Java - Wrapper Classes
- Java - Enums
- Java - Enum Constructor
- Java - Enum Strings
Java Built-in Classes
Java File Handling
- Java - Files
- Java - Create a File
- Java - Write to File
- Java - Read Files
- Java - Delete Files
- Java - Directories
- Java - I/O Streams
Java Error & Exceptions
- Java - Exceptions
- Java - try-catch Block
- Java - try-with-resources
- Java - Multi-catch Block
- Java - Nested try Block
- Java - Finally Block
- Java - throw Exception
- Java - Exception Propagation
- Java - Built-in Exceptions
- Java - Custom Exception
Java Multithreading
- Java - Multithreading
- Java - Thread Life Cycle
- Java - Creating a Thread
- Java - Starting a Thread
- Java - Joining Threads
- Java - Naming Thread
- Java - Thread Scheduler
- Java - Thread Pools
- Java - Main Thread
- Java - Thread Priority
- Java - Daemon Threads
- Java - Thread Group
- Java - Shutdown Hook
Java Synchronization
- Java - Synchronization
- Java - Block Synchronization
- Java - Static Synchronization
- Java - Inter-thread Communication
- Java - Thread Deadlock
- Java - Interrupting a Thread
- Java - Thread Control
- Java - Reentrant Monitor
Java Networking
- Java - Networking
- Java - Socket Programming
- Java - URL Processing
- Java - URL Class
- Java - URLConnection Class
- Java - HttpURLConnection Class
- Java - Socket Class
- Java - Generics
Java Collections
Java Interfaces
- Java - List Interface
- Java - Queue Interface
- Java - Map Interface
- Java - SortedMap Interface
- Java - Set Interface
- Java - SortedSet Interface
Java Data Structures
Java Collections Algorithms
Advanced Java
- Java - Command-Line Arguments
- Java - Lambda Expressions
- Java - Sending Email
- Java - Applet Basics
- Java - Javadoc Comments
- Java - Autoboxing and Unboxing
- Java - File Mismatch Method
- Java - REPL (JShell)
- Java - Multi-Release Jar Files
- Java - Private Interface Methods
- Java - Inner Class Diamond Operator
- Java - Multiresolution Image API
- Java - Collection Factory Methods
- Java - Module System
- Java - Nashorn JavaScript
- Java - Optional Class
- Java - Method References
- Java - Functional Interfaces
- Java - Default Methods
- Java - Base64 Encode Decode
- Java - Switch Expressions
- Java - Teeing Collectors
- Java - Microbenchmark
- Java - Text Blocks
- Java - Dynamic CDS archive
- Java - Z Garbage Collector (ZGC)
- Java - Null Pointer Exception
- Java - Packaging Tools
- Java - Sealed Classes
- Java - Record Classes
- Java - Hidden Classes
- Java - Pattern Matching
- Java - Compact Number Formatting
- Java - Garbage Collection
- Java - JIT Compiler
Java Miscellaneous
- Java - Recursion
- Java - Regular Expressions
- Java - Serialization
- Java - Strings
- Java - Process API Improvements
- Java - Stream API Improvements
- Java - Enhanced @Deprecated Annotation
- Java - CompletableFuture API Improvements
- Java - Streams
- Java - Datetime Api
- Java 8 - New Features
- Java 9 - New Features
- Java 10 - New Features
- Java 11 - New Features
- Java 12 - New Features
- Java 13 - New Features
- Java 14 - New Features
- Java 15 - New Features
- Java 16 - New Features
Java APIs & Frameworks
Java Class References
- Java - Scanner Class
- Java - Arrays Class
- Java - Strings
- Java - Date & Time
- Java - ArrayList
- Java - Vector Class
- Java - Stack Class
- Java - PriorityQueue
- Java - LinkedList
- Java - ArrayDeque
- Java - HashMap
- Java - LinkedHashMap
- Java - WeakHashMap
- Java - EnumMap
- Java - TreeMap
- Java - The IdentityHashMap Class
- Java - HashSet
- Java - EnumSet
- Java - LinkedHashSet
- Java - TreeSet
- Java - BitSet Class
- Java - Dictionary
- Java - Hashtable
- Java - Properties
- Java - Collection Interface
- Java - Array Methods
Java Useful Resources
Java - REPL (JShell)
Introduction to REPL (JShell)
REPL stands for Read Evaluate Print Loop. JShell was introduced in Java 9 which is an interactive console. JShell as REPL allows to run arbitrary snippet of java code in console without need to save and compile java code file. This facility is very important to test codes quickly like evaluating regular expression, checking formating of strings, date formats etc.
JShell reads each line entered, evaluates it and then print the result and then again becomes ready for next set of input.
Advantages of Using JShell
This capability of JShell gives developers following advantages -
No editor is needed to write a Java program. JShell itself works as editor and executes the Java code.
Using JShell, there is no requirement to save a Java file, compile and execute code cycle. Code can be directly tested in JShell without saving anything.
Compilation is not needed prior to execution of code.
If any compile-time or runtime error occurs, start fresh.
Running JShell
Open command prompt and type jshell.
D:\test>jshell | Welcome to JShell -- Version 20.0.2 | For an introduction type: /help intro
With JShell, we can test methods, classes, expressions as well. In following examples, let's explore some of the features of JShell.
Create and Invoke Method in JShell
Following snippet showing a sample "Hello World" program in JShell. Here, we've created a method greet() which has a single statement to print a message as "Hello World!". As next, we've invoked the method greet() and result is printed on the console.
Hello World Example in JShell
jshell> void greet() { System.out.println("Hello World!");} | created method greet() jshell> greet() Hello World! jshell>
Creating Variables in JShell
Following snippet shows how to create variables in JShell. semi-colon is optional. We can create objects as well in JShell. If a variable is not initialized then it is given a default value or null if it is an object reference. Once a variable is created, it can be used as shown in the last statement where we've used the string variable to print its value.
Example
jshell> int i = 10 i ==> 10 jshell> String name = "Mahesh"; name ==> "Mahesh" jshell> Date date = new Date() date ==> Fri Feb 02 14:52:49 IST 2024 jshell> long l l ==> 0 jshell> List list list ==> null jshell> name name ==> "Mahesh"
Evaluate Expression in JShell
Following snippet shows how to evaluate an expression using JShell. Here we've passed a statement which returns a formatted string. JShell automatically created a String variable $9 and assigned it the result. As next statement, we've printed.
Example
jshell> String.format("%d pages read.", 10); $9 ==> "10 pages read." jshell> $9 $9 ==> "10 pages read."
Jshell Built-In Commands
JShell provides various commands to list the variables created, methods created, imports used etc. Some of the important JShell commands are -
- /drop – This command drops code snippets identified by name, ID, or ID range.
- /edit – This command opens an editor.
- /env – This command displays the environment settings.
- /exit – This command exists from the tool.
- /history – This command displays the history of the tool.
- /help – This command displays the command's help.
- /imports – This command displays the current active imports.
Example: Demonstrating /help Command
We can view all commands using /help option.
jshell> /help | Type a Java language expression, statement, or declaration. | Or type one of the following commands: | /list [<name or id>|-all|-start] | list the source you have typed | /edit <name or id> | edit a source entry | /drop <name or id> | delete a source entry | /save [-all|-history|-start] <file> | Save snippet source to a file | /open <file> | open a file as source input | /vars [<name or id>|-all|-start] | list the declared variables and their values | /methods [<name or id>|-all|-start] | list the declared methods and their signatures | /types [<name or id>|-all|-start] | list the type declarations ...
Example: Demonstrating /vars Command
In following example, we've used /vars command to print the variables declared during a session.
C:\Users\Mahesh>jshell | Welcome to JShell -- Version 20.0.2 | For an introduction type: /help intro jshell> int i = 10 i ==> 10 jshell> String name="Mahesh" name ==> "Mahesh" jshell> /vars | int i = 10 | String name = "Mahesh" jshell>
Example: Demonstrating /imports Command
We can use /imports command to check the imports available in JShell as shown below:
jshell> /imports | import java.io.* | import java.math.* | import java.net.* | import java.nio.file.* | import java.util.* | import java.util.concurrent.* | import java.util.function.* | import java.util.prefs.* | import java.util.regex.* | import java.util.stream.* jshell>
Exiting JShell
We can use /exit command to exit JShell as shown below:
Example
jshell> /exit | Goodbye C:\Users\Mahesh>