Data variables, variable declaration & initialization in java programming language
Data types
Data type helps the programmer to create
container to store specific type of data.
Classification:
1.
Primitive data type
2.
Nonprimitive data type
1. 1. Primitive data type:
Which are used to create container
(Variables) to store primitive data (Single value data).
e.g integer number, decimal
number, character
Primitive data |
Data type |
Memory size |
Default value |
Integer |
Byte |
1 byte |
0 |
|
Short |
2 byte |
0 |
|
Int |
4 byte |
0 |
|
Long |
8 byte |
0l/0L |
Decimal |
Float |
4 byte |
0.0f/0.0F |
|
Double |
8 byte |
0.00/0.00d/0.00D |
Character |
Char |
2 byte |
/U0000 |
Boolean |
Boolean |
1 bit |
False |
2. Non primitive data type:
Which are used to create container(variables) to store non primitive data (Multi value data).
e.g String, Arrays
Variables:
Variable is
a container which is used to store value. We can create variables with help of
data types.
Note:
in java we can not use variable without declaration.
Syntax of
variable declaration:
Datatype variablename, variablename1, ……….;
Example:
Int
a, b, c ; //three container created
which names a, b & c respectively.
Boolean
x, y, z, r ; // four
container are created which name x, y, z & r respectively.
Variable initialization:
Putting
value inside the variable is called variable initialization.
Syntax:
Datatype variablename = value / expression ;
e.g: int a ;
a = 10;
System.out.println(a);
Variable declare & initialization statement
Syntax:
Datatype
variablename = value ;
Thes
statement is use to create a variable & store value directly.
Example:
int a =10;
String s = “Hi”;
long l =
101 ;
double d = 1234.03 ;
Classification of variables based on visibility:
Local variable:
A variable
declared in a method block or in any other block other than class block.
Characteristics:
I.
Local variables are not assigned without default
value. Therefore we can not use local variable without initialization and with default value we get CTE (Compile time
error).
Example:
Class Var1
{
Public Static void main(String []
args)
{
Int
a ;
Int
b =0;
System.out.println(a);
// CTE
System.out.println(b);
// CTE
}
}
II.
A variable declared in a block is always restricted
to use in same block. We get CTE if we initialize one block and access in another
block.
Example:
Class Var1
{
Public Static void main(String [] args)
{
Int a =10 ;
{
Int b =30;
}
System.out.println(a);
System.out.println(b);
// CTE
}
}
III.
We can not have more than one local variable of same name inside same block. If created we get compile time
error.
Example:
Class Var1
{
Public Static void main(String [] args)
{
Int a =10 ;
Int b =30;
System.out.println(a); // CTE
System.out.println(b);
// CTE
}
}
Global variable:
global
variable refers to a variable that is accessible throughout the program across
all methods and sometimes even across classes depending on how it's declared.
Example 1 :
Global variable in the same class
public class MyClass {
// Global variable
static int globalCount = 0;
public static void main(String[] args) {
incrementCount();
System.out.println("Global Count: "
+ globalCount);
}
public static void incrementCount() {
globalCount++; // Accessible from any method
in the class
}
}
Example 2 :
Global Variable Accessed in Different Classes
//
GlobalVariables.java
public class
GlobalVariables {
public static String appName = "My
Application";
}
//
MainApp.java
public class
MainApp {
public static void main(String[] args) {
System.out.println("App Name:
" + GlobalVariables.appName);
}
}