Tuesday, December 23, 2014

Variables in Java

Variable in java can be defined as a name for a memory location which can hold the data.
Variable declartion:
syntax:
<modifier> <Data Type> <var-Name>;
or
<modifier> <Data Type> <var-Name>= value;
There are 3 types of variables are there in java. They are,
1)Local Variables
2)Instance Variables
3)Static Variables
1) Local Variable:
a variable which is declared inside a method is called as Local variable.
ex:
void m1(){
int a=10; // local varible declaration
int b=20; // local varible declaration
}
Note:
Local variables must and should be initialized before using. Otherwise we will get Compile time error.
2) Instance Variable:
a variable which is declared inside a class but not inside any method, without static keyword is called as Instance variable.
ex:
class MyProgram{
int a=10; // Instance varible declaration
int b; // Instance varible declaration
}
Note:
If we not initialize Instance variables then it will be initialized by default values of that data type.
3) Static Variable:
a variable which is declared inside a class but not inside any method, with static keyword is called as Static variable.
ex:
class MyProgram{
static int a=10; // local varible declaration
static int b; // local varible declaration
}
Note:
If we not initialize Static variables then it will be initialized by default values of that data type.

Control Statements in Java
We have 3 types of Control Statements in Java. They are,
-- Conditional Control Statements
-- Iterative Control Statements
-- Transfer/Jump Control Statements
Control Statements in java
1) Conditional control statements:
Conditional Statements will work based on the conditions. We have different types of Conditional Statements. They are,
i) if : If the condition is true then the control will execute the block of statements otherwise the control will not enter into if block
Syntax:
if(condition)
{
statement1;
statement2;
----------
}
ii) if-else : If the condition is true then the control will execute the if block otherwise the control will execute else block
Syntax:
if(condition)
{
statement1;
statement2;
}
else{
statement3;
statement4;
}
2) Iterative Control Statements:
Iterative control statements are used to execute the group of statements repeatatively based on a condition.
ex:
i) while
ii) do-while
iii) for
3) Transfer/Jump Control Statements:
whenever these statements are encountered then the control will be transfered from one place to another.
ex:
i) break
ii) continue
iii) return

No comments:

Post a Comment