In all the programs we have written, we used variables and literal values. So, what are the variables?
Variable
A variable is a named storage, which can be manipulated by the programs. As we know through our earlier post, what are the binary values. The data is stored in memory in the form of binary values. In the program, we declare the variables of specific type, and variable holds memory (in no of bits) according to the data type, as each data type can have different memory size and their representation in bits.
We also learnt about the data types. To specify the data type for a variable is called a variable declaration. And when you give a value to the variable, that is called initialization (when it is done first time) or assignment (replacing old value with new value). Here is the syntax -
int i = 5; int j; j = 10;
Every data type variable has default values if not initialized. There are some rules for naming the variables to be followed -
- Variable names should start with a letter (a to z / A to Z), currency character $ or an underscore.
- Variable names can have combinations of any kind of characters (alphabets, numbers or special characters).
- Variable names can not be key words in Java given in the following table.
- Variable names are case-sensitive.
The Java keywords
abstract | assert | boolean | break | byte | case | catch |
class | const | continue | default | do | double | else |
enum | exports | extends | final | finally | float | for |
goto | if | implements | import | instanceof | int | interface |
long | goto | module | native | new | open | opens |
package | private | protected | provides | public | requires | return |
short | static | strictfp | super | switch | synchronized | this |
throw | throws | to | transient | transitive | try | uses |
void | volatile | while | with | null | true | false |
Literals
The constant values given to or assigned to the variables are called literals. Like in above example - number 5 and 10 are literal values.
Constants
When you want a variable to hold a value which should not be changed in the program or outside the program (accessing from other programs) by any means, declare the variable as final. So when you declare the variable as final, it is called as constant variable and will remain unchanged.
final double pi = 3.14;
No comments:
Post a Comment