0% found this document useful (0 votes)
27 views11 pages

Variables

Uploaded by

revathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views11 pages

Variables

Uploaded by

revathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Variables

S.REVATHI AP/CSE

1
Variables
The variable is the basic unit of storage in a Java program.

A variable is defined by the combination of an identifier, a type,


and an optional initializer.

In addition, all variables have a scope, which defines their


visibility, and a lifetime.

2
Declaring a Variable

In Java, all variables must be declared before they can be used.

The basic form of a variable declaration is shown here:

type identifier [ = value][, identifier [=

value] ...] ;

3
Example
int a, b, c;

int d = 3, e, f = 5;

byte z = 22;

double pi = 3.14159;

char x = ‘X’

4
Dynamic Initialization
Java allows variables to be initialized dynamically, using any
expression valid at the time the variable is declared.

Example: Computes the length of the hypotenuse of a right


triangle given the lengths of its two opposing sides

CODING DEMO
5
The Scope and Lifetime of Variables
Java allows variables to be declared within any block.
A block defines a scope.
A scope determines what objects are visible to other parts of your
program.
Declare a variable within a scope, can localizing that variable
and protecting it from unauthorized access and/or
modification.
Scopes can be nested

CODING DEMO
6
Variables are created when their scope is entered, and
destroyed when their scope is left.
This means that a variable will not hold its value once it has
gone out of scope.

CODING DEMO

7
Type Conversion and Casting
To assign a value of one type to a variable of another type.

If the two types are compatible, then Java will perform the
conversion automatically.

For example: int value to a long variable.

For instance, there is no automatic conversion defined from


double to byte.

8
1.Java’s Automatic Conversions
The following two conditions are met:
• The two types are compatible.
• The destination type is larger than the source type

When these two conditions are met, a widening conversion takes


place.

For example, the byte to int values=Automatic conversion

char or Boolean values= No automatic


conversion 9
2.Casting Incompatible Types
For example: int to byte.
This kind of conversion is sometimes called a narrowing
conversion, since you are explicitly making the value narrower so
that it will fit into the target type.
To create a conversion between two incompatible types, you must
use a cast.
A cast is simply an explicit type conversion. It has this general
form:

(target-type) value 10
CODING DEMO

11

You might also like