Examples of Integer Data Types in Java Programming
Maybe some of us already know the data type in a particular programming language. But to note, there may be differences from the data range of a data type between one programming language with other programming languages.
Based on the above problem, in this tutorial we'll discuss about the integer data type in Java Programming. In addition, we give many examples of each integer type category in this tutorial.
Understanding Integer data types
The integer data type is a data type that has a positive integer value and also a negative integer and both positive and negative integers do not contain fractions.Example : 2, -2, -100, 4, 129
Types of Integer data types on Java
Java provides four different integer types to accomodate different size numbers. All the numeric types are signed, which means that they can hold positives or negative numbers. The integer types have the following ranges :- byte has range -128 to 127. This data type has number of bits 8
- short has range -32,768 to 32,767. This data type has number of bits 16.
- int has range -2,147,483,648 to 2,147,483,647. The data type has number of bits 32 .
- long has range -9,223,372,063,854,775,808 to 9,223,372,063,854,775,807. Number of bits = 64.
Example of Byte data type
import java.util.Scanner;
class tipedata
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
byte value;
System.out.print("Enter your first value:");
value = scan.nextByte();
System.out.println("The value entered is :"+value);
}
}
Output :
Enter your first value:127
The value entered is :127
If we enter a value that is outside its range, for example the value 128 will display an exception message as follows:
Enter your first value:128
Exception in thread "main" java.util.InputMismatchException: Value out of range.
Value:"128" Radix:10
at java.util.Scanner.nextByte(Scanner.java:1887)
at java.util.Scanner.nextByte(Scanner.java:1840)
at tipedata.main(tipedata.java:9)
Example of Short data type
import java.util.Scanner;
class tipedata
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
short value;
System.out.print("Enter your value :");
value = scan.nextShort();
System.out.println("The value entered is :"+value);
}
}
Output:
Enter your value:-32768
The value entered is :-32768
If if we input the value of -32,769 where the value is outside the range that can be accommodated by the short data type, it will show the exception as below:
Enter your value:-32769
Exception in thread "main" java.util.InputMismatchException: Value out of range.
Value:"-32769" Radix:10
at java.util.Scanner.nextShort(Scanner.java:1993)
at java.util.Scanner.nextShort(Scanner.java:1946)
at tipedata.main(tipedata.java:9)
Press any key to continue...
Example of Int data type
import java.util.Scanner;
class tipedata
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int value;
System.out.print("Enter your value :");
value = scan.nextInt();
System.out.println("The value entered is :"+value);
}
}
Output:
Enter your value:2147483647
The value entered is:2147483647
If if we input the value 2147483648 where the value is outside the range that can be accommodated by int data type, it will show the exception as below:
Enter your value:2147483648
Exception in thread "main" java.util.InputMismatchException: Value out of range.
Value:"2147483648" Radix:10
at java.util.Scanner.nextShort(Scanner.java:1993)
at java.util.Scanner.nextShort(Scanner.java:1946)
at tipedata.main(tipedata.java:9)
Press any key to continue...
Example of Long data type
import java.util.Scanner;
class tipedata
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
long value;
System.out.print("Enter your value :");
value = scan.nextLong();
System.out.println("The value entered is :"+value);
}
}
Output:
Enter your value:-9223372063854775808
The value entered is :-9223372063854775808
If if we enter the value 9223372063854775809 where the value is outside the range that can be accommodated by long data type, it will show the exception as below:
Enter your value:-9223372063854775809
Exception in thread "main" java.util.InputMismatchException: Value out of range.
Value:"-9223372063854775809" Radix:10
at java.util.Scanner.nextShort(Scanner.java:1993)
at java.util.Scanner.nextShort(Scanner.java:1946)
at tipedata.main(tipedata.java:9)
Press any key to continue...