Java Byte, Short, Long, and Double Variable Types
By Stephen Bucaro
In Java, as in other programming languages, information that can be changed as the program runs is stored
in variables. A variable is created with a statement that has the type and the name
of the variable. A variable statement can also include the value of the information being stored.
A primitive variable type is predefined by the language and is named by a reserved keyword. The eight
primitive data types supported by the Java programming language are:
byte bdata = 120;
The byte data type is an 8-bit signed value. With a signed data type one bit is used to
store the sign of the value. Therefore half of the range of values stores positive numbers and half
stores negative numbers. For example, a byte takes 8 bits and its value can range form -128 to 127.
If the sign bit is ignored, that is if you store only positive numbers in a byte, its range would have
been 0 to 255. The byte data type is commonly used when dealing with raw data like reading a binary file.
short sdata = 123;
The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768
and a maximum value of 32,767. With a signed data type the most significant (leftmost) bit indicates
the sign of the value (sometimes called the sign bit). If the sign bit is zero, then the number is positive.
If the sign bit is one, then the number is negative. Two's complement binary values are used in computers
because it makes the circuitry required to perform binary addition and subtraction simpler. The short data
type is commonly used when dealing with raw data like reading a binary file.
The int data type is a 32-bit signed two's complement integer, It can be used to hold any integer from
-2,147,483,648 to 2,147,483,647.
The long data type is a 64-bit two's complement integer. It has minimum value of -27^63 and a maximum
value of 2^63-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long,
which has a minimum value of 0 and a maximum value of 2^64-1. The long data type is used when range of values
wider than those provided by int is needed.
float bigAmt = 487.34;
The floating point data type is a 32-bit single precision (the mantissa part is 4 bytes) binary
value, designated by the keyword float, used to store numbers that contain a decimal point. It can
used to hold any number from 4E-45 to 3.4028235E38.
The double data type is a double-precision 64-bit double precision (the mantissa part is 8 bytes)
binary value, It can used to hold any number from -1.79769313486232E308 to 1.79769313486232E308.
The boolean type can store only the keyword true or the keyword false. Although the
boolean data type represents one bit, but its size isn't precisely defined. Booleans can be used to store
the result of test statements.
int a = 4;
int b = 5;
result = a < b; // true
result = a > b; // false
char bigC = 'C';
The char type is used to store a single character. A character may be a single letter, a punctuation
mark, or a symbol. The char data type is a single 16-bit Unicode character. Unicode is a standard for
representing characters as binary integers. 16-bits means that it can represent more than 65,000 unique characters.
It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535). To designate char type
data, it must be contained within single-quotes as shown above.
In Java variable names can begin with a letter, underscore, or dollar sign. The rest of the name can be any
letters or numbers except punctuation marks or spaces. Also, remember that Java is case-sensitive, so when you
use upper-case or lower-case characters in a variable name, you must use that same arrangement of upper-case
and lower-case characters wherever you refer to that variable in your code.
Reserved words are words that are used by the language for special purposes. Java has 53 reserved words,
including: break, catch, continue, final, native, package, private, super, throw, transient, and many more.
Reserved words cannot be used for variable names.