VARIABLES & DATA
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.
Variable is a name given to a memory location
Also defined as a name of a reserved area allocated in
memory
In Java, variables can only store values of primitive data
types and references to objects.
Variables that store references to objects are called
reference variables.
VARIABLES Reserved
Area
There are three types of
variables in Java:
Local Variables
Instance Variables
Static Variables
50
Example:
int data= 50;
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
The first declaration above is equivalent to:
char a;
char b;
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
Local Instance Static/ Class
TYPES OF VARIABLES
Local Variables
Are declared inside methods, constructors or blocks.
They are created when the method, constructor, or block is
entered.
They get destroyed as soon as the method, constructor, or
block exits.
Access modifiers cannot be used with local variables.
They are visible only within the method, constructor, or block,
in which they are declared.
They are implemented at stack level internally.
There is no default value for local variables, so they should
be declared and an initial value should be assigned before the
first use.
Local Variables- Example 1
Here age is a local variable.
This is defined inside puppyAge() method and its scope is limited to this
method only.
public class Test
{
public void puppyAge()
{
int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age);
}
public static void main(String args[])
{
Test test = new Test();
test.puppyAge();
}
Output:
} Puppy age is: 7
Local Variables- Example 2
This example uses age without initializing it, so it would give an error at the
time of compilation.
public class Test
{
public void puppyAge()
{
int age;
age = age + 7;
System.out.println(“ Puppy age is : " + age);
}
public static void main(String args[])
{
Test test = new Test();
test.puppyAge();
}
}
Output:
Test.java:4:variable age might not have been initialized age = age + 7;
^
1 error
TYPES OF VARIABLES
Instance Variables
A variable declared inside a class but outside a method, constructor, or any
block.
When a space is allocated for an object in the heap a slot for each instance
variable value is created.
They are created when an object is created using the keyword ‘new’ , and
destroyed when the object is destroyed.
They hold values that must be referenced by more than one method,
constructor, or block.
They can be declared at class level, before or after use.
Access modifiers can be given for them.
They are visible for all methods, constructors and blocks in the class.
They can be accessed directly by calling the variable name inside the class.
They have default values.
For numbers, the default value is 0
For booleans, it is false
For object references it is null.
Instance Variables: Example
public class Employee // This method prints the employee
{ details.
// this instance variable is visible for any public void printEmp()
child class. {
public String name; System.out.println("name : "
+ name );
// salary variable is visible in Employee System.out.println("salary :"
class only. +salary);
private double salary; }
public static void main(String
// The name variable is assigned in the args[])
constructor. {
public Employee (String empName) Employee empOne = new
{ Employee("Ransika");
name = empName; empOne.setSalary(1000);
} empOne.printEmp();
// The salary variable is assigned a value. }
public void setSalary(double empSal) }
{
salary = empSal;
}
TYPES OF VARIABLES
Static/ Class variables
A variable declared using static keyword
They are declared within a class but outside any method, constructor,
or block.
There would be only one copy of each static variable per class,
regardless of how many objects are created.
They are stored in static memory.
They are created when the program starts, and destroyed when the
program stops.
Visibility is similar to instance variables. However, mostly they are
declared as public, since they must be available for users of the class.
Default values are same as instance variables.
Values can be assigned during the declaration, or within a constructor.
Additionally, values can be assigned in special static initialising blocks.
They can be accessed with the class name as,
ClassName.VariableName
DATA TYPES IN JAVA
Data types in Java are primarily divided into two
categories:
Primitive Data Types
Non-Primitive Data Types
Data Types
Primitive Non-Primitive
String
Boolean Numeric Array
Etc.
Character Integral
Integer Floating Point
byte short int long
boolean char float double
Data Type Default Value Default Size
boolean False 1 Bit
char ‘\u0000’ 2 Bytes
byte 0 1 Byte
short 0 2 Bytes
int 0 4 Bytes
long 0L 8 Bytes
float 0.0F 4 Bytes
double 0.0D 8 Bytes
PRIMITIVE DATA TYPES- EXPLAINED!
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,
Zero and Non-Zero,
1 and 0,
Or any other Numeric Value.
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?
Because Java uses Unicode System instead of ASCII
code system.
WHAT IS ‘\U0000’ ?
‘\u0000’ is the lowest range in Unicode System.
WHAT IS UNICODE SYSTEM?
Unicode is a Universal International Standard Character
Encoding that is capable of representing most of the
world’s written languages.
WHY JAVA USES
UNICODE???
BEFORE UNICODE...???
Before Unicode, there were many language standards:
ASCII: American Standard Code For Information
Interchange, For the United States
ISO 8859-1: For Western European Languages.
KOI-8: For Russian
GB18030 and BIG-5: For Chinese...and so on!!!
PROBLEMS...!
This caused Two Problems:
A particular code value corresponds to different letters
in various language standards.
The Encodings for languages with large character sets
have variable length, some common characters are
encoded as Single Bytes, others require Two or More
Bytes.
SOLUTION..!
To solve these problems, a new language standard was
developed, i.e., Unicode System.
In Unicode, Character holds 2 bytes, so Java also uses 2
bytes to store a character.
Lowest Value: ‘\u0000’
Highest Value: ‘\uFFFF’