
- 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 create wrapper objects in JShell in Java 9?
Each primitive type in Java has a corresponding built-in wrapper class, and these wrapper classes are also immutable. Integer, Float, Double, Byte, and etc.. are some of the built-in wrapper classes. The main incentive of using such wrappers in our code is accessing type information about the corresponding primitive type, Auto-Boxing feature, where a primitive data is automatically promoted to an object reference type, and moving primitive type data around data structures.
We can create an instance of Wrapper Classes by using a new operator, and also use the valueOf() method within types such as Integer to create a wrapper object. The Integer.valueOf() method will reuse existing Integer objects with the same value on the heap. If an object with the same value is present in the heap, it will return a reference to an existing object, or else returns the reference of the newly created Integer object.
In the below code snippets, we can able to create wrapper objects in JShell tool.
Snippet-1
jshell> Integer int1 = new Integer(10); int1 ==> 10 jshell> Integer int2 = new Integer(10); int2 ==> 10 jshell> Integer int3 = Integer.valueOf(10); int3 ==> 10 jshell> Integer int4 = Integer.valueOf(10); int4 ==> 10 jshell> int1 == int2; $7 ==> true jshell> int3 == int4; $8 ==> true
Snippet-2
jshell> Integer abc1 = Integer.valueOf(700); abc1 ==> 700 jshell> Integer abc2 = 700; abc2 ==> 700 jshell> Integer abc3 = 700; abc3 ==> 700 jshell> abc2 == abc3 $4 ==> false jshell> Integer.MAX_VALUE $5 ==> 2147483647 jshell> Integer.MIN_VALUE $6 ==> -2147483648 jshell> Integer.SIZE $7 ==> 32 jshell> Integer.BYTES $8 ==> 4
- Related Articles
- How to create JShell instance programmatically in Java 9?
- How to create scratch variables in JShell in Java 9?
- How to create a thread in JShell in Java 9?
- How to create a class and object 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 reset the JShell session in Java 9?
- How to implement java.time.LocalDate using 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 implement a String in JShell in Java 9?
- How to initialize an array in JShell in Java 9?
- How to declare reference types in JShell in Java 9?
- How to implement an ArrayList using JShell in Java 9?
