3-Java-Variables & Data Types
3-Java-Variables & Data Types
TYPES
IN JAVA
VARIABLES
A variable stores a value of a particular type.
It has a name, a type, and a value associated with it.
RAM
DECLARING & INITIALIZING
VARIABLES
Variable declarations are used to specify the type and
name of the variables.
This implicitly determines their memory allocation and
the values that can be stored in them.
In Java, all variables must be declared before they
can be used.
DECLARING & INITIALIZING
VARIABLES
The basic form of a variable declaration is:
type identifier [= value][, identifier [= value] ... ] ;
type is one of Java’s data types.
identifier is the name of the variable.
To declare more than one variable of the specified type, use a
comma-separated list.
Examples:
char a, b, c; //a, b, & c are character variables.
double area; // area is a floating point variable
boolean flag; // flag is a boolean variable
char c;
DECLARING & INITIALIZING
VARIABLES
A declaration can also include initialization code to specify
an appropriate initial value for the variable:
int i = 10; // i is an int variable with initial value 10
int j = 101; // j is an int variable with initial value 101
long big = 2147483648L; //big is a long variable with
specified initial value
TYPES OF VARIABLES
Variables
Primitive Non-Primitive
String
Boolean Numeric Array
Etc.
Character Integral
A. boolean:
1 bit.
May take on the values true and false only.
true and false are defined constants of the language, and are not
same as
True and False,
TRUE and FALSE,
1 and 0,
Booleans may not be cast into any other type of variable, nor
may any other type of variable be cast into a boolean.
PRIMITIVE DATA TYPES- EXPLAINED!
B. byte:
1 Byte.
Signed (Two’s Complement).
Range: -128 to +127 (or -27 to 27-1)
C. short:
2 Bytes.
Signed (Two’s Complement).
Range: -32,768 to +32,767 (or -215 to 215-1)
PRIMITIVE DATA TYPES- EXPLAINED!
D. int:
4 Bytes.
Signed (Two’s Complement).
Range: -2,147,483,648 to +2,147,483,647
(or -231 to 231-1)
May be cast into other numeric types (byte, short, long, float,
double).
In case of Lossy casts (like int to byte), the length of the
higher data type is cut short to accommodate in the lower data
type.
PRIMITIVE DATA TYPES- EXPLAINED!
E. long:
8 Bytes.
Signed (Two’s Complement).
Range: -9,223,372,036,854,775,808 to
+9,223,372,036,854,775,807
(or -263 to 263-1)
PRIMITIVE DATA TYPES- EXPLAINED!
F. float:
4 Bytes
Single Precision.
Uses 32 bits.
The fist bit is the Sign (S) bit
Next 8 bits are Exponent (E) bits.
Final 23 bits are the Fraction (F) bits.
S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF
Range: 1.40129846432481707e-45 to 3.40282346638528860e+38
(positive or negative).
Like all numeric types floats may be cast into other numeric types (like
byte, short, long, int, double).
When lossy casts to integer type are done (like float to short), the
fractional part is truncated and the length of the higher data type is cut
short to accommodate in the lower data type.
PRIMITIVE DATA TYPES- EXPLAINED!
G. double:
8 Bytes.
Double Precision.
Uses 64 bits.
The fist bit is the Sign (S) bit
Next 11 bits are Exponent (E) bits.
Final 52 bits are the Fraction (F) bits.
S EEEEEEEEEEE
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FF
Range: 4.94065645841246544e-324d to
1.79769313486231570e+308d (positive or negative).
PRIMITIVE DATA TYPES- EXPLAINED!
H. char:
2 Bytes
Unsigned.
Unicode.
Used to store any character.
Range: 0 to 65,535.
chars are not the same as bytes, ints, shorts or Strings.
WHY CHAR USES 2 BYTES?
WHAT IS ‘\U0000’ ?