Variables and identifiers in Java

Variables and identifiers in Java

Variables in Java

Variables are used in every programming language, variables are used to store some values, where these are used by the program. These are named as variables because these values can be changed.

A variable is just a place holder or container which holds some value nd the stored value can be changed.

Declaring or Creating Variables in Java

Syntax to declare a variable

type varname = value
  • type: it is used to specify the data type of the variable to be declared
  • varname: this is the variable name and this has some rules to be applied (For example you want to store some data into the storage box, but you need to name the storage box to identify it),these are called identifiers
  • value :value to be stored in the variable(memory),the value which is assigned should be of the same data type )

Examples for declaring variables with different data types

int x = 23; //integer data type
short s=11; //short data type
long l=24235345; //long data type
byte b=13; //byte data type
float f=123.10f; //float data type

Identifiers in Java

For creating any varibale in java,the varibale should be identified with unique name.so these unique names are called identifiers

For better understanding of code ,we should use descriptive names for variables,example if the program is about age,so the to store someones age,we need to take a variable name as ‘age‘,if we take variable name as x or y,the other person cant understand our code

there are some rules and conventions for naming variables

Rules for naming variables(Identifiers)

  • identifiers can begin with letters ,dollar signs $,under score _
  • identifiers should start with lower sensitive
  • identifiers should not contain white space,instead of white space under score _ can be used
  • identifiers are case sensitive. myage and myAge is different
  • reserved words or java buzz words are cannot be used as the identifiers

Types of Variables in Java

there are three variables in java

  • Local variable
  • Class variable or static variable
  • Instance variable

Variables are used in every programming language, variables are used to store some values, where these are used by the program. These are named as variables because these values can be changed.

A variable is just a place holder or container which holds some value nd the stored value can be changed.

Declaring or Creating Variables in Java

Syntax to declare a variable

type varname = value
  • type: it is used to specify the data type of the variable to be declared
  • varname: this is the variable name and this has some rules to be applied (For example you want to store some data into the storage box, but you need to name the storage box to identify it),these are called identifiers
  • value :value to be stored in the variable(memory),the value which is assigned should be of the same data type )

Examples for declaring variables with different data types

int x = 23; //integer data type
short s=11; //short data type
long l=24235345; //long data type
byte b=13; //byte data type
float f=123.10f; //float data type

Identifiers in Java

For creating any varibale in java,the varibale should be identified with unique name.so these unique names are called identifiers

For better understanding of code ,we should use descriptive names for variables,example if the program is about age,so the to store someones age,we need to take a variable name as ‘age‘,if we take variable name as x or y,the other person cant understand our code

there are some rules and conventions for naming variables

Rules for naming variables(Identifiers)

  • identifiers can begin with letters ,dollar signs $,under score _
  • identifiers should start with lower sensitive
  • identifiers should not contain white space,instead of white space under score _ can be used
  • identifiers are case sensitive. myage and myAge is different
  • reserved words or java buzz words are cannot be used as the identifiers

Types of Variables in Java

there are three variables in java

  • Local variable
  • Class variable or static variable
  • Instance variable
Last updated on by vishal devxo