google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Variable in Java

Variable in Java

0

 


Variables are the names of memory locations where data is saved temporarily before being utilized later in the program. Values can be saved in a variable throughout the execution of a program, and the stored value can be altered. Before a variable may be used in Java programming, it must be declared.

 

  • Declaration of Variables in Java
  • Initialization of Variables in Java
  • Rules of declaring variables in Java
  • Scope of Variables in Java
  • Local variables
  • Instance variables
  • Class/Static variables

 

Declaration of Variables in Java

Declaring a variable implies specifying the type of data it will hold. Variables are named storage places whose values can be updated while the application is running. In a Java application, it is the fundamental storage unit.

Syntax

 

// Declare Single Variable in a Program for same Data_Type

 

Data_type variable_name;

 

 

// Declare Multiple Variable in a Program for same Data_Type

 

Data_type variable_name, variable_name, variable_name;

 

 

// Declare Single and Multi Variable in a Program for different Data_Type

 

Data_type variable_name;

Data_type variable_name;

Data_type variable_name;

Data_type variable_name, variable_name, variable_name;

 

 

Here, Data_type determines the type of data that will be stored in the variable.

Initialization of Variables in Java

Example:

 

// Declare Single Variable in a Program for same Data_Type

 

int a;

 

 

// Declare Multiple Variable in a Program for same Data_Type

 

int a,b,c;

 

 

// Declare Single and Multi Variable in a Program for different Data_Type

 

int a;

float b;

char c;

double d,e,f;

 

 

Initialization of Variables in Java

This means assigning a value to variables. There are two ways to assign a value to variables in Java:

  • Static - When the program starts, the memory for variables is predetermined (at the time of writing a programme).
  • Dynamic - In Java, dynamic means that you may define variables anywhere in the program since memory is assigned to them when the statement is performed (at the execution time).

 

// Initialization of Variables in Java

 

int a,b; // declare variable

a=10; // Initialization of Variable

b=20;// Initialization of Variable

 

 

Rules of declaring variables in Java

  1. Variable names are case-sensitive.
  2. Capital letters A-Z, lowercase letters a-z, numerals 0-9, and two special characters such as underscore and dollar sign can all be used in a variable name.
  3. A letter must be the first character.
  4. Variable names cannot contain blank spaces.
  5. Keywords in Java can't be used as variable names.

 

Scope of Variables in Java

Variable Scope means - That limit, as far as the variable can be used.

In Java there are various types of variable scope:

  1. Local variables
  2. Instance variables
  3. Class/Static variables

Local variables

A variable that is declared within the method called local variables. It is defined in method or other statements, such as defined and used within the cache block, and outside the block or method, the variable cannot be used.

Instance variables

A non-static variable that is declared within the class but not in the method is called instance variable. Instance variables are related to a specific object; they can access class variables.

Class/Static variables

A variable that is declared with static keyword in a class but not in the method is called static or class variable.

Example

 

// class starts here.

 

class a2p {                                         

 int rupee = 100; //instance variable 

 static int pin = 2315; //static variable

 

// method starts here.

 

 public static void main(String[] args) {

  int age = 25; //local variable 

 }

 

 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top