>In this post, we will learn about the variable and java data types.
>Variable is a name of memory location.
>There are three types of variables:
local, instance and static.
>There are two types of datatypes in java,
primitive and non-primitive.
Variable:
Variable is name of reserved area allocated in memory. int age=20; //Here age is a variable
Types of Variable:
There are three types of variables in java
> local variable
> instance variable
> static variable
Local Variable: A variable that is declared inside the method is called local variable.
Instance Variable: A variable that is declared inside the class but outside the method is called instance variable .It is not declared as static.
Static variable: A variable that is declared as static is called static variable.It cannot be local.
Example to understand the types of variables:
class Test{
int data=50; //instance variable
static int m=100; //static variable
void method(){
int a=90; //local variable
}
}
Data Types in Java:
In java, there are two types of data types
>1 Primitive data types
>2 Non-primitive data types
Primitive data types:
Boolean---------------->boolean
Numeric: It has 3 types
>Character------------>char
>Integer-------------->byte, short, int long
>Floating-point------->float,double
Data Type Default Value Default Size
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0 8 byte
float 0.0 4 byte
double 0.0 8 byte
Why char uses 2 byte in java and what is \u0000 ?
Because java uses unicode system rather than ASCII code system. \u0000 is the lowest range of unicode system.
To get detail about Unicode System see Unicode System post.
No comments:
Post a Comment