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 Class | Primitive 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. |