Compare the Difference Between Similar Terms

Difference Between Integer and Float

Key Difference – Integer vs Float

Float and Double are other wrapper classes that are used for converting primitive data types. Sometimes it is required to convert the primitive data type to an object and to convert the object to the primitive data type. For that, Wrapper classes can be used. Programming languages such as Java contains Wrapper classes. They are used for this conversion process. A wrapper class is a class that encapsulates types. Those types can be used to create object instances and methods in another class that needs those types. There are eight primitive types in Java. They are int, short, byte, long, boolean, char, float and double. The corresponding wrapper class for the boolean data type is Boolean. The wrapper class for char data type is a character. Short, Byte, Integer, Long, Float and Double are other wrapper classes. Converting the primitive data type to an object automatically is called autoboxing. Converting the object to a primitive type automatically is called unboxing. This article discusses two wrapper classes that are Integer and Float. The key difference between Integer and Float is that Integer is the wrapper class related to int primitive data type while Float is the wrapper class related to float primitive data type.

CONTENTS

1. Overview and Key Difference
2. What is Integer
3. What is Float
4. Similarities Between Integer and Float
5. Side by Side Comparison – Integer vs Float in Tabular Form
6. Summary

What is Integer?

Integer is a wrapper class in Java. The corresponding data type is int. It is used to convert an int data type to an object or to convert an object to an int. Refer the below example with Integer wrapper class.

Figure 01: Program with Integer Wrapper Class

According to the above program, the x is a variable of type int. It contains the value 10. The Integer.valueOf is used to convert the int to Integer type object. The x variable is passed to the value of the method. Likewise, the int is converted to an Integer.

The y is an object of type Integer. Value 5 is passed to the constructor. Using intValue method, that object is converted to int data type. That converted value is stored into z variable that can hold an int.

Figure 02: Autoboxing and Unboxing Example1

According to the above program, the variable x has an int. When assigning it to Integer, the compiler automatically writes Integer.valueOf(x) internally. That is auto boxing. The ‘a’ is of type Integer. The value 6 is passed to the constructor. When assigning the a value to b, the compiler automatically writes a.intValue() internally. That is unboxing.

What is Float?

Float is a wrapper class in Java. The corresponding data type is a float. It is used to convert a float data type to an object or to convert an object to a float. Refer the below example with Float wrapper class.

Figure 03: Program with Float Wrapper Class

According to the above program, the x is a variable of type float. It contains the value 20.5f. The Float.valueOf is used to convert the float to Float type object. The x variable is passed to the valueOf method. Likewise, the float is converted to a Float.

The y is an object of type Float. Value 10.5f is passed to the constructor. Using floatValue method, that object is converted to float data type. That converted value is stored into z variable that can hold a float value.

Figure 04: Autoboxing and Unboxing Example2

According to the above program, the variable x has a float. When assigning it to Float, the compiler automatically writes Float.valueOf(x) internally. That is autoboxing. The ‘a’ is of type Float. The value 6.1f is passed to the constructor. When assigning the a value to b, the compiler automatically writes a.floatValue() internally. That is unboxing.

What is the Similarity Between Integer and Float?

What is the Difference Between Integer and Float?

Integer vs Float

Integer is a class that wraps a value of the primitive type int in an object. Float is a class that wraps a value of the primitive type float in an object.
Related Primitive Data Type
Integer is the wrapper class related to int data type. Float is the wrapper class related to float data type.

Summary – Integer vs Float

There are eight major primitive types in Java. They are int, short, byte, long, boolean, char, float and double. Sometimes it is necessary to convert the primitive data types to object and object to primitive types. Wrapper classes are used for that. Each primitive type has a corresponding wrapper class. Those wrapper classes are Integer, Short, Byte, Long, Boolean, Char, Float, and Double. This article discussed the difference between Integer and Float. The difference between Integer and Float is that Integer is the wrapper class related to int primitive data type while Float is the wrapper class related to float primitive data type.

Reference:

1.“Wrapper class in java – Javatpoint.”, Java Point. Available here
2.Integer (Java SE 9 & JDK 9). Available here
3.Float (Java SE 9 & JDK 9). Available here