1

I have a question about Java Primitive.

Is there some reason as to why the default type in java for the Integer primitive is int and not for example a short or byte ?

For example if I write

byte b = 10; 

the literal 10 is a int for the compiler. Why is that?

Does anyone know I indicate where to find the answer? I also searched the JSL but I found nothing...

micklesh
  • 417
  • 1
  • 4
  • 16
  • 2
    There are no "short" or "byte" literals: http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1 –  May 19 '16 at 08:34
  • 1
    Because a number without a specific character is a int, meaning that the JVM will use 4byte to store it. If you write a "long value" without a letter L, the JVM won't accept it since it is to long (long, ahah ;) ). – AxelH May 19 '16 at 08:40

2 Answers2

1

As noted by @a_horse_with_no_name Chapter 3. Lexical Structure describes Integer Literals. Quote:

An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).

Ilya
  • 2,177
  • 15
  • 19
0

The official doc says: `

byte: The byte data type is an 8-bit signed two's complement integer.

short: The short data type is a 16-bit signed two's complement integer. `

See the link

Alvin
  • 894
  • 8
  • 16