ប្រភេទទិន្នន័យជា លេខគត់ byte, short, int long នៅក្នុង Primitive Data Types in Java Part 7.2
#សរសេរកូដជាមួយចិត្រា #javaprogramming #javabasics #codingwithchitra #sololearncoding
JAVA ថ្នាក់ទី១ | JAVA For Beginner
Primitive Data Types- ប្រភេទទិន្នន័យជាលេខគត់(Integers)
ប្រភេទទិន្នន័យជាលេខគត់មានដូចជា៖
- ប្រភេទទិន្នន័យជា byte
- ប្រភេទទិន្នន័យជា short
- ប្រភេទទិន្នន័យជា int
- ប្រភេទទិន្នន័យជា long
1. ប្រភេទទិន្នន័យជា byte
- ប្រភេទទិន្នន័យជា byte មានទំហំផ្ទុកក្នុង Memory ជា Fix size 8-bit(1 Byte) ដែលជាលេខគត់
- តម្លៃលេខតូចបំផុតគឺ -128 (-2^7)
- តម្លៃលេខធំបំផុតគឺ 127 (2^7 -1)
- តម្លៃដើមរបស់វាគឺស្មើរ 0
- ប្រភេទទិន្នន័យជា byte ត្រូវបានប្រើដើម្បីសន្សំទំហំផ្ទុកនៅក្នុង Memory
- ប្រើជំនួសប្រភេទទិន្នន័យជា integer ពីព្រោះវាមានទំហំ ៤ដងតូចជាង integer
ឧទាហរណ៍
byte a = 100, byte b = -50
+ប្រភេទទិន្នន័យជា short
- ប្រភេទទិន្នន័យជា short មានទំហំផ្ទុកក្នុង Memory ជា Fix size 16-bit(2 Byte) ដែលជាលេខគត់
- តម្លៃលេខតូចបំផុតគឺ -32,768 (-2^15)
- តម្លៃលេខធំបំផុតគឺ 32767 (2^15 -1)
- តម្លៃដើមរបស់វាគឺស្មើរ 0
- ប្រភេទទិន្នន័យជា short ត្រូវបានប្រើដើម្បីសន្សំទំហំផ្ទុកនៅក្នុង Memory ដូជា byte ផងដែរ
- ប្រើជំនួសប្រភេទទិន្នន័យជា int ពីព្រោះវាមានទំហំ ២ដងតូចជាង int
ឧទាហរណ៍
short s = 10000, short r = -20000
+ ប្រភេទទិន្នន័យជា int
- ប្រភេទទិន្នន័យជា int មានទំហំផ្ទុកក្នុង Memory ជា Fix size 32-bit(4 Byte) ដែលជាលេខគត់
- តម្លៃលេខតូចបំផុតគឺ -2,147,483,648 (-2^31)
- តម្លៃលេខធំបំផុតគឺ 2,147,483,647 (2^31 -1)
- តម្លៃដើមរបស់វាគឺស្មើរ 0
- ប្រភេទទិន្នន័យជា int ជាទូរទៅត្រូវបានគេប្រើប្រាស់លំនាំដើមជាលេខគត់ តែបើយើងគិតពីការសន្សំសំចៃ Memory ត្រូវប្រើប្រភេទទិន្នន័យជា byte ឫ short
ឧទាហរណ៍
int a = 100000, int b = -200000
+ ប្រភេទទិន្នន័យជា long
- ប្រភេទទិន្នន័យជា long មានទំហំផ្ទុកក្នុង Memory ជា Fix size 64-bit(8 Byte) ដែលជាលេខគត់
- តម្លៃលេខតូចបំផុតគឺ -9,223,372,036,854,775,808 (-2^63)
- តម្លៃលេខធំបំផុតគឺ -9,223,372,036,854,775,807 (2^63 -1)
- តម្លៃដើមរបស់វាគឺស្មើរ 0L / 0l (ប្រកាស់ប្រើជាអក្សរអិលធំកុំអោយច្រឡំលេខ១)
- ប្រភេទទិន្នន័យជា long ជាទូរទៅត្រូវបានគេប្រើប្រាស់ជាលេខគត់ ដែលមានច្រើនខ្ទង់ តែវាស៊ីទំហំ Memory ច្រើនដូច្នេះសូមប្រុងប្រយ័ត្នក្នុងការប្រើប្រាស់វា
ឧទាហរណ៍
long a = 100000L, long b = -200000L
លំហាត់នៅផ្ទះ
Declaration and Initialization:
Declare a variable named myByte of type byte and initialize it with a value between -128 and 127.
Declare a variable named myShort of type short and initialize it with a value between -32,768 and 32,767.
Declare a variable named myInt of type int and initialize it with a value between -2,147,483,648 and 2,147,483,647.
Declare a variable named myLong of type long and initialize it with a value beyond the range of int.
Arithmetic Operations:
Declare a byte variable named byteNum and a short variable named shortNum.
Perform the addition of byteNum and shortNum, storing the result in an int variable named result1.
Declare an int variable named intNum and a long variable named longNum.
Perform the multiplication of intNum and longNum, storing the result in a long variable named result2.
3. Conversion and Output:
Declare a byte variable named conversionByte and initialize it with a value.
Declare an int variable named conversionInt and initialize it with the value of conversionByte.
Print the values of conversionByte and conversionInt to the console.
4. Overflow and Underflow:
Declare a byte variable named overflowByte and initialize it with the maximum value for a byte.
Increment overflowByte by 1 and print the result.
Declare a short variable named underflowShort and initialize it with the minimum value for a short.
Decrement underflowShort by 1 and print the result.
Ref:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
https://www.javatpoint.com/java-data-types
https://www.tutorialspoint.com/java/java_basic_datatypes.htm
រៀបរៀងដោយ @សរសេរកូដជាមួយចិត្រា @codingwithchitra
កម្រងវីដេអូមេរៀន|Play List 👉 https://youtube.com/playlist?list=PLSJTJj3gU6qhkjSEUmgBKGJnn0ob1NZSu&si=rcvL8CwF_5EGx_2n
YouTube Channel 👉 https://www.youtube.com/@codingwithchitra
Facebook Page 👉 https://www.facebook.com/khmshared
Telegram Channel 👉 https://t.me/codingwithchitra