We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

Variables in Java

Variables in Java

What is a Variable?

A variable can be comprehended of as an ampule which holds an incentive for us, amid the life of a Java program. Each factor is relegated an information type which assigns the sort and amount of significant worth it can hold.

Variable is a memory location with a name and can be deployed by programs.

In order to use a variable in a program we need to perform two steps

  • Variable Declaration
  • Variable Initialization

Variable Declaration:

To declare a variable, we must specify the data type & give the variable a unique name.

The syntax for declaring a variable is:

datatype variable name;

The variable name is an identifier. These variables are used to denote constants, arrays, methods, classes and files.

Examples of Valid Declarations are

int a,b,c;
float pi;
double d;
char a;

Variable Initialization:

To initialize a variable using assignment operator.

The syntax to initialize a variable is:

data_type variable_name = value1,variable_name = value2……variable_name = value n;

Example :

int a=2,b=4,c=6;
float pi=3.14f;
double do=20.22d;
char a=’v’;

Click Here-> Get Prepared for Java Interviews

Types of Variable in Java

Static or Class variables

  • These variables are perceived inside the class and outside techniques, with the modifier ‘static’.
  • These are likewise called class variables, since every one of the occasions of a class share a similar variable
  • If a case adjusts the static variable, the change is repeated in every single other case of the class. Subsequently the extent of these kind of variables is simply the class.
  • If it is most loved that the estimation of a static variable ought to be kept steady, making examples unfit of changing the esteem, “last” modifier ought to be utilized amid variable statement.

Non-static or Instance variables

These Type of variables are announced inside the class and outside strategies, without the modifier “static”.

As these sorts of variable qualities are special to a case, they characterize the condition of the item/occasion of the class.

Local variables

These variables are expressed inside techniques. Indeed, even inside a technique, the extent of local variable could be bound to a particular square.

Example Program to demonstrate the variable types:

 

Variables Types

Click Here-> Get Java Training with Real-time Projects

Besant Technologies WhatsApp