Open In App

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

It is better to use the Primitive Class variable for the I/O operation unless there is a necessity of using the Wrapper Class. In this article, we can discuss briefly both wrapper class and primitive data type.

Primitive Data types

Default values

int 

0

byte 

0

short 

0

char 

\u0000 or null

boolean

false

double 

0.0

float 

0.0




/*package whatever //do not write package name here */
  
import java.io.*;
class GFG {
    public static void main(String[] args)
    {
        // primitive data type
        int n = 15;
  
        // Creating object from primitive data type
        Integer obj = Integer.valueOf(n);
        System.out.println(n + " " + obj);
    }
}

Output:



15 15

Primitive data Type 

Wrapper Class

Even Large scale calculations can be made faster When collections are used, a wrapper class is required
Methods will return a value Methods will return null
mostly used when the variable should not be null or ‘\0’ Used when the variable should be null




/*package whatever //do not write package name here */
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // wrapper class to primitive datatype
        Integer a = new Integer(6);
        // Unboxing
        int b = a;
        System.out.print(b);
    }
}

Output:

6




/*package whatever //do not write package name here */
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        // converting primitive data type to wrapper class
        int a = 6;
        // Autoboxing
        Integer b = Integer.valueOf(a);
        System.out.println(b);
    }
}

Output:



6

Article Tags :