در کل ۳ نوع متغیر وجود داره
local variable
instance variable
static variable (class variable)
local variable
instance variable
static variable (class variable)
متغیری که درون یک class و خارج یک method تعریف میشه رو میگیم instance variable
و همچنین این متغیرها نمیتونن static باشن
و همچنین این متغیرها نمیتونن static باشن
متغیرهایی که به عنوان static تعریف میشن رو میگیم static variable
و همچنین این متغیرها نمیتونن local باشن
و همچنین این متغیرها نمیتونن local باشن
class A { int data = 50; // instance variable
static int m = 100; // static variable
void method () { int n = 90; // local variable
}
} //end of class
Primitive data type
اولین نوع از دیتاها در جاواست و از قبل تعریف شده هستن
که تعداد اونا ۸ تاست
اولین نوع از دیتاها در جاواست و از قبل تعریف شده هستن
که تعداد اونا ۸ تاست
boolean
char
byte
short
int
long
float
double
اما non primitive type ها
نوعی از داده ها هستن که بصورت کلاس تعریف میشن
مثل String, Array و .....
و کلاسهایی که خودمون تعریف میکنیم
نوعی از داده ها هستن که بصورت کلاس تعریف میشن
مثل String, Array و .....
و کلاسهایی که خودمون تعریف میکنیم
مقدار و سایز اولیه primitive type ها به شکل زیر هست
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 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
class Simple { public static void main (String[] args) { int a = 10;
int b = 10;
int c = a + b;
System.out.println(c);
}
}
--------------------------------------------
Output:
20
جمع دوتا متغیر در جاوا
class Simple { public static void main (String[] args) { int a = 10;
float f = a;
System.out.println(a);
System.out.println(f);
}
}
--------------------------------------------
Output:
10
10.0
ریختن دیتا یک نوع متغیر به نوع دیگر
در این مثال مقدار a ریخته میشه در متغیر f
ینی int به float
به این کار میگن Widening یا گسترش دادن
انواع Widening ها رو میشه به صورت زیر دید
در واقع تبدیل شدن نوع دیتا کوچکتر به بزرگتر به راحتی در جاوا امکانپذیر هست
- From a byte to a short, an int, a long, a float, or a double
- From a short to an int, a long, a float, or a double
- From a char to an int, a long, a float, or a double
- From an int to a long, a float, or a double
- From a long to a float or a double
- From a float to a double
در واقع تبدیل شدن نوع دیتا کوچکتر به بزرگتر به راحتی در جاوا امکانپذیر هست
class Simple { public static void main(String[] args) { float f = 10.5f;
//int a = f; // Compile time error
int a = (int) f;
System.out.println(f);
System.out.println(a);
}
}
--------------------------------------------
Output:
10.5
10
ریختن نوع دیتا بزرگتر در کوچک تر به طور مستقیم امکان پذیر نیست
بخاطر همین در خط ۴ در صورت کامنت نبودن خط، compile error خواهیم داشت
بخاطر همین در خط ۵ میگیم متغیر f رو می خوایم به int تبدیل کنیم
ینی از متغیر float مقدار اضافه شو بردار بریز تو متغیر int
به اینکار میگن Narrowing (Type Casting)
انواع type casting ها رو میشه به صورت زیر دید
از نوع دیتا بزرگتر به نوع دیتا کوچکتر
- From a byte to a char
- From a short to a byte or a char
- From a char to a byte or a short
- From an int to a byte, a short, or a char
- From a long to a byte, a short, a char, or an int
- From a float to a byte, a short, a char, an int, or a long
- From a double to a byte, a short, a char, an int, a long, or a float
از نوع دیتا بزرگتر به نوع دیتا کوچکتر