int Data Type in Java: Definition and Usage Explained

Rumman Ansari   Software Engineer   2024-07-04 03:17:02   9805  Share
Subject Syllabus DetailsSubject Details 1 Program
☰ TContent
☰Fullscreen

Table of Content:

int Data Type in Java

Java defines four integer types: byte, short, int, and long. All of these are signed, positive and negative values. Java does not support unsigned, positive-only integers. The most commonly used integer type is int. It is a 32-bit signed two’s complement integer.

  • Int data type is a 32-bit signed two's complement integer.

  • Minimum value is - 2,147,483,648 (-2^31)

  • Maximum value is 2,147,483,647(inclusive) (2^31 -1)

  • Integer is generally used as the default data type for integral values unless there is a concern about memory.

  • The default value is 0

  • variables of type int are commonly employed to control loops and to index arrays.

  • Example:

     int a = 100000, int b = -200000 


Data TypeSize(Byte)Range
int4–2,147,483,648 to 2,147,483,647

int Variable Declaration and Variable Initialization:

Variable Declaration : To declare a variable , you must specify the data type & give the variable a unique name.

 byte age;  

Variable Initialization : To initialize a variable you must assign it a valid value.

 age= 2 ;  

int Variable Declaration and Variable Initialization in two steps:

byte age;
age = 20;
int data type in java

Example :

Save Source File Name as : IntExample1.java
To compile : javac IntExample1.java
To Run : java IntExample1
// A Java program to demonstrate int data type
class IntExample1 {
   public static void main(String args[])
     {
           int age;
           age = 20;
           System.out.println(age);
     }
  }
 
Output
 20
 press any key to continue ...
 
int data type in java

int Variable Declaration and Variable Initialization in one line:

int age = 20;
int data type in java

Example :

Save Source File Name as : IntExample1.java
To compile : javac IntExample1.java
To Run : java IntExample1
// A Java program to demonstrate int data type
class IntExample1 {
   public static void main(String args[])
     {
           int age = 20;
           System.out.println(age);
     }
  }
Output
20
press any key to continue ...

Example Addition using int

// Java program to demonstrate int data type in Java
class IntExample2
{
  public static void main(String args[])
    {
    int number1 = 22;
    int number2 = 23;
    int c;
    c = number1 + number2 ;
    System.out.println("Sum of above two Number : " + c);
    }
}
Output
Sum of above two Number : 45
Press any key to continue . . .
MCQ Available

There are 2 MCQs available for this topic.

2 MCQ