Next: Floating-point primitive types. Up: Primitive types. Previous: None.
Textbook: Section 9.2
Java actually has four primitive types for representing integer values.
All of these are signed numbers, being represented in two's-complement form. Thus, a byte can represent numbers from -128 to 127. (The class Byte defines MIN_VALUE and MAX_VALUE constants, for you to use when you want to refer to the smallest popssible value.)
byte represents an 8-bit integer value short represents a 16-bit integer value int represents a 32-bit integer value long represents a 64-bit integer value
Generally you should just stick with int, but every once in a while one of the other types is justified. Some examples of situations where they can be important:
A literal is the character representation of an individual value that you can include in the code. For example, ``123'' is an int literal, representing the integer value 123.
By default, when you type a literal in Java containing only digits, it is an int literal. You can also use an octal representation, prefixed by an initial 0. For example, ``015'' is an octal representation of the number 15(8)=13(10). And you can use a hexadecimal representation by prefixing the number with an initial 0x or 0X: ``0xA0 represents A0(16)=160(10).
You can create a long literal by appending an L to an int literal. (You can also use l, but that's confusing, as it looks too much like a 1.) 10000000000000L represents a long value - and it's a compiler error if you leave off the L, as the number doesn't fit into an int.
Next: Floating-point primitive types. Up: Primitive types. Previous: None.