The document explains the concept of variables in Java, defining them as data containers that store values during program execution. It outlines different types of variables, including local, instance, and static variables, along with their characteristics and examples. Additionally, it provides rules for naming variables, emphasizing the importance of unique identifiers and descriptive names for maintainable code.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
19 views5 pages
Variable
The document explains the concept of variables in Java, defining them as data containers that store values during program execution. It outlines different types of variables, including local, instance, and static variables, along with their characteristics and examples. Additionally, it provides rules for naming variables, emphasizing the importance of unique identifiers and descriptive names for maintainable code.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
Java Programming
Topperworld.in
Variables in Java
Variable in Java is a data container that stores the data values during Java
program execution,
Variable is a memory location name of the data
Every variable is assigned data type which designates the type and quantity
of value it can hold.
In Java, there are different types of variables, for example:
+ String - stores text, such as "Hello". String values are surrounded by
double quotes
+ int - stores integers (whole numbers), without decimals, such as 123
or -123
+ float - stores floating point numbers, with decimals, such as 19.99 or -
19.99
+ char - stores single characters, such as ‘a' or 'B', Char values are
surrounded by single quotes
+ boolean - stores values with two states: true or false
Types of Variable
In Java, there are three types of variables: Types of Variables
1. Local Variables
2. Instance Variables
3, Static VariablesJava Programming
* Local variable: This is a variable that is declared inside the body of a
method.
+ Instance variable: This Java variable is defined without the STATIC
keyword, but as outside of a method declaration. They are object-
specific variables, which is why they are known by this name.
«Static variable: This variable is initialized only once, just when the
program execution starts. It is the variable that should be initialized
first, especially before an instance variable is initialized.
Java Variables Example
Pike eoy
t
eas)
id method()
Pere Cl MET CARE ea)
fis
Ma
Bi keul ema
In order to use a variable in a program you to need to perform 2 stepsJava Programming
Variable Declaration:
To declare a variable, you must specify the data type & give the variable a
unique name.
type
int count;
Examples of other Valid Declarations are
Variable Ini
ization:
To initialize a variable, you must assign it a valid value.Java Programming
Example of other Valid Initializations are
You can combine variable declaration and initialization.
int count = 100;
Example :
Trane
era ett
Clr Ly
Cri eae
Identifier in Java
All Java variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names
(age, sum, totalVolume).
In Java, an identifier can be a class name, method name, variable
name, or labelJava Programming
Note: It is recommended to use descriptive names in order to create
understandable and maintainable code:
Example
Om seu tra ee Tate
Rules for Naming Variables in Java
The general rules for naming variables are
+ Names can contain letters, digits, underscores, and dollar signs.
+ Names must begin with a letter.
+ Names should start with a lowercase letter and it cannot contain
whitespace.
+ Names can also begin with $ and _ (but we will not use it in this
tutorial).
+ Names are case sensitive ("myVar" and "myvar" are different
variables).
+ Reserved words (like Java keywords, such as int or boolean) cannot be
used as names,