QnA | Difference between Integer and int keywords.

Get prepared for the Java.

The basic difference between Integer and int is that Integer is a Class whereas int is a data type.

int Data Type

We already know that int is a primitive data type. It means that these data types are not further breakable. Any variable declared with int data type can store only numeric values in it.

Integer Class

On the other hand, Integer refers to a Class which wraps a value of int type in an object. In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.

Below code snippet will give the clear picture of this difference.

/**
 * @author ashok.kumar
 *
 */
public class Example {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println("Creating variable of int data type:");
		int value=5;
		System.out.println(value);
		
		// Creating object of Integer Class 
		Integer obj=new Integer(2);
		
		// Comparing two integer values using Integer methods.
		// Returns 0, 1 and -1 based on the comparing result.
		System.out.println("\nResult of compareTo method of Integer object:");
		System.out.println(obj.compareTo(value));
		
		System.out.println("\nAdding values in the Integer Object directly:");
		System.out.println(obj+10);
		
		// Converted Integer value into String and added values into it.
		System.out.println("\nAdding values in the Integer Object after converted into the String:");
		System.out.println(obj.toString()+10);
		
		// Converting int value into Binary value.
		System.out.println("\nBinary value of "+value+" is");
		System.out.println(obj.toBinaryString(value));
	}
}
Console Output using Integer and int.

Other Methods

  • SIZE: number of bits used to represent int value into binary form.
  • compareTo(Integer i): Used to compare an integer value with another int value.
  • toString(): Converts integer value into the String.
  • toBinaryString(int i): Returns the binary value of the given int value.
  • compare(int x, int y): Used to compare two int values.
  • floatValue(): Coverts integer value into Float value.
  • max(int a, int b): Compares two int value and returns the maximum value.
  • min(int a, int b): Compares two int value and returns the minimum value.

Comparison between Integer and int

intInteger
Primitive data type and can store only numeric values.Wrapper class of int data type.
No support for other methods.There are multiple methods present to manipulate values.
Can store values directly.We can store values only while creating an object of Integer class.
Direct conversion of the values in other bases like binary, octal, etc.Direct conversion is possible using in-built methods.

Related Links:

TestNG:

Java Basics:

Java File Handling:

OOPs Concept:

Computer Basics:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


960total visits,24visits today

Leave a Reply

Your email address will not be published. Required fields are marked *