yt

Header Ads

Chapter 6 What is Variable and Types with Program Explain in Details.

 What is Variable in JAVA ?

  • It is a storage space which is used to store data just like a container or box.
  • It's value may be changed.
  • It always contains last value stored to it.
  • It is always declared with data type.
For Example 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=97.77;
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.
  • Back 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.
//Q1. Sum Of Two variable
class Tech369kk
{
 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)
 {
  Tech369kk obj=new Tech369kk();
  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 variabe can be used
 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 void mul()
 {
 x=y*z;//accessing instance variable
 System.out.println("Mul="+x);
 }


 public static void main(String[] args)
 {
  Easy obj=new Easy();
  obj.add();  
  obj.sub();
  obj.mul();
 }
}

******Output:*****
Add= 30
Sub= 10
Mul= 600
                                                

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 for permanently.
  • Static variables are created when the program starts and destroyed when the program stops.
  • Static variable can be called by class name directly.

// Q3. find static keywords in java
class Statt
{
 //declaring static variable
 static String text="I Hope Clear your Doubts in Java Variable.";
 public static void main(String[] args)
 {
     System.out.println(Statt.text);
 }
}

******Output:*****
I Hope Clear your Doubts in Java Variable

No comments

Theme images by Dizzo. Powered by Blogger.