Variable and Constant in Java

  • It is a name of storage space which is used to store data.
  • It's value may be changed.
  • It always contains last value stored to it.
  • It is always declared with data type.

Variable Declaration

int rollno;
float marks;
char grade;
  • Here rollno is a variable of type int ,marks is a variable of type float and grade is a variable of type char.

Variable Initialization

  • We can initialize a variable at the time of declaration of variable or after declaration.
int rollno=201;
float marks=85.6;
char grade='A';
  • Here 201 is the value of rollno,85.6 is the value of marks and A is the value of grade. Character value is always written in single quotes.

Other method of variable Initialization

  • We can also initialize a variable after the time of declaration of variable.
int rollno;
float marks;
char grade;

rollno=201;
marks=85.6;
grade='A';

Rules to define a variable

  • The first letter of a variable should be alphabet or underscore(_).
  • The first letter of variable should not be digit.
  • After first character it may be combination of alphabets and digits.
  • Blank spaces are not allowed in variable name.
  • Variable name should not be a keyword.

Types of Variable

  • Local Variable
  • Instance Variable
  • Static Variable

Local Variable

  • A variable declared inside the body of the method or constructor or block is called local variable.
  • Local variable can be used only inside that method/function in which it is declared.
  • A local variable can be a static variable.
class Easy
{
 public void add()
 {
 int x,y=10,z=20;//declaring local variable
 x=y+z;
 System.out.println("Add="+x);
 }
 public static void main(String[] args) 
 {
  Easy obj=new Easy();
  obj.add();       
 }
}
/*
### Output ###
Add=30
*/

Instance Variable

  • A variable which is declared inside a class but outside the body of the method or constructor or block is called instance variable.
  • Instance variable can be used anywhere in the program.
class Easy
{
 //these instance variable can be used 
 //inside all the function of this class
 int x,y=30,z=20;
 public void add()
 {
 x=y+z;//accessing instance variable
 System.out.println("Add="+x);
 }
 public void sub()
 {
 x=y-z;//accessing instance variable
 System.out.println("Sub="+x);
 }
 public static void main(String[] args) 
 {
  Easy obj=new Easy();
  obj.add();  
  obj.sub();
 }
}
/*
### Output ###
Add=30
Sub=10
*/

Static Variable

  • A variable which is declared with static keyword, inside a class but outside the body of the method or constructor or block is called static variable.
  • Static variable is stored in the static memory.
  • Static variables are created when the program starts and destroyed when the program stops.
  • Static variable can be called by class name directly.
class Easy
{
 //declaring static variable
 static String text="You are a smart guy.";
 public static void main(String[] args) 
 {
     System.out.println(Easy.text);
 }
}
/*
### Output ###
You are a smart guy.
*/

What is constants in Java ?

  • An element of program whose value can not be changed at the time of execution of program is called constant.
  • It is also called literals.
  • It may be int, float and character data type.

Rules for constructing integer constant

  • It must have at least one digit.
  • It must not have a decimal point.
  • It may be positive or negative.
  • No comma or blank space are allowed in integer constant.

Rules for constructing floating point constant

  • It must have at least one digit.
  • It must have a decimal point.
  • It may be positive or negative.
  • No comma or blank space are allowed in floating point constant.

Rules for constructing character constant

  • It is a single alphabet, digit or special symbol.
  • The length of character constant is 1 character.
  • Character constant is enclosed within single quotes (Example char c='A';).

Post a Comment

0 Comments