-1

I came to know that byte and short are interpreted as int type in java. If that is correct, please let me know why do we need byte and short primitive data types in java. Is it just for type checking by the compiler?

And also byte will take 1 byte of memory and short will take 2 bytes of memory, and int type takes 4 bytes. Please let me know if it is not a performance issue to save byte and short types in 4 bytes as byte and short are interpreted as int type.

Rajesh
  • 39
  • 4
  • 1
    Possible duplicate of [Why does the Java API use int instead of short or byte?](http://stackoverflow.com/questions/27122610/why-does-the-java-api-use-int-instead-of-short-or-byte) – Tom May 21 '17 at 09:02
  • [Why does the Java API use int instead of short or byte?](//stackoverflow.com/q/27122610) – Tom May 21 '17 at 09:02

1 Answers1

5

I came to know that byte and short are interpreted as int type in java.

No you didn't. You 'came to know' that they are promoted to int when used in arithmetic expressions or when used as an actual parameter to functions that have specifed int as the formal argument.

If that is correct

It isn't.

please let me know why do we need byte and short primitive data types in Java.

To hold smaller values.

Is it just for type checking by the compiler?

No.

And also byte will take 1 byte of memory and short will take 2 bytes of memory, and int type takes 4 bytes.

Exactly, which contradicts your initial baseless assertion.

Please let me know if it is not a performance issue to save byte and short types in 4 bytes as byte and short are interpreted as int type.

It isn't. It doesn't happen.

user207421
  • 305,947
  • 44
  • 307
  • 483