In java programming we use four integer types - 'byte’, ‘short‘, ‘int’, ‘long’. All of this integers holds positive and negative values. We are going to know about them one by one with examples.
byte
The smallest type integer in java is byte. This is a signed 8-bit type and it has a range from –128 to 127. Byte is useful when working with raw binary data. We write ‘byte’ before variable to declare them as byte.
Let’s write a program to use byte
short
short is a signed 16-bit type and it has a range from –32,768 to 32,767. We write ‘short’ before variable to declare it as short. It is probably the least-used Java type.
Let’s write a program to use short
int
The most commonly used integer type is int. It is a signed 32-bit type that has a range From –2,147,483,648 to 2,147,483,647. int is the best choice when time to use integer.
Let’s write a program to use int
long
long is a signed 64-bit type and that has a range from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. We use long when ‘int’ is unable to hold the range of integer value.
Let’s write a program to use long
byte
The smallest type integer in java is byte. This is a signed 8-bit type and it has a range from –128 to 127. Byte is useful when working with raw binary data. We write ‘byte’ before variable to declare them as byte.
Let’s write a program to use byte
public class usebyte {
public static void main(String args[])
{
byte a=12,b=20;
System.out.print(a+b);
}
}
Output of above programpublic static void main(String args[])
{
byte a=12,b=20;
System.out.print(a+b);
}
}
32
short
short is a signed 16-bit type and it has a range from –32,768 to 32,767. We write ‘short’ before variable to declare it as short. It is probably the least-used Java type.
Let’s write a program to use short
public class useshort {
public static void main(String args[])
{
short a=120,b=200;
System.out.print(a+b);
}
}
Output of above programpublic static void main(String args[])
{
short a=120,b=200;
System.out.print(a+b);
}
}
320
int
The most commonly used integer type is int. It is a signed 32-bit type that has a range From –2,147,483,648 to 2,147,483,647. int is the best choice when time to use integer.
Let’s write a program to use int
public class useint {
public static void main(String args[])
{
int a=320,b=600;
System.out.print(a+b);
}
}
Output of above programpublic static void main(String args[])
{
int a=320,b=600;
System.out.print(a+b);
}
}
920
long
long is a signed 64-bit type and that has a range from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. We use long when ‘int’ is unable to hold the range of integer value.
Let’s write a program to use long
public class uselong {
public static void main(String args[])
{
long a=620,b=1200;
System.out.print(a+b);
}
}
Output of above programpublic static void main(String args[])
{
long a=620,b=1200;
System.out.print(a+b);
}
}
1820

0 comments:
Post a Comment