Skip to main content

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

Comments

Popular posts from this blog

Assignment of ITA/ Information Technology and Application BCA- Technology369kk

Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

C++ and Java Practical All Questions Answers - BCA -Technology369kk

C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

Assignment of PMO (Principal of Management and Organization) - Technology369kk

 ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...