Variables and Primitive Data Types
Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
There are two data types available in Java:
- Primitive Data Types
- Reference/Object Data Types
For now, we’ll focus only on primitive data types.
Primitive data types
Java provides us with 8 primitive (basic) data types. These are:
- Byte (8 bits)
- Short (16 bits)
- Int(32 bits)
- Long (64 bits)
- Float (32 bits)
- Double (64 bits)
- Char (16 bits)
The byte, short, long and int can hold only integer numbers with no fractions(decimal points)
The float and double data types can hold very large numbers, including those with decimal points.
The char data type can hold only single character at a time.
The Boolean data type can hold only true or false – nothing else.
Before we start, please note that every statement in java must end with a semi-colon (;). Also remember that you cannot use a variable before declaring it. For example, look at the following statement:
This is not correct, since the compiler does not know how many bits of memory are required to store the variable x. What we need to do is first declare the data type of the variable x, and then assign a value to it.
To declare a variable, we write something like this:
The first word in the statement int x; is the data type int. Next the name of the variable is written, which is x. Finally we have a semi colon. So a variable is declared using a data type followed by the name of the variable, followed by a semi-colon.Some examples of variable declarations are shown below:
- int x;//(Declare one variable at a time)
- int x=5 ;//(Declare and initialize a variable with a value on the same time)
- int x, y, z;//(Declare more than one variable of the same data type on the same time)
- int x, y=55, z;//(Initialize some of the variables on one line)
- int x=88, y=878, z=555;//(Initialize all the variables on the same line)
You can assign value to a variable in three ways:
- x=12;//(Assign a literal value to the variable)
- y=x;//(Assign another variable’s value)
- z=x+y+6;//(Use an expression containing literals or variables or a mixture of both)
The names we assign to our variables are called identifiers.
The rules for naming identifiers are:
- An identifier must start with either a letter, the $ sign, or the underscore (_) character.
- An identifier cannot start with a number.
The reserved keywords in java should not be used as identifier. Below mentioned keywords should not be used as identifiers as these are reserved words.
- Since java is a case sensitive language, an identifier named student is different from Student.
- The length of an identifier has no limit. We can define an identifier salary, and another one called Salary_of_the_chairman.However it would not be practical to use very long identifier names in your programs.
A literal is the value in the source code. For example:
The data type is int, the identifier is age and the literal value is 5.