Angle Bracket <> in Java with Examples Different types of Recursions in Java Java Lambda Filter Example Java Program for Shopping Bill Pythagorean Triplet with Given Sum Using Single Loop in Java TemporalAdjusters lastInMonth() method in Java with Examples ZIP API in Java Atomic reference in Java Digit Extraction in Java DRY (Don't Repeat Yourself) Principles in Java with Examples Empty array in Java Is main method compulsory in Java? Java I/O Operation - Wrapper Class vs Primitive Class Variables Java Program to Find the Perimeter of a Rectangle Java Thread Priority Java Type Erasure Need of Concurrent Collections in Java Nested ArrayList in Java Print Odd and Even Numbers by Two Threads in Java Square pattern in Java TemporalAdjusters next() method in Java with Examples What does start() function do in multithreading in Java Convert Number to Words Problem in Java Detect And Remove Cycle in a Single Linked List in Java Evolution of Interfaces in Java How to pad a String in Java Implementing Patricia Trie in Java Java Program to Find the Most Repeated Word in a Text File java.util.UUID class in Java ReadWriteLock Interface in Java Reference Data Types in Java Sort An Array According to The Count of Set Bits in Java Alternate Vowel and Consonant string in Java Built-in Exceptions in Java with Examples Capture the Pawns Problem in Java Collections.shuffle() Method in Java with Examples JDBC MySQL Localhost 3306 Alternate Vowel and Consonant string in Java Built-in Exceptions in Java with Examples Capture the Pawns Problem in Java Collections.shuffle() Method in Java with Examples Convert Number to Words Problem in Java Detect And Remove Cycle in a Single Linked List in Java Evolution of Interfaces in Java How to pad a String in Java Implementing Patricia Trie in Java Java Program to Find the Most Repeated Word in a Text File java.util.UUID class in Java ReadWriteLock Interface in Java Reference Data Types in Java Sort An Array According to The Count of Set Bits in Java JDBC MySQL Localhost 3306 Adding a Character as Thousands Separator to Given Number in Java Circular Primes in Java Equilibrium Index Problem in Java Java String regionMatches() Method with Examples LFU Cache Problem in Java Longest Repeating and Non-Overlapping Substring in Java Prefix Suffix Search in Java Product Array Puzzle Problem in Java Russian Doll Envelopes Problem in Java Second Most Repeated Word in a Sequence in Java Special Two-Digit Numbers in a Binary Search Tree in Java Swap Corner Words and Reverse Middle Characters in Java Toggle K bits Problem in Java Upside Down Binary Tree in Java Verbal Arithmetic Puzzle Problem in Java Insert a String into another String in Java Print Shortest Path to Print a String on Screen in Java Search Insert Position in Java BST Sequences in Java Burrows - Wheeler Data Transform Algorithm in Java Convert BST to Min Heap in Java Fibonacci Sum in Java Get name of current method being executed in Java Keith number in Java Longest Even Length Substring in Java Saint-Exupery Numbers in Java Standard practice for protecting sensitive data in Java application Strobogrammatic number in Java Types of Methods in Java Programming Valid Number Problem in Java Boggle Search Problem in Java Convert Java Object to JSON String using Jackson API Generate Random String in Java Java Program to Determine the Unicode Code Point at Given Index in Strin Java 18 snippet tags Jumping Numbers in Java Junction Numbers in Java Find Four Elements that Sums to a given value in Java How to print string vertically in Java How to remove in string in Java Three-Way Partition Problem in Java Apocalyptic Number in Java Check if the given two matrices are mirror images of one another in Java Duplicate Characters Problem in Java Duplicate Parenthesis Problem in Java Sum of Minimum and Maximum Elements of all Subarrays of Size K in Java Triple Shift Operator in Java Valid Pairing of Numbers in Java Valid Sudoku problem in Java Java Cipher Class Kth Missing Positive Number in Java Largest Square Matrix Problem in Java Length of the longest substring without repeating characters in Java Minimum Cost to Make String Valid Problem in Java Ordered pair in Java Range Addition Problem in Java

Java I/O Operation - Wrapper Class vs Primitive Class Variables

Java's input/output (I/O) functions are essential for managing diverse kinds of data because they let us read and write to a variety of sources, including files, network connections, and standard input/output streams. There are instances in which handling both primitive and object forms of data is necessary while working with input and output in Java. Java offers two ways to make this easier: using primitive class variables directly or using wrapper classes.

Primitive Types in Java:

The predefined data types that the Java programming language offers are known as primitive-data types. Eight primitive types are present. They are as follows:-byte, double, boolean, short, int, long, float, and char.

A signed integer with 8 bits and 2's complement is stored in the byte data type. An integer with the sign of two and a size of 16 bits is stored in the short data type. 32-bit signed twos' complement integers are stored in int data types, whereas 64-bit signed twos' complement integers are stored in long data types. -A single precision 32-bit floating point value is stored in a float, whereas a double precision 64-bit floating point value is stored in a double. True or false is represented by the boolean. A single character is stored in the char.

The following are important factors to take into consideration while performing I/O operations with primitive data types:

Performance considerations:

When compared to wrapper classes, primitive data types perform better. They use less resources and have a smaller memory footprint for data storage and manipulation.

Direct Data Manipulation:

When you require precise control over the data, primitive data types let you deal directly with raw numbers. Without the burden of object-oriented operations, you can carry out bitwise operations, mathematical calculations, and other low-level manipulations.

Functionality Restrictions:

Primitive data types lack utility methods for tasks like formatting and number conversion, in contrast to wrapper classes. One could have to manually implement these functionalities when working with primitive types, or one might have to rely on assistance methods from other libraries.

Wrapper Classes:

Java wrapper classes for working with primitive data types, such as "Character," "Float," "Integer," and "Boolean," offer object-oriented capabilities. They give you the ability to use primitive types as objects and offer additional functions and methods that aren't possible with just raw data types.

Since wrapper classes are objects, they are initialized with the default value of "null" until a value is specifically given.

Here are some essential factors to consider about when performing I/O operations with wrapper classes:

Boxing and Unboxing:

Using boxing and Unboxing, wrapper classes make it easier to transition between primitive types and objects. A primitive value is wrapped within the corresponding wrapper class object in boxing, and the primitive value is extracted from the wrapper object in Unboxing. This enables you to do I/O operations that call for objects using primitive types.

Utility Methods:

For a variety of operations on the matching primitive types, wrapper classes provide utility methods. The Integer class, for instance, offers functions for handling number formatting, executing mathematical operations, and converting texts to numbers.

Interoperability with Collections and Generics:

In situations where collections and generics are involved, wrapper classes are essential. Working with primitive types within generic classes and collections is made possible by the use of wrapper classes, as Java's generics can only handle reference types. This lets users to-handle various data types in I/O operations and leverage the power of generics.

Wrapper Class to Primitive Data Type:

Java wrapper classes provide a means of converting between primitive data types and the objects that correspond to them. Unboxing is the process of converting anything to make it easily interchangeable and to provide you access to the primitive value inside the wrapper class object.

In the below code, it-uses autoboxing to set a 'Double' wrapper class object's value to 3.14 and unboxing to return the wrapper object to a basic 'double'.

Implementation:

FileName: WrapperToPrimitive.java

import java.io.*;

import java.util.*;

public class WrapperToPrimitive

{

   public static void main(String[] args)

   {

      // Autoboxing: value of wrapper class

      Double wval = 3.14;       

      //Unboxing: convert to double

      double pval = wval;       

      System.out.println("The Primitive Value is given by: " + pval);

   }

}

Output:

The Primitive Value is given by: 3.14

Primitive Data Type to Wrapper Class:

Java supports autoboxing, which is the translation of primitive data types into their equivalent wrapper classes. By allowing the direct assignment of primitive values to wrapper class objects, this automatic conversion facilitates actions that call for objects rather than primitives, hence simplifying the code.

In the below code, it-sets the 'primitiveValue' to the boolean primitive value 'true'. Next, a ''Boolean' wrapper class object named 'wval' is created from this raw value using autoboxing.

Implementation:

FileName: PrimitiveToWrapper.java

import java.io.*;

import java.util.*;

public class PrimitiveToWrapper

{

   public static void main(String[] args)

   {

      // Value of primitive data type

      boolean pval = true;       

      // Autoboxing: convert to Boolean

      Boolean wval = Boolean.valueOf(pval);       

      System.out.println("The Wrapper Value is given by: " + wval);

   }

}

Output:

The Wrapper Value is given by: true

Wrapper class vs Primitive Class differences:

Wrapper ClassPrimitive Class
An object can be transformed into a primitive type and an object into an object using the wrapper class.A Java predefined data type is called a primitive type. -
There is a comparable class for a Wrapper class since it is used to generate objects.Since a primitive type is not an object, it is not a member of any class.
Null values are accepted by the wrapper class objects.Null values cannot exist in a primitive data type.
Compared to the primitive types, more memory is needed. No extra space is needed for the Clustered Index.In contrast to wrapper classes, less memory is needed.
Collections like ArrayList and others can be utilised with a Wrapper class.Collections don't use primitive types.