Jayesh Deshmukh [ft
JAVA
VARIABLES
6
aa
——s
——"Jayesh Deshmukh ‘in|
A Variable is a name given to a memory location. It is
used to store a value that may vary. Java is a statically
typed language, and hence, all the variables are
declared before use.
Variable Declaration
a rm,
int count;
1. datatype: Type of data that can be stored in this
variable.
2. data_name: Name given to the variable.
In this way, aname can only be given to a memory
location. It can be assigned values in two ways:
¢ Variable Initialization
¢ Assigning value by taking input
>Jayesh Deshmukh o
Variable Initialization
¢ datatype: Type of data that can be stored in this
variable.
* variable_name: Name given to the variable.
¢ value: It is the initial value stored in the variable.
datatype an
Woy co i _ 5a
int age = cm
Reserved Memory for variableJayesh Deshmukh [fy
Variables naming in Java
A variable name should be short and meaningful.
It should begin with a lowercase letter.
It can begin with special characters such as _
(underscore) and $ (dollar) sign.
If the variable name contains multiple words, then
use the camel case, i.e. variable name should start
with a lowercase letter followed by an uppercase
letter. For eg: iamRupnath, instaGram.
Always try to avoid single character variable names
such as i, j, and k except for the temporary variables.
A variable name can not contain whitespaces.
We can’t use keywords(pre-defined literals) as the
variable names.Jayesh Deshmukh oo
Types of Variables
1. Local Variables
A variable declared inside the body of the method is
called local variable. You can use this variable only within
that method and the other methods in the class aren't
even aware that the variable exists.
>Jayesh Deshmukh BH
fetes 18)
2. Instance Variables
A variable declared inside the class but outside the body of
the method, is called an instance variable. It is not declared
as static.
It is called an instance variable because its value is
instance-specific and is not shared among instances.Jayesh Deshmukh Oo
3. Static Variables
A variable that is declared as static is called a static
variable. It cannot be local. You can create a single copy of
the static variable and share it among all the instances of
the class. Memory allocation for static variables happens
only once when the class is loaded in the memory.
Satan
{
void rst merelep)Jayesh Deshmukh
ike Comment Share REN