Variables and Data Types
Variables:
Variable is name of reserved area allocated in
memory. In other words, it is a name of
memory location.
Types of Variable
There are three types of variables in java:
◦ local variable
◦ Instance variable
◦ static variable
Different variables:
1) Local Variable
A variable which is declared inside the
method is called local variable.
2) Instance Variable
A variable which is declared inside the
class but outside the method, is called
instance variable . It is not declared as
static.
3) Static variable
A variable that is declared as static is called
static variable. It cannot be local.
Example to understand the types of variables in
java
Class A
{
int data=50;//instance variable
static int m=100;//static variable
void method()
{
Int n=90;//local variable
}
}//end of class
Data Types
Based on the data type of a variable, the operating
system allocates memory and decides what can be
stored in the reserved memory.
Data Types in java
Primitive Non-primitive
Class Array
Numeric Non-Numeric
Interface
Integer Floating
point Character Boolean
Type Data Type Default value Default size
byte 0 1 byte
short 0 2 byte
Integer
int 0 4 byte
long 0L 8 byte
Floating point float 0.0f 4 byte
double 0.0 8 byte
Boolean boolean false 1 bit
Character char '\u0000' 2 byte
Java Variable Example: Widening
class Simple
{
public static void main(String[] args)
{
int a=10;
float f=a;
System.out.println(a);
Output
System.out.println(f); 10
} 10.0
}
Note: Javas’s Automatic conversion
1. The two types are compatible.
2. Destination is larger than source.
byte < int< float<double
Java Variable Example: Narrowing (Typecasting)
class Simple
{
public static void main(String[] args)
{
float f=10.5f;
//int a=f;//Compile time error
int a=(int)f;
Output
System.out.println(f); 10.5
System.out.println(a); 10
}
}
Note: float and double can be cast to any type except
boolean.
Java Variable Example: Adding Lower
Type
class Simple
{ public static void main(String[] args)
{
byte a=10;
byte b=10;
//byte c=a+b;//Compile Time Error: because
a+b=20 will be int
byte c=(byte)(a+b);
System.out.println(c); Output
} 20
}
Type promotion rules:
All byte and short values are promoted to int.
If one operand is long then whole expression
is promoted to long.
If one operand is float (except double), the
expression is promoted to float.
If one operand is double then result is in
double.
Java Array
Java array is an variable that contains elements
of similar data type. We can store only fixed set
of elements in a java array.
Types of Array in java
Single Dimensional Array
Multidimensional Array
Creating an array
Three steps for creating an array
1. Declaration
2. Memory allocation
3. Putting values into memory location
(initialization)
1. Syntax to Declare an Array in java
dataType[] arr;
dataType []arr;
dataType arr[];
Note: declaration establishes the fact that ‘arr’
is an array variable and no array actually exists.
Creating an array contd..
2. Syntax to Memory allocation for an Array
arr=new datatype[size];
3. Initialization
arr[0]=10;
arr[1]=20;
arr[2]=70;
arr[3]=40;
arr[4]=50;
OR
Declaration, instantiation and initialization
int a[]={33,3,4,5};
simple example single dimensional array
class Testarray1
{
public static void main(String args[])
{
//declaration, instantiation and initialization
int a[]={33,3,4,5};
//printing array
for(int i=0;i<a.length;i++)
//length is the property of array
System.out.println(a[i]);
}
}
Two dimensional array in java
Example to instantiate Two-dimensional Array
in java
int[][] arr=new int[2][2];//2 row and 2 column
Example to initialize Multidimensional Array in
java
arr[0][0]=1;
arr[0][1]=2;
arr[1][0]=3;
arr[1][1]=4;
Simple example Two dimensional array
class Testarray3
{ public static void main(String args[])
{
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++)
{ for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Variable size array
A[0] A[0][0] A[0][1] A[0][2]
A[1] A[1][0] A[1][1]
A[2][0]
A[2]
Creation of variable size array:
int A[][]=new int[3][];
A[0]=new int[3];
A[1]=new int[2];
A[2]=new int[1];