A program works with values. Some never change, and some change as the program runs. Every value has a type, which decides how it is stored and what can be done with it.
Constants
Constants (literals) are fixed values that do not change during execution. Integer constants may be decimal (123), octal with a leading 0 (017), hexadecimal with 0x (0x1F) or binary with 0b (0b1010). Real constants have a decimal point or exponent (3.14, 2.5e3). Character constants are single characters in single quotes ('A'), including escape sequences such as '\n' (new line), '\t' (tab) and '\\' (backslash). String constants are characters in double quotes ("Java"). Boolean constants are true and false.
Variables
A variable is a named location in memory whose value can change. A variable name is an identifier: it may use letters, digits, _ and $, cannot start with a digit, cannot be a keyword, and is case-sensitive. By convention, variable names use camelCase, such as totalMarks.
Definition: Variable. A named storage location in memory, of a declared data type, whose value can be changed while the program runs.
Data Types
Java is a strongly typed language: every variable has a type, checked by the compiler. Data types are primitive or non-primitive (reference). The eight primitive types have fixed sizes on every platform. Integer types: byte (1 byte), short (2 bytes), int (4 bytes) and long (8 bytes, with suffix L). Floating-point types: float (4 bytes, suffix f) and double (8 bytes, the default for real numbers). char (2 bytes) holds a Unicode character. boolean holds true or false. Reference types include classes, interfaces, arrays and strings, and hold the address of an object.
Declaration and Giving Values
A variable is declared with its type and name: int count; double rate, price;. It is given a value by assignment, count = 10;, or initialised when declared, int count = 10;. A value can also be read from the keyboard with a Scanner. Local variables must be given a value before use, or the compiler reports an error. Instance and class variables get default values: 0 for numbers, false for boolean, '\u0000' for char and null for references.
Symbolic Constants
A symbolic constant is a named value that cannot change, declared with final: final double PI = 3.14159;. By convention its name is in capital letters. Symbolic constants make programs easier to read and change, since the value is written in one place. A final variable can be assigned only once.
Table 2.1: Primitive data types
| Type | Size | Range | Default |
|---|---|---|---|
| byte | 1 byte | −128 to 127 | 0 |
| short | 2 bytes | −32,768 to 32,767 | 0 |
| int | 4 bytes | about ±2.1 × 10⁹ | 0 |
| long | 8 bytes | about ±9.2 × 10¹⁸ | 0L |
| float | 4 bytes | about 7 significant digits | 0.0f |
| double | 8 bytes | about 15 significant digits | 0.0 |
| char | 2 bytes | 0 to 65,535 (Unicode) | '\u0000' |
| boolean | — | true or false | false |
Example 2.1: Declaring and using variables
Problem: Write a program that declares variables of several types, defines a symbolic constant for the GST rate, and computes the price with tax.
Solution:
- Declare int quantity, double price, char grade and boolean member.
- Define final double GST = 0.18.
- Compute total = quantity × price × (1 + GST).
Result: The program prints the values and the total with tax.
public class Variables {
public static void main(String[] args) {
final double GST = 0.18; // symbolic constant
int quantity = 3;
double price = 250.0;
char grade = 'A';
boolean member = true;
long big = 9_000_000_000L;
double total = quantity * price * (1 + GST);
System.out.println("quantity = " + quantity + ", grade = " +
grade);
System.out.println("member = " + member + ", big = " + big);
System.out.println("total with GST = " + total);
System.out.println("int range: " + Integer.MIN_VALUE +
" to " + Integer.MAX_VALUE);
}
}
Output
quantity = 3, grade = A
member = true, big = 9000000000
total with GST = 885.0
int range: -2147483648 to 2147483647
Common mistakes
- Writing a long literal without L or a float literal without f, as in float f = 3.5;.
- Using a local variable before giving it a value.
- Trying to change a final variable after it has been assigned.
Key takeaways
- Java has eight primitive types with fixed sizes.
- double is the default real type; int is the default integer type.
- Variables are declared with a type and must be initialised before use.
- final declares a symbolic constant.
Practice questions
- Name the eight primitive data types.
- What is the size of a char in Java, and why?
- How is a symbolic constant declared?
Answers to the practice questions
- byte, short, int, long, float, double, char and boolean.
- 2 bytes, because Java uses Unicode.
- With the keyword final, such as final int MAX = 100;.
Sources
- Official BCA syllabus Object Oriented Programming using Java, course code 25BCA402SEC01, Unit II
