In this section, you will learn about Java variables. A variable refers to the memory location that holds values like: numbers, texts etc. in the computer memory. A variable is a name of location where the data is stored when a program executes.
The Java contains the following types of variables:
Before using variables you must declare the variables name and type. See the following example for variables declaration:
int num; //represents that num is a variable that can store value of int type.
String name; //represents that name is a variable that can store string value.
boolean bol; //represents that bol is a variable that can take boolean value (true/false);
You can assign a value to a variable at the declaration time by using an assignment operator ( = ).
int num = 1000; // This line declares num as an int variable which holds value "1000".
boolean bol = true; // This line declares bol as boolean variable which is set to the value "true".
Data type: The type of value that a variable can hold is called data type. When we declare a variable we need to specify the type of value it will hold along with the name of the variable. This tells the compiler that the particular variable will hold certain amount of memory to store values. For example, in the lines of code above num is int type and takes two bytes to hold the integer value, bol is a boolean type and takes one bit to hold a boolean value .
Some common types of data types are used in the programming languages called as the primitive types like characters, integers, floating point numbers etc. These primitive data types are given below where size represents the memory size it takes, For example, boolean takes a value "true"/"false" in 1 bit memory. It takes value "false" if not initialized (in the case of non-local variables)
|Data Type||Description||Size||Default Value|
|boolean||true or false||1-bit||false|
|byte||Signed Integer||8-bit||(byte) 0|
|short||Signed Integer||16-bit||(short) 0|
Rs. 20,000 US$ 300
Today: Rs. 10,000 US$150
Course Duration: 30 hrs
Posted on: February 25, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles