Variables In Java Language
VARIABLES :
Variables are labels that express a particular position in memory and connect it with a data type.
The first way to declare a variable : This specifies its data type, and reserves memory for it. It assigns zero to primitive types and null to objects.
Syntax
dataType variableName;
The second way to declare a variable : This specifies its data type, reserves memory for it, and puts an initial value into that memory. The initial value must be of the correct data type.
Syntax
dataType variableName = initialValue;
The first way to declare two variables : all of the same data type, reserves memory for each.
Syntax
dataType variableNameOne, variableNameTwo;
The second way to declare two variables : both of the same data type, reserves memory, and puts an initial value in each variable.
Syntax
dataType variableNameI = initialValueI, variableNameII=initialValueII;
Variable name:
- Use only the characters ‘a’ through ‘z’, ‘A’ through ‘Z’, ‘0’ through ‘9’, character ‘_’, and character ‘$’.
- A name cannot include the space character.
- Do not begin with a digit.
- A name can be of any realistic length.
- Upper and lower case count as different characters.
- A name cannot be a reserved word (keyword).
- A name must not previously be in utilized in this block of the program.
CONSTANT :
Constant means fixed value which is not change at the time of execution of program. In Java, there are two types of constant as follows:
Numeric Constants
Character Constants
An Integer constant refers to a series of digits. There are three types of integer as follows :
Embedded spaces, commas and characters are not alloed in between digits.For example : 23 411 7,00,000 17.33
It allows us any sequence of numbers or digits from 0 to 7 with leading 0 and it is called as Octal integer. For example : 011 00 0425
It allows the sequence which is preceded by 0X or 0x and it also allows alphabets from ‘A’ to ‘F’ or ‘a’ to ‘f’ (‘A’ to ‘F’ stands for the numbers ‘10’ to ‘15’) it is called as Hexadecimal integer. For example: 0x7 00X 0A2B
It allows us fractional data and it is also called as folating point constant. It is used for percentage, height and so on. For example : 0.0234 0.777 -1.23
It allows us single character within pair of single coute. For example : ‘A’ ‘7’ ‘\’
It allows us the series of characters within pair of double coute. For example : “WELCOME” “END OF PROGRAM” “BYE …BYE” “A”
In Java program, there are many things which is requires repeatedly and if we want to make changes then we have to make these changes in whole program where this variable is used. For this purpose, Java provides ‘final’ keyword to declare the value of variable as follows:
Syntax
final type Symbolic_name=value;
For example : If I want to declare the value of ‘PI’ then:
CODE/PROGRAM/EXAMPLE
final float PI=3.1459
the condition is, Symbolic_name will be in capital letter( it shows the difference between normal variable and symblic name) and do not declare in method.