JavaJava · Chapterبەش 4 · Lectureوانە 4.2
Primitive Data Typesجۆرە سەرەتاییەکان
Java's eight primitive types: the integer types and their ranges, literals with L, integer overflow, float and double with E notation and precision (why 0.1 + 0.2 is not 0.3), boolean, and char as a 16-bit Unicode number that holds Kurdish letters but not emoji.
هەشت Java: جۆرەکانی و سنوورەکانیان، بە L، ، float و double لەگەڵ و وردی (بۆچی 0.1 + 0.2 ناکاتە 0.3)، boolean و char وەک ژمارەیەکی 16 کە پیتی کوردی هەڵدەگرێت بەڵام نا.
What you will learnچی فێر دەبیت
- Name Java's eight primitive types with their sizes, and tell primitive from reference types.
- Choose an integer type for a value, write
longliterals withL, and explain integer overflow. - Use
doubleandfloatcorrectly, read E notation and explain why0.1 + 0.2is not exactly0.3. - Use
booleanandcharvalues, escape sequences and character numbers, including Kurdish letters.
- هەشت Java لەگەڵ قەبارەکانیان ناو بهێنیت و لە جیا بکەیتەوە.
- جۆرێکی بۆ هەڵبژێریت،
longبەLبنووسیت و ڕوون بکەیتەوە. doubleوfloatبە دروستی بەکار بهێنیت، بخوێنیتەوە و ڕوونی بکەیتەوە بۆچی0.1 + 0.2ڕێک0.3نییە.-
booleanوchar، و ژمارەی بەکار بهێنیت، پیتی کوردیشەوە.
Every value has a typeهەموو نرخێک جۆرێکی هەیە
Java is a strongly typed language: every variable has a type that is fixed when the program is compiled (written in the declaration, or worked out by the compiler with var), and the compiler checks the types before the program runs (; see Static and dynamic typing). That is why int age = "twenty"; never runs: javac stops it. Java's types form two groups:
Java بە جۆری توند کار دەکات: هەموو جۆرێکی هەیە کە لە کاتی کۆمپایلکردندا جێگیر دەبێت (لە ناساندنەکەدا دەنووسرێت، یان بە var خۆی دەیدۆزێتەوە) و پێش جێبەجێبوونی جۆرەکان دەپشکنێت (؛ بڕوانە جۆرداریی جێگیر و جۆرداریی ناجێگیر). بۆیە int age = "twenty"; هەرگیز جێبەجێ نابێت: javac ڕایدەگرێت. جۆرەکانی Java دوو گرووپن:
- : eight simple types built into the language, such as
intanddouble. A variable of a primitive type holds the value itself. - (non-primitive types):
String, arrays and every class. A variable of a reference type holds a to an object (What new does in memory).Stringhas its own chapter: Strings.
- : هەشت جۆری سادە کە لە ناو زمانەکەدا دروست کراون، وەک
intوdouble. خودی هەڵدەگرێت. - (جۆرە ناسەرەتاییەکان):
String، و هەموو . بۆ هەڵدەگرێت (newلە بیرگەدا چی دەکات).Stringبەشی تایبەتی خۆی هەیە: زنجیرەنووسەکان.
The eight primitive typesهەشت جۆرە سەرەتاییەکە
Java has exactly eight primitive types: four for whole numbers, two for numbers with a fraction, one for a single character and one for true or false. Their names are all in small letters. Each one also has a class partner, such as Integer for int, which lists need (Wrapper classes).
Java ڕێک هەشت هەیە: چواریان بۆ ، دوویان بۆ ژمارەی ، یەکێکیان بۆ تاکە و یەکێکیان بۆ ڕاست یان هەڵە. ناوەکانیان هەمووی بە پیتی بچووکن. هەریەکەیان هاوتاشی هەیە، وەک Integer بۆ int، کە لیستەکان پێویستیان پێیەتی (کلاسە پێچەرەوەکان).
| Typeجۆر | Holdsهەڵدەگرێت | Sizeقەبارە |
|---|---|---|
byte short int long | whole numbers; int is the usual choice؛ int هەڵبژاردنی ئاساییە | 8, 16, 32, 64 bits8، 16، 32، 64 |
float double | numbers with a fraction; double is the usual choiceژمارەی ؛ double هەڵبژاردنی ئاساییە | 32, 64 bits32، 64 |
char | one characterیەک | 16 bitsقەبارەی 16 |
boolean | true or falsetrue یان false | not fixedدیاری نەکراوە |
Whole numbers: byte, short, int and longژمارە تەواوەکان: byte، short، int و long
The integer types hold numbers without a fraction, and negative numbers are allowed. They differ only in size: a type with n bits holds to , because one bit holds the sign (Java uses two's complement, see The 2's complement wheel).
جۆرەکانی ژمارەی بێ هەڵدەگرن و ژمارەی نەرێنیش ڕێگە پێدراوە. تەنیا لە قەبارەدا جیاوازن: جۆرێک بە n لە تا هەڵدەگرێت، چونکە یەک نیشانەکە هەڵدەگرێت (Java بەکار دەهێنێت، بڕوانە بازنەی تەواوکەری دوو).
public class IntegerRanges {
public static void main(String[] args) {
System.out.println("byte: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
System.out.println("short: " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
System.out.println("int: " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
System.out.println("long: " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
}
}$ java IntegerRanges byte: -128 to 127 short: -32768 to 32767 int: -2147483648 to 2147483647 long: -9223372036854775808 to 9223372036854775807
Choose the type that fits. Usually that is int. long is for counts beyond about 2.1 billion; byte and short are mainly for special jobs, such as handling files byte by byte or saving memory in very large lists of numbers.
ئەو جۆرە هەڵبژێرە کە دەگونجێت. زۆرجار ئەوە intـە. long بۆ ژماردنی زیاتر لە نزیکەی 2.1 ملیارە؛ byte و short زیاتر بۆ کاری تایبەتن، وەک کارکردن لەسەر بە ، یان پاشەکەوتکردنی لە لیستی زۆر گەورەی ژمارەدا.
Writing whole numbers: literals and Lنووسینی ژمارە تەواوەکان: لیتەراڵەکان و L
A value written straight into the code is a . These four declarations store one value of each integer type:
کە ڕاستەوخۆ لە کۆدەکەدا نووسرابێت . ئەم چوار ناساندنە هەریەکەیان یەکێک لە جۆرەکانی هەڵدەگرێت:
public class IntegerTypes {
public static void main(String[] args) {
byte smallNum = 100;
short mediumNum = 5000;
int num = 100000;
long bigNum = 15000000000L; // L makes it a long
System.out.println(smallNum);
System.out.println(mediumNum);
System.out.println(num);
System.out.println(bigNum);
}
}$ java IntegerTypes 100 5000 100000 15000000000
A whole-number literal is an int unless it ends with L. So long bigNum = 15000000000; does not compile: the literal alone is too big for an int (integer number too large). Write a capital L, because a small l looks like the digit 1. A value outside the variable's range is refused too: byte b = 128; gives incompatible types: possible lossy conversion from int to byte (see When a small type needs no cast).
intـە، مەگەر بە L کۆتایی بێت. بۆیە long bigNum = 15000000000; کۆمپایل نابێت: بە تەنیا بۆ int زۆر گەورەیە (integer number too large). Lـی گەورە بنووسە، چونکە lـی بچووک وەک 1 دەردەکەوێت. کە لە سنووری دەرچێت ڕەت دەکرێتەوە: byte b = 128; دەدات incompatible types: possible lossy conversion from int to byte (بڕوانە کەی جۆرێکی بچووک پێویستی بە (type) نییە).
Writing whole numbers: literals and Lنووسینی ژمارە تەواوەکان: لیتەراڵەکان و L
When int is too smallکاتێک int بەس نییە
To count the people on Earth you need a long. The United Nations estimated about 8.2 billion people in 2024, almost four times the largest int. Underscores make long literals easier to read; Java ignores them.
بۆ ژماردنی خەڵکی سەر زەوی longـت پێویستە. نەتەوە یەکگرتووەکان لە ساڵی 2024دا نزیکەی 8.2 ملیار کەسی خەمڵاند، نزیکەی چوار ئەوەندەی گەورەترین int. درێژ ئاسانتر دەکەن بۆ خوێندنەوە؛ Java گوێیان پێ نادات.
longـی پێویستەCêITpublic class People {
public static void main(String[] args) {
long people = 8_200_000_000L; // underscores only help you read
int intMax = Integer.MAX_VALUE;
System.out.println(people);
System.out.println(intMax);
}
}$ java People 8200000000 2147483647
Overflow: when a number does not fitسەرڕێژبوون: کاتێک ژمارەیەک جێی نابێتەوە
What happens when a calculation goes past the largest int? Java does not stop and does not warn you: the value wraps round to the other end of the range, like a car's odometer. This is :
چی ڕوو دەدات کاتێک ژماردنێک لە گەورەترین int تێدەپەڕێت؟ Java نە دەوەستێت و نە ئاگادارت دەکاتەوە: دەسوڕێتەوە بۆ سەری تری سنوورەکە، وەک کیلۆمەترپێوی ئۆتۆمبێل. ئەمە :
public class Overflow {
public static void main(String[] args) {
int big = Integer.MAX_VALUE;
System.out.println(big);
big = big + 1; // one more than the largest int
System.out.println(big);
}
}$ java Overflow 2147483647 -2147483648


Underneath, the bits behave as in Overflow in Logic Design: 0111…1 + 1 = 1000…0, and in two's complement that pattern is the most negative number. Below the smallest value it wraps the other way: Integer.MIN_VALUE - 1 gives 2147483647. An 8-bit register of a microcontroller wraps round in the same way: Common mistake: 258 and −2.
لە ژێرەوە، وەک سەرڕێژبوون لە دیزاینی ڕەفتار دەکەن: 0111…1 + 1 = 1000…0 و لە ئەو شێوەیە نەرێنیترین ژمارەیە. لە خوار بچووکترین بە پێچەوانەوە دەسوڕێتەوە: Integer.MIN_VALUE - 1 دەدات 2147483647. لە ژمارەیەکی 8 بە هەمان شێوە دەسوڕێتەوە: هەڵەی باو: 258 و −2.
Overflow: when a number does not fitسەرڕێژبوون: کاتێک ژمارەیەک جێی نابێتەوە
A real overflow bugباگێکی ڕاستەقینەی سەرڕێژبوون
Overflow hides in ordinary calculations. How many milliseconds are there in 30 days?
لە ناو ژماردنی ئاساییدا خۆی دەشارێتەوە. 30 ڕۆژ چەند میلیچرکەیە؟
public class Month {
public static void main(String[] args) {
int msInt = 30 * 24 * 60 * 60 * 1000; // milliseconds in 30 days
long msWrong = 30 * 24 * 60 * 60 * 1000; // still int arithmetic!
long msRight = 30L * 24 * 60 * 60 * 1000; // long arithmetic
System.out.println(msInt);
System.out.println(msWrong);
System.out.println(msRight);
}
}$ java Month -1702967296 -1702967296 2592000000
The right answer is 2 592 000 000, more than an int can hold. 30 * 24 * 60 * 60 * 1000 is worked out with ints and overflows before the result is stored, so even the long variable msWrong gets the wrong number. Writing 30L makes the whole calculation long.
وەڵامی ڕاست 2 592 000 000ـە، زیاتر لەوەی int دەتوانێت هەڵیبگرێت. 30 * 24 * 60 * 60 * 1000 بە int دەژمێردرێت و پێش هەڵگرتنی ئەنجامەکە سەرڕێژ دەبێت، بۆیە تەنانەت longـی msWrongـیش ژمارە هەڵەکە وەردەگرێت. نووسینی 30L هەموو ژماردنەکە دەکات بە long.
Numbers with a fraction: float and doubleژمارە کەرتییەکان: float و double
Real numbers, numbers with a fraction such as 9.99 or 3.14515, use float (32 bits) or double (64 bits). Both follow the IEEE 754 standard and can hold very large and very small numbers. Normally you should use double: it is more precise, and a literal such as 9.99 is a double already.
ژمارە ڕاستەقینەکان، واتە ژمارەی وەک 9.99 یان 3.14515، float (32 ) یان double (64 ) بەکار دەهێنن. هەردووکیان پەیڕەوی ستانداردی IEEE 754 دەکەن و دەتوانن ژمارەی زۆر گەورە و زۆر بچووک هەڵبگرن. بە ئاسایی double بەکار بهێنە: وردترە و وەک 9.99 خۆی doubleـە.
| Typeجۆر | Positive rangeسنووری ئەرێنی | Negative rangeسنووری نەرێنی |
|---|---|---|
float | 1.4E-45 … 3.4028235E38 | -3.4028235E38 … -1.4E-45 |
double | 4.9E-324 … 1.7976931348623157E308 | -1.7976931348623157E308 … -4.9E-324 |
public class FloatRanges {
public static void main(String[] args) {
System.out.println(Float.MAX_VALUE);
System.out.println(Float.MIN_VALUE);
System.out.println(Double.MAX_VALUE);
System.out.println(Double.MIN_VALUE);
}
}$ java FloatRanges 3.4028235E38 1.4E-45 1.7976931348623157E308 4.9E-324
Numbers with a fraction: float and doubleژمارە کەرتییەکان: float و double
E notation: very big and very small numbersشێوازی زانستی: ژمارەی زۆر گەورە و زۆر بچووک
3.4028235E38 uses : it means . You may write literals this way too, and println uses it for doubles of 10 000 000 or more and below 0.001:
3.4028235E38 بەکار دەهێنێت: واتە . دەتوانیت بەم شێوەیەش بنووسیت و println بۆ doubleـی 10 000 000 و زیاتر و بچووکتر لە 0.001 بەکاری دەهێنێت:
public class Notation {
public static void main(String[] args) {
double sun = 1.496e8; // km from the Earth to the Sun
double light = 3e8; // metres per second
double charge = 1.6e-19; // an electron, in coulombs
System.out.println(sun);
System.out.println(light);
System.out.println(charge);
System.out.println(1234567.0);
System.out.println(12345678.0);
}
}$ java Notation 1.496E8 3.0E8 1.6E-19 1234567.0 1.2345678E7
float or double? The f suffix and precisionfloat یان double؟ پاشگری f و وردی
These lines store a float and a double and divide 1 by 3 with each type:
ئەم دێڕانە floatـێک و doubleـێک هەڵدەگرن و بە هەر جۆرێک 1 دابەش بەسەر 3دا دەکەن:
public class FloatDouble {
public static void main(String[] args) {
float price = 5.75f; // f: a float literal
double weight = 19.99d; // d is allowed, but not needed
System.out.println(price);
System.out.println(weight);
System.out.println(1.0f / 3); // float
System.out.println(1.0 / 3); // double
}
}$ java FloatDouble 5.75 19.99 0.33333334 0.3333333333333333
A decimal literal is a double, so a float needs the suffix f (or F): float price = 5.75; gives incompatible types: possible lossy conversion from double to float. The d is allowed but never needed. Precision: a float keeps only about 6–7 significant digits, a double about 15–16. Compare 1.0f / 3 and 1.0 / 3 above.
دەیی doubleـە، بۆیە float پاشگری f (یان F)ی پێویستە: float price = 5.75; دەدات incompatible types: possible lossy conversion from double to float. d ڕێگە پێدراوە بەڵام هەرگیز پێویست نییە. وردی: float تەنیا نزیکەی 6 تا 7 گرنگ دەپارێزێت و double نزیکەی 15 تا 16. 1.0f / 3 و 1.0 / 3ـی سەرەوە بەراورد بکە.
Why 0.1 + 0.2 is not 0.3بۆچی 0.1 + 0.2 ناکاتە 0.3
Java stores a double in binary, and most decimal fractions, such as 0.1, have no exact binary form (see Fractions that never end). Each one is stored as the nearest binary value, and the tiny differences can show:
Java double بە هەڵدەگرێت و زۆربەی دەیی، وەک 0.1، هیچ شێوەیەکی ورد و تەواویان نییە (بڕوانە کەرتەکان کە کۆتاییان نایەت). هەریەکەیان وەک نزیکترین هەڵدەگیرێت و جیاوازییە زۆر بچووکەکان دەتوانن دەربکەون:
public class Precision {
public static void main(String[] args) {
System.out.println(0.1 + 0.2);
System.out.println(0.1 + 0.2 == 0.3);
double difference = Math.abs((0.1 + 0.2) - 0.3);
System.out.println(difference < 0.000001);
}
}$ java Precision 0.30000000000000004 false true
So never compare doubles with ==; check that the difference is very small instead (Math.abs gives the difference without its sign: Absolute value). For money, count whole units such as dinars in an int or long. (A float prints 0.1f + 0.2f as 0.3 only because it prints fewer digits; it is not exact either.)
بۆیە هەرگیز doubleـەکان بە == بەراورد مەکە؛ لە جیاتی ئەوە بپشکنە کە جیاوازییەکەیان زۆر بچووکە (Math.abs جیاوازییەکە بەبێ نیشانەکەی دەدات: نرخی ڕووت). بۆ پارە، یەکەی تەواو وەک دینار لە int یان longـدا بژمێرە. (float 0.1f + 0.2f وەک 0.3 چاپ دەکات تەنیا چونکە کەمتر چاپ دەکات؛ ئەویش ورد نییە.)
boolean: true or falseboolean: ڕاست یان هەڵە
The type holds a truth value. It has only two possible values, standing for on or off, yes or no: true and false. Java reserves these two words for them; they are boolean , so they cannot be names either.
جۆری ڕاستی هەڵدەگرێت. تەنیا دوو هەیە، بۆ داگیرساو یان کوژاوە، بەڵێ یان نەخێر: true و false. Java ئەم دوو وشەیەی بۆ ئەوان هەڵگرتووە؛ booleanـن، بۆیە ناشتوانن ببن بە ناو.
public class BooleanDemo {
public static void main(String[] args) {
boolean lightOn = true;
boolean passed = false;
System.out.println(lightOn);
System.out.println(passed);
int age = 20;
boolean isAdult = age >= 18; // a comparison gives a boolean
System.out.println(isAdult);
}
}$ java BooleanDemo true false true
Comparisons such as age >= 18 give booleans, and decisions use them (Boolean expressions). Java never mixes numbers and booleans: boolean lightOn = 1; gives incompatible types: int cannot be converted to boolean.
بەراوردکردنی وەک age >= 18 دەدات و بڕیارەکان بەکاری دەهێنن (دەربڕینە بوولییەکان). Java هەرگیز ژمارە و تێکەڵ ناکات: boolean lightOn = 1; دەدات incompatible types: int cannot be converted to boolean.
char: one characterchar: یەک نووسە
A char stores exactly one : a letter, a digit, a symbol, or whitespace such as a tab or a new line. It is written between single quotes, like 'A' or 'c'. Double quotes make a String, even for one letter: char grade = "A"; gives incompatible types: String cannot be converted to char. Characters that are hard to type are written with an , a backslash and a letter, the same ones as in strings:
char ڕێک یەک هەڵدەگرێت: پیتێک، ، هێمایەک، یان وەک تاب یان دێڕی نوێ. لە نێوان دوو دەنووسرێت، وەک 'A' یان 'c'. String دروست دەکات، تەنانەت بۆ یەک پیت: char grade = "A"; دەدات incompatible types: String cannot be converted to char. کە نووسینی قورس بێت بە دەنووسرێت: و پیتێک، هەمان ئەوانەی لە زنجیرەنووسەکاندا:
public class CharType {
public static void main(String[] args) {
char letter = 'a';
char tab = '\t';
char newLine = '\n';
char backslash = '\\';
char singleQuote = '\'';
char doubleQuote = '\"';
System.out.println("[" + letter + "]");
System.out.println("[" + tab + "]");
System.out.println("[" + newLine + "]");
System.out.println("[" + backslash + "]");
System.out.println("[" + singleQuote + "]");
System.out.println("[" + doubleQuote + "]");
}
}$ java CharType [a] [ ] [ ] [\] ['] ["]
A char is a numberchar ژمارەیەکە
Inside the computer every character is a number, its in . A char has 16 bits, so it holds a number from 0 to 65 535, and you can turn a char into its number and back:
لە ناو هەموو ژمارەیەکە، واتە لە . char 16 هەیە، بۆیە ژمارەیەک لە 0 تا 65 535 هەڵدەگرێت و دەتوانیت charـێک بگۆڕیت بۆ ژمارەکەی و بە پێچەوانەوە:
public class CharNumber {
public static void main(String[] args) {
char first = 'A';
System.out.println((int) first); // its number
System.out.println((char) 66); // number 66 as a char
System.out.println(first + 1); // char + int gives an int
System.out.println((char) (first + 1)); // back to a char
}
}$ java CharNumber 65 B 66 B
Arithmetic with a char gives an int: first + 1 is 66. A cast back to char turns a number into a letter again (There and back: char, byte and short). The numbers are the ones of Unicode.
ژماردن بە char int دەدات: first + 1 دەکاتە 66. گۆڕینەوە بۆ char ژمارەکە دەکاتەوە بە پیت (بۆ double و گەڕانەوە: char، byte و short). ژمارەکان هەمان ژمارەکانی Unicodeن.
A char is a numberchar ژمارەیەکە
Kurdish letters in a charپیتی کوردی لە charـێکدا
Kurdish letters are Unicode characters too, and each one fits in a single char: ڤ is 1700 (U+06A4) and ێ is 1742 (U+06CE). You can also write a character by its hexadecimal number with \u:
پیتە کوردییەکانیش و هەریەکەیان لە یەک charـدا جێی دەبێتەوە: ڤ 1700ـە (U+06A4) و ێ 1742ـە (U+06CE). دەتوانیت بە ژمارەکەیشی بنووسیت، بە و \u:
public class KurdishLetters {
public static void main(String[] args) {
char ve = 'ڤ';
char ee = 'ێ';
System.out.println(ve + " is number " + (int) ve);
System.out.println(ee + " is number " + (int) ee);
char sameVe = '\u06A4'; // the same letter, written with its hex number
System.out.println(sameVe);
System.out.println(ve == sameVe);
}
}$ java KurdishLetters ڤ is number 1700 ێ is number 1742 ڤ true
A char is a numberchar ژمارەیەکە
Emoji do not fit in a charئیمۆجی لە charـێکدا جێی نابێتەوە
A few characters have numbers above 65 535, most among them: 😀 is 128 512 (U+1F600). Java stores such a character as two chars. javac 17 does not even complain about '😀': it silently keeps only the first half, 55357, which prints as ?:
هەندێک ژمارەکەیان لە 65 535 زیاترە، زۆربەی لەوانەن: 😀 128 512ـە (U+1F600). Java ئەو جۆرە وەک دوو char هەڵدەگرێت. javac 17 تەنانەت گلەیی لە '😀' ناکات: بە بێدەنگی تەنیا نیوەی یەکەمی دەهێڵێتەوە، 55357، کە وەک ? چاپ دەبێت:
public class Smile {
public static void main(String[] args) {
char smile = '😀';
System.out.println((int) smile);
System.out.println(smile);
}
}$ java Smile 55357 ?
Use a String for emoji: String smile = "😀"; prints 😀, and smile.length() is 2 because it holds two chars (Kurdish text in strings).
بۆ String بەکار بهێنە: String smile = "😀"; 😀 چاپ دەکات و smile.length() دەکاتە 2 چونکە دوو char هەڵدەگرێت (دەقی کوردی لە زنجیرەنووسەکاندا).
Common mistakesهەڵە باوەکان
| You wroteنووسیت | Java saysJava دەڵێت | Fixچارەسەر |
|---|---|---|
long n = 15000000000; | integer number too large | Add L: 15000000000L.L زیاد بکە: 15000000000L. |
byte b = 128; | incompatible types: possible lossy conversion from int to byte | Use a bigger type, such as short or int.جۆرێکی گەورەتر بەکار بهێنە، وەک short یان int. |
float f = 5.75; | incompatible types: possible lossy conversion from double to float | Write 5.75f, or use double.5.75f بنووسە، یان double بەکار بهێنە. |
char c = "A"; | incompatible types: String cannot be converted to char | Single quotes for a char: 'A'. بۆ char: 'A'. |
char c = 'AB'; | unclosed character literal | One character only, or a String: "AB".تەنیا یەک ، یان String: "AB". |
boolean on = 1; | incompatible types: int cannot be converted to boolean | Use true or false.true یان false بەکار بهێنە. |
0.1 + 0.2 == 0.3 | (no message: false)(هیچ پەیامێک: false) | Check that the difference is tiny.بپشکنە کە جیاوازییەکە زۆر بچووکە. |
30 * 24 * 60 * 60 * 1000 | (no message: -1702967296)(هیچ پەیامێک: -1702967296) | Calculate in long: 30L * 24 * ….بە long بژمێرە: 30L * 24 * …. |
long a = 15000000000l; | (no message, but l looks like 1)(هیچ پەیامێک، بەڵام l وەک 1 دەردەکەوێت) | Always a capital L.هەمیشە Lـی گەورە. |
Practiceڕاهێنان
Choose the best type for each value and declare it: a name (Lana), an age (19), a height in metres (1.62), a grade letter (B), passed or not (yes), a phone number (07501234567) and the people on Earth (8.2 billion).
باشترین جۆر بۆ هەر هەڵبژێرە و بیناسێنە: ناوێک (Lana)، تەمەنێک (19)، باڵایەک بە مەتر (1.62)، پیتی نمرەیەک (B)، دەرچوو یان نا (بەڵێ)، ژمارەی مۆبایلێک (07501234567) و خەڵکی سەر زەوی (8.2 ملیار).
Hintئاماژە
Ask: is it a whole number, a fraction, one character, yes/no, or text?
بپرسە: ، ، یەک ، بەڵێ/نەخێرە، یان دەقە؟
Show answerوەڵامەکە پیشان بدە
public class Student {
public static void main(String[] args) {
String name = "Lana";
int age = 19;
double height = 1.62;
char grade = 'B';
boolean passed = true;
String phone = "07501234567"; // text: keeps the first 0
long people = 8_200_000_000L;
System.out.println(name + " " + age + " " + height + " " + grade + " " + passed);
System.out.println(phone + " " + people);
}
}$ java Student Lana 19 1.62 B true 07501234567 8200000000
The phone number is a String: you never calculate with it, and a number type would lose the first 0. 8.2 billion needs a long.
ژمارەی مۆبایلەکە Stringـە: هەرگیز ژماردنی پێ ناکەیت و جۆرێکی ژمارەیی سفری سەرەتا ون دەکات. 8.2 ملیار longـی پێویستە.
What does each line print? 'A' + 2 · (char) ('A' + 2) · Short.MAX_VALUE + 1 · 2.0f / 3 · 7 / 2
هەر دێڕێک چی چاپ دەکات؟ 'A' + 2 · (char) ('A' + 2) · Short.MAX_VALUE + 1 · 2.0f / 3 · 7 / 2
Show answerوەڵامەکە پیشان بدە
public class Predict {
public static void main(String[] args) {
System.out.println('A' + 2);
System.out.println((char) ('A' + 2));
System.out.println(Short.MAX_VALUE + 1);
System.out.println(2.0f / 3);
System.out.println(7 / 2);
}
}$ java Predict 67 C 32768 0.6666667 3
'A' + 2is anint: 65 + 2 = 67.- The cast turns 67 back into a letter: C.
Short.MAX_VALUE + 1is 32768: the sum is calculated as anint, so nothing overflows.- A
floatshows about 7 digits: 0.6666667. - Two
ints give anint: 3 (Operators).
'A' + 2intـە: 65 + 2 = 67.- گۆڕینەوەکە 67 دەکاتەوە بە پیت: C.
Short.MAX_VALUE + 1دەکاتە 32768: کۆکردنەوەکە بەintدەکرێت، بۆیە هیچ نییە.floatنزیکەی 7 پیشان دەدات: 0.6666667.- دوو
intintـێک دەدەن: 3 (کارپێکەرەکان).
byte b = 127; is followed by b++; (add 1). What does System.out.println(b); print, and why?
دوای byte b = 127;، b++; (1 زیاد بکە) دێت. System.out.println(b); چی چاپ دەکات و بۆچی؟
Show answerوەڵامەکە پیشان بدە
public class ByteWrap {
public static void main(String[] args) {
byte b = 127;
b++;
System.out.println(b);
}
}$ java ByteWrap -128
127 is the largest byte; one more wraps round to the smallest, −128.
127 گەورەترین byteـە؛ یەکێکی تر دەسوڕێتەوە بۆ بچووکترین، −128.
Print the Unicode number of each letter of «کورد» with (int), as in Kurdish letters in a char.
ژمارەی هەر پیتێکی «کورد» بە (int) چاپ بکە، وەک لە پیتی کوردی لە charـێکدا.
Show answerوەڵامەکە پیشان بدە
public class KurdishCodes {
public static void main(String[] args) {
System.out.println((int) 'ک');
System.out.println((int) 'و');
System.out.println((int) 'ر');
System.out.println((int) 'د');
}
}$ java KurdishCodes 1705 1608 1585 1583
In hexadecimal these are U+06A9, U+0648, U+0631 and U+062F, as in Kurdish in Unicode.
بە ئەمانە U+06A9، U+0648، U+0631 و U+062Fن، وەک لە کوردی لە Unicodeـدا.
Summaryپوختە
- Java is strongly and statically typed. Its types are primitive (the value itself) or reference types (a reference to an object, such as
String). - Eight primitives:
byte,short,int,long(8–64 bits),float,double,char(16 bits) andboolean. - Integer literals are
intunless they end inL; integer overflow wraps round silently. - Decimal literals are
double;floatneedsf. Doubles are binary approximations:0.1 + 0.2is0.30000000000000004. - A
charis one 16-bit Unicode unit in single quotes: Kurdish letters fit, emoji need aString.
- Java جۆری توند و بەکار دەهێنێت. جۆرەکانی یان سەرەتایین (خودی ) یان ئاماژەیی ( بۆ ، وەک
String). - هەشت :
byte،short،int،long(8 تا 64 )،float،double،char(16 ) وboolean. -
intن مەگەر بەLکۆتایی بێن؛ بە بێدەنگی دەسوڕێتەوە. - دەیی
doubleن؛floatfـی پێویستە. doubleـەکان نزیککراوەی :0.1 + 0.2دەکاتە0.30000000000000004. charیەک یەکەی 16 لە ناو : پیتی کوردی جێی دەبێتەوە، Stringـی پێویستە.
Why does long n = 15000000000; not compile?بۆچی long n = 15000000000; کۆمپایل نابێت؟
Without L the literal is an int, and it is too large for one, before it ever reaches the long.
بەبێ L intـە و بۆ int زۆر گەورەیە، پێش ئەوەی بگاتە long.
What is the difference between 'A' and "A"?جیاوازیی نێوان 'A' و "A" چییە؟
'A' is a char, the number 65; "A" is a String object that holds one character.
'A' charـە، ژمارەی 65؛ "A" Stringـە کە یەک هەڵدەگرێت.
Which type should you use for money?بۆ پارە کام جۆر بەکار بهێنیت؟
Whole units such as dinars in an int or long, or BigDecimal; not float or double.
یەکەی تەواو وەک دینار لە int یان longـدا، یان BigDecimal؛ نەک float یان double.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Type Casting: Widening and Narrowingگۆڕینی جۆر: فراوانکردنی جۆر و تەسککردنەوەی جۆر
Related lecturesوانە پەیوەندیدارەکان
- Type Casting: Widening and Narrowingگۆڕینی جۆر: فراوانکردنی جۆر و تەسککردنەوەی جۆر
- Strings and String Methodsزنجیرەنووسەکان و میتۆدەکانی String
- Arithmetic, Increment and Assignment Operatorsکارپێکەرە ژمێرەییەکان و کارپێکەرەکانی دانانی نرخ،
++و-- - Errors and Debugging: Syntax, Runtime and Logic Errorsهەڵەکان و کرمەکوژی: هەڵەی داڕستن، هەڵەی کاتی جێبەجێکردن و هەڵەی لۆجیکی
- Adding and Subtracting Signed Numbersکۆکردنەوە و لێدەرکردنی ژمارە نیشانەدارەکان
- ASCII and Character CodesASCII و کۆدەکانی نووسە
- Python Next to JavaPython لە تەنیشت Javaـەوە
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- W3Schools: Java Data Types
- W3Schools: Java Numbers
- W3Schools: Java Booleans
- W3Schools: Java Characters
- W3Schools: Non-Primitive Data Types
- The Java Language Specification, Java SE 17 — chapter 4: types and values (§4.2 primitive types)
- The Java Language Specification, Java SE 17 — §3.10 literals (L, f, underscores, char literals, escapes)
- The Java Tutorials: Primitive Data Types (Oracle)
- JEP 400: UTF-8 by Default (JDK 18)
- OpenJDK bug JDK-8354908: javac mishandles supplementary character in character literal
- UN DESA: launch statement of World Population Prospects 2024 (8.2 billion people in 2024)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.