VARIABLES
In Java, every variable has a type. You declare a variable by placing the type first, followed by the name
of the variable. Here are some examples:
double salary; //declaring a single variable salary of type double
int vacationDays;
long earthPopulation;
boolean done;
int i,j; //declaring multiple variables
Rules for Naming Variable
A variable name must begin with a letter and must be a sequence of letters or
Digits
Java is case sensitive. Hence variable Total and total are different variables.
You also cannot use a Java reserved word(keyword) as a variable name.
You cannot have a white space for variable name.
Initializing Variables
After you declare a variable, you must explicitly initialize it by means of an
assignment statement
For Example: If variable is not initialized you get the error as below
int vacationDays;
System.out.println(vacationDays); // ERROR--variable not initialized
Example :
int vacationDays; //declaring a variable called vacationDays
VacationDays = 12; // Initializing vacationDays to 12
You can both declare and initialize a variable on the same line. For example:
int vacationDays = 12;
In Java you can put declarations anywhere in your code.
, Types of Variables
There are three types of variables in Java
1. local variable
2. instance variable
3. static variable
1) Local Variable : 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.
A local variable cannot be defined with "static" keyword.
2) Instance Variable
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.
3) Static variable
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.
Constants
In Java, you use the keyword final to denote a constant. The final modifier means that the variable's
value cannot change. Once the value is assigned, it cannot be reassigned.
class DemoProgram
{
public static void main(String args[])
{
final double PI=3.14; //PI is real constant
int radius =10;
double area;
area= PI*radius*radius;
In Java, every variable has a type. You declare a variable by placing the type first, followed by the name
of the variable. Here are some examples:
double salary; //declaring a single variable salary of type double
int vacationDays;
long earthPopulation;
boolean done;
int i,j; //declaring multiple variables
Rules for Naming Variable
A variable name must begin with a letter and must be a sequence of letters or
Digits
Java is case sensitive. Hence variable Total and total are different variables.
You also cannot use a Java reserved word(keyword) as a variable name.
You cannot have a white space for variable name.
Initializing Variables
After you declare a variable, you must explicitly initialize it by means of an
assignment statement
For Example: If variable is not initialized you get the error as below
int vacationDays;
System.out.println(vacationDays); // ERROR--variable not initialized
Example :
int vacationDays; //declaring a variable called vacationDays
VacationDays = 12; // Initializing vacationDays to 12
You can both declare and initialize a variable on the same line. For example:
int vacationDays = 12;
In Java you can put declarations anywhere in your code.
, Types of Variables
There are three types of variables in Java
1. local variable
2. instance variable
3. static variable
1) Local Variable : 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.
A local variable cannot be defined with "static" keyword.
2) Instance Variable
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.
3) Static variable
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.
Constants
In Java, you use the keyword final to denote a constant. The final modifier means that the variable's
value cannot change. Once the value is assigned, it cannot be reassigned.
class DemoProgram
{
public static void main(String args[])
{
final double PI=3.14; //PI is real constant
int radius =10;
double area;
area= PI*radius*radius;