
- 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 declare reference types in JShell in Java 9?
JShell is an interactive tool in Java 9 that allows user inputs, evaluates it, and prints output to the user.
Unlike a value type, a reference type doesn't store its value directly. Instead, it will store the address where a value is stored. It means that a reference type contains a pointer to another memory location that holds the data. The reference types are String, arrays, class, and delegates.
In the below code snippet, when we create a new instance of Animal, it can be created on the heap memory. The new Animal() creates an object on the Heap. Animal@73846619, the object is stored on the Heap at address 73846619.
Snippet-1
jshell> class Animal { ...> } | created class Animal jshell> Animal dog = new Animal(); dog ==> Animal@73846619
In the below code snippet, two new Animal objects are created on the Heap. Their memory locations (references) are stored in the reference variables dog and cat. In Java, all classes are Reference Types. Except for primitive variable instances, all the instances or objects are stored on the Heap. The references to the objects are stored in the reference variables like dog and cat.
Snippet-2
jshell> class Animal { ...> int id; ...> public Animal(int id) { ...> this.id = id; ...> } ...> } | created class Animal jshell> Animal dog = new Animal(10); dog ==> Animal@6adede5 jshell> Animal cat = new Animal(20); cat ==> Animal@5025a98f
In the below code snippet, "j = I" copies the value of "I" into "j". Later, when the value of "j" is changed, "I" is not affected. By using primitive variables, compares their values.
Snippet-3
jshell> int i = 5; i ==> 5 jshell> int j; j ==> 0 jshell> j = i; j ==> 5 jshell> j = 10; j ==> 10 jshell> i; i ==> 5 jshell> i == j; $11 ==> false jshell> j = 5; j ==> 5 jshell> i == j; $13 ==> true
- Related Articles
- How to declare a class and an interface in JShell in Java 9?
- What is a forward reference in JShell in Java 9?
- How to debug JShell in Java 9?
- JShell in Java 9?
- What are the different "/types" commands in JShell in Java 9?
- How to get JShell documentation 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?
- How to create a thread in JShell in Java 9?
- How to import external libraries in JShell in Java 9?
- How to handle an exception in JShell in Java 9?
- How to create scratch variables in JShell in Java 9?
- How to implement a String in JShell in Java 9?
- How to create wrapper objects in JShell in Java 9?
