Type Casting in Java Part 8 - JAVA ថ្នាក់ទី១ | JAVA For Beginner
#រៀនសរសេរកូដជាមួយចិត្រា #javaprogramming #javabasics #codingwithchitra #sololearncoding
JAVA ថ្នាក់ទី១ | JAVA For Beginner
អ្វីទៅជា Type Casting in Java?
Type Casting គឺសំដៅទៅលើការផ្តល់ តម្លៃទៅអោយ primitive data type មួយទៅកាន់ primitive data type ផ្សេងទៀត
នៅក្នុងភាសា Java មាន Type Casting ចំនួន ២ គឺ:
១) Widening Casting(Automatically)
២) Narrowing Casting(Manually)
Widening Casting
អ្វីទៅជា Widening Casting(Automatically) ?
យើងអាចហៅវាម្យ៉ាងទៀតថា implicit type casting
គឺជាការបំប្លែងទិន្នន័យពីតូចទៅធំ
វាផ្លាស់ប្តូរប្រភេទទិន្នន័យនៃអញ្ញាត នៅកំឡុងពេល Compile Time
ពុំមានការបាត់បង់ទិន្នន័យនោះទេ
លំដាប់នៃប្រភេទទិន្នន័យនៅក្នុងភាសា Java:
byte → short → char → int → long → float → double
ឧទាហរណ៍
int a = 9;
double b = a; //Automatically casting int to double
អ្វីទៅជា Narrowing Casting(Manually) ?
អាចហៅម្យ៉ាងទៀតថា explicit type casting ឫ explicit type conversion
គឺជាការបំប្លែងទិន្នន័យពីធំទៅតូច
អាចមានការបាត់បង់ទិន្នន័យ
ចាំបាច់ត្រូវប្រើសញ្ញារង្វង់ក្រចកដើម្បី casting
លំដាប់នៃប្រភេទទិន្នន័យនៅក្នុងភាសា Java:
double → float → long→ int → short→ byte
ឧទាហរណ៍
double a = 4.4;
int b = (int) a; //manually casting: double to int
លំហាត់: Type Casting
1. Declare a variable double num1 and assign it the value 3.14.
2. Create an int variable roundedNum and assign it the rounded value of num1 using explicit type casting.
3. Print the value of roundedNum to the console.
4. Declare a byte variable byteValue and assign it the value 128.
5. Create a short variable shortValue and assign it the value of byteValue using explicit type casting.
6. Print the value of shortValue to the console.
7. Declare a long variable longValue and assign it the value 9876543210.
8. Create an int variable intValue and assign it the value of longValue using explicit type casting.
9. Print the value of intValue to the console.
10. Declare a float variable floatValue and assign it the value 2.71828.
11. Create a double variable doubleValue and assign it the value of floatValue using explicit type casting.
12. Print the value of doubleValue to the console.
កម្រងវីដេអូមេរៀន JAVA ថ្នាក់ទី១👉 https://youtube.com/playlist?list=PLSJTJj3gU6qhkjSEUmgBKGJnn0ob1NZSu&si=rcvL8CwF_5EGx_2n
កម្រងវីដេអូមេរៀន Python 👉 https://youtube.com/playlist?list=PLSJTJj3gU6qimVFO-iV1pOsxTujdIEAqo&si=EyC1cjq73QNGJYI_
YouTube Channel 👉 https://www.youtube.com/@codingwithchitra
Facebook Page 👉 https://www.facebook.com/khmshared
Telegram Channel 👉 https://t.me/codingwithchitra
0:00 Introduction
3:32 Type Casting in Java
5:47 Widening Casting (Automatically Casting)
7:42 Narrowing Casting (Manually Casting)
10:20 Practise
22:25 Quiz
25:23 Exercise