AddThis

Wednesday 13 February 2019

Java variables / types of variables / instance, static, local variables

Variables:
  • Variable is a container which holds user data.
  • Memory will be allocated for the variable while executing the program.
  • Value of the variable can be changed any number of times during the program execution.
Syntax:
<data type> <var name>;
<data type> <var name> = <value>;

Example: Coming soon...

Types of variables:
There are two types of variables based on data type used to declare the variable.

  1. Primitive Variables
  2. Reference Variables
1. Primitive Variables: Variables declared with primitive data types are called primitive variables.
Ex. 
int a;
int b = 99;
double d1;
double d2 = 9.9;

2. Reference Variables: Variables declared with user defined data types are called reference variables.
Ex.
String str1;
String str2 = "programingsoeasy";

Lab1.java
class Hello{
boolean b1;
byte b2;
short s;
int i;
long l;
float f;
double d;
String str;
Hello h;

void show(){
System.out.println(b1);
System.out.println(b2);
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(f);
System.out.println(d);
System.out.println(str);
System.out.println(h);
}
}
class Lab1{
public static void main(String[] args){
Hello h = new Hello();
h.show();
}
}



Lab2.java
class Hello{
char ch;

void show(){
System.out.println(ch == 0);
System.out.println(ch == ‘’);
System.out.println(ch == ‘ \u0000’);
}
}
class Lab2{
public static void main(String[] args){
Hello h = new Hello();
h.show();
}
}


S.N.
Primitive Variables
Reference Variables
1
Variables declared with primitive data types are called primitive variables.
Variables declared with reference data types are called reference variables.
2
Memory allocation for primitive variables dependents on the primitive data types.
Always 8 bytes of memory will be allocated for reference variables.
3
Default value for primitive variables depends on primitive data types.
Always null will be assigned as default value for reference variables.
4
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 Variables:
There are three types of variables based on the scope of the variables.
  1. Instance Variables
  2. Static Variables
  3. Local Variables
1. Instance Variables : 
  • Variables declared in the class without using static keyword are called as instance variables.

2. Static Variables:
  • Variables declared in the class using static keyword are called as static variables.
3. Local Variables:
  • Variables declared in the member of the class like method etc are called as local variables.

There are various syntax for declaring variables











5. Java data types primitive and user defined


No comments:

Post a Comment

100 AWS Services in Just One Line Each

  100 AWS Services in Just One Line Each Amazon EC2:  Virtual servers in the cloud. Amazon S3:  Scalable object storage service. Amazon RDS:...