A variable is a location in the computer’s memory where a value is stored and from which the value can be retrieved later.
Variables are used in a Java program to store data that changes during the execution of the program. They are the basic units of storage in a Java program. Variables can be declared to store values, such as names, addresses, and salary details. Variables must be declared before they can be used.
The image below shows the storage of variables in memory.
datatype is a valid data type in Java.
variableName is a valid variable name.
The following code shows the declaration of variables.
int rollNumber; char gender;
The statements declare an integer variable called rollNumber, and a character variable called gender.These statements instruct the operating system to allocate the required amount of memory to each variable in order to hold the type of data specified for each. Additionally, each statement also provides a name that can be used within the program to access the values stored in each of the variables.
Values can be assigned to variables by using the assignment operator (=) as follows:
rollNumber = 101; gender = ‘M’;
101 and ‘M’ are called literals. You can also assign a value to a variable upon creation, as shown:
int rollNumber = 101;
The following code shows the different ways for declaring and initializing variables.
Line 0: int x, y, z; // declares three integer variables x, y and z. Line 1: int a = 5, b, c = 10; // declares three integer variables, initializes a and c. Line 2: byte num = 20; // declares a byte variable num and initializes its value to 20. Line 3: char c = ‘c’; // declares the character variable c with value ‘c’. Line 4: int num1 = num2 = 10; // value 10 is stored in num1 and num2.
As shown in the code, Line 0 and Line 1 are examples of comma separated list of variables. Line 4 displays an example of same value assigned to more than one variable at the time of declaration.
Rules and Conventions
Java programming language has its own set of rules and conventions that need to be followed for naming variables. For a start, variable names should be short and meaningful.
Not adhering to the rules will result in syntax errors. Naming conventions are to be followed as they ensure good programming practices.
The rules and conventions for naming variables are as follows:
- Variable names may consist of Unicode letters and digits, underscore (_), and dollar sign($).
- A variable’s name must begin with a letter, the dollar sign ($), or the underscore character(_). The convention, however, is to always begin your variable names with a letter, not “$” or“_”.
- Variable names must not be a keyword or reserved word in Java.
- Variable names in Java are case-sensitive (for example, the variable names number and Number refer to two different variables).
- If a variable name comprises a single word, the name should be in lowercase (for example,velocity or ratio).
- If the variable name consists of more than one word, the first letter of each subsequent word should be capitalized (for example, employeeNumber and accountBalance).
The variable names in Java should start with a letter (A-Z or a-z), dollar sign ($), or underscore (_).
The table below lists some examples of valid and invalid Java variable names.
|Variable name||Valid / Invalid|
|amount#Balance||Invalid and contains the illegal character #|
|double||Invalid and is a keyword|
|4short||Invalid and the first character is a digit|
A literal signifies a fixed value and is represented directly in the code without requiring computation. For example, the following code shows assigning of literals in the Java program.
Line 1: int val = 50; Line 2: float num = 35.7F; Line 3: char x = ‘x’;
A literal is used wherever a value of its type is allowed. However, there are several different types of literals. Some of them are as follows:
Integer literals are used to represent an int value, which in Java is a 32-bit integer value. In a program, integers are probably the most commonly used type. Any whole number value is an integer literal.
Integers can be expressed as:
- Decimal values, expressed in base 10
- Octal values, expressed in base 8
- Hexadecimal values, expressed in base 16
Each of these have their own literal. An integer literal can also be assigned to other integer types,such as byte or long. When a literal value is assigned to a byte or short variable, no error is generated if the literal value is within the range of the target type. Integer numbers can be represented with an optional uppercase character (‘L’) or lowercase character (‘l’) at the end, which tells the computer to treat that number as a long (64-bit) integer.
Floating-point literals represent decimal values with a fractional component. Floating point literals have several parts.
- Whole number component, for example 0, 1, 2,….., 9.
- Decimal point, for example 4.90, 3.141, and so on.
- Exponent is indicated by an E or e followed by a decimal number, which can be positive or negative. For example, e+208, 7.436E6, 23763E-05, and so on.
- Type suffix D, d, F, or f.
Floating-point literals in Java default to double precision. A float literal is represented by F or f appended to the value, and a double literal is represented by D or d.
Boolean literals are simple and have only two logical values – true and false. These values do not convert into any numerical representation. A true boolean literal in Java is not equal to one, nor does the false literal equals to zero. They can only be assigned to variables declared as boolean, or used in expressions with Boolean operators.
Character literals are enclosed in single quotes. All of the visible ASCII characters can be directly entered inside the quotes, such as ‘g’, ‘$’, and ‘z’. Single characters that cannot be directly entered are hyphen (-) and backslash (\).
When an object is created, a certain amount of memory is allocated for that object. The starting address of this memory is stored in an object, that is, a reference variable. However, at times, it is not desirable for the reference variable to refer that object. In such a case, the reference variable is assigned the literal value null as shown in the following example:
obj = null;
String literals consist of sequence of characters enclosed in double quotes. The characters can be printable or non-printable. However, backslash, double quote and non-printable characters are represented using escape sequences. Following is an example of String literal:
"Welcome to Java"