Saturday 8 November 2014

Data types and Variables


Java has 8 simple data types: byte, short, int, long, char, float, double, and boolean.
These can be categorized in to four groups
Integers: byte(8 bit), short(16 bit), int(32 bit) and long(64 bit)
Floating point numbers: float(32 bit) and double(64 bit)
Characters: char( range is 0-255)
Boolean Values: boolean( either true or false)
Now let us take another example to get complete picture about variables. 

Calss VariablesDemo{
          public static void main(String args[]){
              int a;
              int x,y;
              int z=10;
     }
}
In the above code int a says that a is a integer variable. This is called declaration of variable a. In java we can declare single variable at a time or sequence of variable of the same data type(int x,y;).
Int a also says that a's data type is Integer, that means a is capable storing integer values of 32-bit length. We can also assign values to the variable while we are declaring the variable as in int z=10. Here z is a variable of Integer data type holding value 10.
Variable can change their variables at any time in their scope.
Integer variables supports operations like additiona(+), substraction(-), multiplication(*) ,division(/) and modulo(%).
For integer variables in java we have post increments(i++) and pre increment(++i) as well as post decrement(i--) and  pre decrement(--i).
As we can see from their names post increment means the value of i will be incremented after executing the statement i++. and preincrement means the value of i will be incremented before the execution of the statement itself.
 For example
let us look at sample code
      i=5,j=10,k;
      k=i+++j
 here k value will be 15
If we use ++i + j then k value will be 16
This is same for decrement operations also, except that it will decrease the value.
Character variables
character variables define as follows
char c;
here c represents a variable capable of storing a single character data. This value can be any of ASCII values or their corresponding integer values.
char type variables supports only addition and subtraction operation.
Boolean variables are like binary variables, they will take either true or false as their variables. As we will see in the forthcoming topics boolean variables are used in if and else statements as well as loops.


No comments:

Post a Comment