Java Constants, Variables, and Datatypes
Java Constants
A constant is a value that cannot be changed.
Java Variables
A variable has these attributes:
- Name   Must start with a letter and consist of letters and
digits. Variables are case sensitive, which means that capitalization
matters. The naming convention in Java is to start a variable name
with a lower case letter, and start new words within the name with
a capital letter. For example: numberOfCustomers.
- Value   The binary data contained in memory. The
value is interpreted according to the variable's datatype.
- Address   Location in memory where the value is stored.
- Size   The number of bytes that the variable occupies in memory.
- Datatype   The interpretation of the value. Some important datatypes
are listed in the following table.
- Range   The minimum and maximum values of the
variable. The range of a variable depends on its size. In general, the
bigger the size in bytes, the bigger the range.
Java Primitive Datatypes
Datatype | Meaning | Size in Bytes | Min Value |
Max Value | Sig. Digits |
byte | Integer | 1 | 0 |
255 |   |
short | Integer | 2 | -32,768 |
32,767 |   |
int | Integer | 4 | -2 billion |
2 billion |   |
long | Integer | 8 |
-9.2x1018 |
9.2x1018 |   |
float | Floating Point | 4 |
-3.4x1038 | 3.4x1038
| 7 |
double | Floating Point | 8 |
-1.8x10308 | 1.8x10308
| 15 |
char | Character | 2 |
Any Unicode character |   |
boolean | boolean | 1 |
false | true |   |
Long and Float Constants
The default datatype for an integer constant is int.
A long constant uses an L suffix:
345L.
The default datatype for a floating point constant is double.
A float constant uses an F suffix:
3.14159F.
Use a narrowing cast to convert a numeric constant to
byte or short.
String Objects
A string in Java is not a primitive data type. It is an object from
the system defined class String.
Examples of string constants:
"watermelon",
"$%&*^%!!",
"354",
" " (space),
"" (null string)
The string declaration
String item = "apple";
is short for the object declaration
String item = new String("apple");