Variables
Variable is the container which holds user data. Memory will be allocated for the variable while executing the program. Values of the variable can be changed any number of times during the program execution.
Syntax:
<DataType> <variableName>;
<DataType> <variableName> = <value>;
Example:
int a;
int a = 10;
String str;
String str = "Hello";
Types of Varibles - Based on Data Types
There are two types of variables based on data types used to declare the variable.
- Primitive Variables
- Reference Variables.
Primitive Variables
Variables declared with primitive data types are called primitive variables.
Example:
int a;
int b = 99;
double d1;
double d2 = 9.9;
Reference Variable
Variables declared with user defined data types are called as reference variables. Example:
String str1;
String str2="Hello World";
| Primitive Variables | Reference Variables |
|---|---|
| Variables declared with Primitive data types are called as primitive variables. | Variables declared with user defined data types are called as reference variables. |
| Memory allocation for primitive variables depends on the primitive data types. | Always 8 bytes of memory will be allocated for reference variable(JVM dependent) |
| Default value for primitive variables depends on the primitive data types. | Always null will assigned as default value for reference variables. |
| Primitive variables hold valid literals or value in the allocated memory. | Reference variables hold either null or address of an object in the allocated memory. |
Types of Varibles - Based on Scope of the Variable.
There are three types of variables based on the scope of the variables.
- Instance variables.
- Static variables.
- Local variables.
Instance variables.
Variables declared inside the class without using static keyword are called as instance variables.
Static variables.
Variables declared in the class using static keyword are called as Static variables.
Local Variables.
Variables declared in the member of the class like method etc are called as Local variables.
There are various syntaxes for declaring variables
1.Declaring a single variable in one variable declaration statement.**
Syntax:
[modifiers] <Data type> <varName>;
Ex:
byte b;
int a;
double d;
char ch;
String str;
Hello h;
Example1.java
class Example1 {
public static void main(String[] as) {
int a;
System.out.println(a);
}
}
Output
compilation error
Example2.java
class Example2 {
static int a;
public static void main(String[] as) {
System.out.println(a);
}
}
Output
0
Example3.java
class Example3 {
int a;
public static void main(String[] as) {
System.out.println(a);
}
}
Output
non static variable cannot be referenced from a static context
2.Initializing a single variable
Syntax:
<varName> = <value/expression>;
Ex:
b = 24;
ab = 98787;
d = 1234.456;
ch = 'A';
str = "Midhun";
h = new Hello();
**Example4.java
class Example4 {
public static void main(String[] as) {
String str;
int a;
str = "Midhun";
a = 99;
System.out.println(str);
System.out.println(a);
}
}
3.Declaring and initializing a single variable in one variable declaration statement.
Syntax:
[modifiers] <Data type> <varName> = <value/expression>;
Ex:
int a = 99;
String str = "Midhun";
double d = 999.99;
boolean b = true;
Example5.java
class Example5 {
public static void main(String as[]) {
int a = 99;
boolean b = true;
double d = 999.99;
String str = "Midhun";
System.out.println(a);
System.out.println(b);
System.out.println(d);
System.out.println(str);
}
}
Output
99
true
999.99
Midhun
4.Declaring multiple variables of single type in one variable declaration statement.
Syntax:
[modifiers] <Data type> <var1>, <var2>, <var3>...;
Ex:
byte b1, b2, b3;
int a, b, c;
char ch1, ch2, ch3;
String str1, str2, str3;
5.Initializing multiple variables with same value
Syntax:
<var1> = <var2> = <var3> = <value/expression>;
Ex:
a = b = c = 9899;
str1 = str2 = str3 = "Midhun";
Example6.java
class Example5{
public static void main(String[] as) {
int a,byte b;
System.out.println(a);
System.out.println(b);
}
}
// cannot declare diff type variable in the same line
Example7.java
class Example7 {
public static void main(String[] as) {
int a, b, c;
a = b = c = 99;
System.out.println(a);
System.out.println(b);
System.out.println(c);
String str1, str2, str3;
str1 = str2 = str3 = "Midhun";
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
6.Declaring and initializing multiple variables of single type in one variable declaration statement.
Syntax:
[modifiers] <Data type> <var1>=<val1>, <var2>=<val2>, <var3>=<val3> ...;
Example:
int a = 120,
b = 76,
c = 56;
String str1 = "Midhun",
str2 = "Balan";
boolean b1 = true,
b2 = false;
Example8.java**
class Example8 {
public static void main(String as[]) {
int a = 120,
b = 76,
c = 56;
System.out.println(a);
System.out.println(b);
System.out.println(c);
String str1 = "Midhun",
str2 = "Balan";
System.out.println(str1);
System.out.println(str2);
}
}