Variables are used for storing data in memory during program execution.
Data types
Data Type | Size (Bits) | Description |
---|---|---|
byte short int long | 8 16 32 64 | Signed integer |
float double | 32 64 | Floating-point number |
char | 16 | Unicode character |
boolean | 1 | Boolean value |
Declaring variables
To declare (create) a variable you start with the data type you want it to hold followed by a variable name. The name can be anything you want, but it’s a good idea to give your variables names that are closely related to the values they will hold. The standard naming convention for variables is that the first word should be lowercase and any subsequent words initially capitalized:
Assigning variables
To give the variable a value you use the assignment operator (=) followed by the value. When a variable is initialized (assigned a value), it then becomes defined (declared and assigned):
The declaration and assignment can be combined into a single statement:
If you need multiple variables of the same type, there is a shorthand way of declaring or defining them using the comma operator (,):
Using variables
Once a variable has been defined, you can use it by simply referencing the variable’s name—for example, to print it:
Integer types
As shown earlier, there are four signed integer types you can use, depending on how large a number you need the variable to hold:
In addition to standard decimal notation, integers can also be assigned by using octal or hexadecimal notation. As of Java 7, a binary notation is also available:
Digits in a number can be separated by an underscore (_). This feature was introduced in Java 7 and is provided only to improve readability:
Floating-point types
The floating-point types can store integers as well as floats. They can be assigned with either decimal or exponential notation:
Note that constant floating-point numbers in Java are always kept internally as doubles. Therefore, if you try to assign a double to a float, you’ll get an error because a double has a higher precision than a float. To assign it correctly you can append an F character to the constant, which says that the number is in fact a float:
A more common and useful way to do that is by using an explicit cast. An explicit cast is performed by placing the desired data type in parentheses before the variable or constant that is to be converted. This will convert the value to the specified type—in this case, float—before the assignment occurs:
Char type
The char data type can contain a single Unicode character, delimited by single quotes:
Chars can also be assigned by using a special hexadecimal notation that gives access to all Unicode characters:
Boolean type
The boolean type can store a Boolean value, which is a value that can only be either true or false. These values are specified with the true and false keywords:
Variable scope
The scope of a variable refers to the code block within which it’s possible to use that variable without qualification. For example, a local variable is a variable declared within a method. Such a variable will only be available within the method’s code block, after it’s been declared. Once the scope (code block) of the method ends, the local variable will be destroyed:
In addition to local variables , Java has field and parameter type variables, which later chapters will cover. But Java doesn’t have global variables, as, for example, C++ does.
Anonymous block
You can restrict the scope of local variables using an anonymous (unnamed) code block . This construct is seldom used, because if a method is large enough to warrant the use of an anonymous block, a better choice is often to break up the code into separate methods: