Variables in Java. Types of variables (static, instance and local variable).
Variables in Java :
A variable is a name of the memory location. It is used to store data. Its value can be changed, and it can be reused many times. variable is a storage area to hold data.
Or
variable is a name which is used to store a value of any type during program execution.
Variable declaration :
To declare the variable in Java, we can use following Syntax.
Syntax :
data_type variable_name;
Example:
int rno;
String name;
float percentage;
char grade;
Here, rno, name, percentage, grade are variables.
The int, float, char, String are the data types.
Variable initialization :
This means assigning a value to variables.
Initializing a variable means to provide it with a value.
Syntax :
variable_name = value;
Example:
rno=10;
name="arun";
percentage=89.99;
grade='A';
A variable can be initialized and defined in a single statement, like:
Syntax :
data_type variable_name = value;
Example :
int rno=10;
String name="arun";
float percentage=89.99;
char grade='A';
This will reduce line of your code.
Rules for defining variables :
1. A variable can have alphabets, digits, and underscore.
Invalid variable name:
int 2^;
Valid variable name:
int power_of_2;
2. A variable name can start with the alphabet, and underscore only. It can't start with a digit.
Invalid variable name:
int 4;
Valid variable name:
int four;
3.No whitespace is allowed within the variable name.
Invalid variable name:
int total marks;
Valid variable name:
int total_marks;
Or
int totalmarks;
4.A variable name must not be any reserved word or keyword, e.g. Switch while, if,int, etc.
Invalid variable name:
int long;
valid variable name:
int big;
5. A Variable names are case sensitive.
variable name:
int MAX; int Max, max;
These are valid. But case sensitive.
Types of variables in Java.
There are two types of variables in Java.
1.static(class) variable
2.non-static variable
1.Static variable :
A variable which is declared as static is called static variable.
Static variables are also known as Class variables.
It cannot be local. You can create a single copy of static variable and share among all the instances of the class.
2.Non-static :
A variable cannot be defined with "static" keywords. It is not declared as static.
There are two types of non-static variable :
A. local variable : A variable declared inside the body of method, constructor or blocks is called local variable.
The scope of these variables exists only within the block in which the variable is declared.
B. instance variable : A variable declared inside the class but outside the body of the method, constructor or block, is called instance variable.
we may use access specifiers for instance variables. If we do not specify any access specifier then the default access specifier will be used.
Example to understand the types of variables in java
class Student
{
int rno=10;
//instance variable
String name="Omkar dengi ";
//instance variable.
static String college ="SB arts and kcp college;//static variable
Student()
{
String university="Rani Channamma university belgavi (RCUB) ";
//local variable
}
}
program :
Program to demonstrate use of variables in Java.
public class student
{
int rno;
String name;
static String college="Asp";
//static variable is college.
public static void change()
{
college = "kcp";
}
public student()//default
{
String university ="Rani Channamma University Belagavi(RCUB)";
//local variable is university.
System.out.println(university);
}
public student( int r,String n)
{
rno=r;
name=n;
}
public void display()
{
System.out.println("roll no:"+rno);
System.out.println("name:"+name);
System.out.println("college name:"+college);
}
public static void main(String args[])
{
student s=new student();
student s1=new student(10,"xyz");
s1.display();
student.change();
student s2=new student(11,"abc");
s2.display();
}
}
Output:
Rani Chanamma University Belagavi(RCUB)
roll no:10
name:xyz
college name:Asp
roll no:11
name:abc
college name:kcp
Comments
Post a Comment