Byte Short Int Long Data Types in Java

In this class, We discuss Byte Short Int Long Data Types in Java.

The reader should have a basic understanding of data type and variables. Click Here.

The four data types

1) Byte

2) Short

3) Int

4) Long

The above data types are used to store integer values.

Integer value examples: 10, 20, -10

10.2 is a floating point number.

If we need to store floating point numbers, we use float and double data types.

What is the difference between the above data types?

Size difference for the above data types.

Byte is a 1-byte space.

Short is a 2 bytes space

int is 4 bytes space

long is 8 bytes space.

Integer values are both positive and negative numbers.

Java uses two’s complement representation of storing the integers.

If you are not good at two’s complement representation. Please take the course digital logic design.

The first 25 lessons cover the basics of number systems.

The concept of a numbers system helps a lot in understanding programming concepts.

The diagram below shows an example of 4-bit two’s complement representation of numbers.

The minimum number stored using 4 bits is -8.

The maximum number stored is 7.

N bits the minimum and maximum lint is (-2^(n-1)) to (2^(n-1) -1)

Byte -128 to 127

Short -32768 to 32767

int -2147483648 to 2147483647

Long -9223372036854775808 to 9223372036854775807

Execute the code for practice.

class test

{

public static void main(String args[])

{

byte a=20;

short b = 30;

int c = 40;

long d = 50;

System.out.println(a);

System.out.println(b);

System.out.println(c);

System.out.println(d);

}

}