Skip to toolbar

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:


3055total visits,12visits today

Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 9
  • Page views today : 19
  • Total visitors : 48,395
  • Total page view: 66,587

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.