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⁩ کە پیتی کوردی هەڵدەگرێت بەڵام نا.

15 min read14 خولەک خوێندنەوە · 19 stepsهەنگاو

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 long literals with L, and explain integer overflow.
  • Use double and float correctly, read E notation and explain why 0.1 + 0.2 is not exactly 0.3.
  • Use boolean and char values, 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 int and double. 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). String has its own chapter: Strings.
  • : هەشت جۆری سادە کە لە ناو زمانەکەدا دروست کراون، وەک int و double. خودی هەڵدەگرێت.
  • (جۆرە ناسەرەتاییەکان): String، و هەموو . بۆ هەڵدەگرێت (new لە بیرگەدا چی دەکات). String بەشی تایبەتی خۆی هەیە: زنجیرەنووسەکان.
A tree. Java data types split into 8 primitive types, where a variable holds the value itself (whole numbers: byte, short, int, long; decimal numbers: float, double; one character: char; true or false: boolean), and reference types, where a variable holds a reference to an object (text: String; arrays; classes such as Scanner; interfaces and enums). Below: the box int age holds 21 itself; the box String name holds an arrow to the object "Lana".دارێک. جۆرەکانی دراوەی Java دابەش دەبن بۆ 8 جۆری سەرەتایی، کە گۆڕاوەکە خودی نرخەکە هەڵدەگرێت (ژمارە تەواوەکان: byte، short، int، long؛ ژمارە کەرتییەکان: float، double؛ یەک نووسە: char؛ ڕاست یان هەڵە: boolean) و جۆرە ئاماژەییەکان، کە گۆڕاوەکە ئاماژەیەک بۆ ئۆبجێکتێک هەڵدەگرێت (دەق: String؛ ڕیزەکەکان؛ کلاسی وەک Scanner؛ interface و enum). لە خوارەوە: سندووقی int age خودی 21 هەڵدەگرێت؛ سندووقی String name تیرێک بۆ ئۆبجێکتی "Lana" هەڵدەگرێت.
Java's data types: primitive and reference Java: سەرەتایی و ئاماژەییCêIT

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، کە لیستەکان پێویستیان پێیەتی (کلاسە پێچەرەوەکان).

Eight bars, one per primitive type, as long as its size in bits, each with a literal: byte 8 bits, 100; short 16 bits, 5000; int 32 bits, 100000; long 64 bits, 15000000000L; float 32 bits, 5.75f; double 64 bits, 19.99; char 16 bits, 'A'; boolean, true or false, one bit of information whose size Java does not fix, true.هەشت هێڵکاری، یەکێک بۆ هەر جۆرێکی سەرەتایی، بە درێژیی قەبارەکەی بە بیت، هەریەکەیان لەگەڵ لیتەراڵێک: byte 8 بیت، 100؛ short 16 بیت، 5000؛ int 32 بیت، 100000؛ long 64 بیت، 15000000000L؛ float 32 بیت، 5.75f؛ double 64 بیت، 19.99؛ char 16 بیت، 'A'؛ boolean، ڕاست یان هەڵە، یەک بیت زانیاری کە Java قەبارەکەی دیاری ناکات، true.
The eight primitive types and their sizesهەشت و قەبارەکانیانCêIT
TypeجۆرHoldsهەڵدەگرێتSizeقەبارە
byte short int longwhole numbers; int is the usual choice؛ int هەڵبژاردنی ئاساییە8, 16, 32, 64 bits8، ⁦16⁩، ⁦32⁩، ⁦64⁩
float doublenumbers with a fraction; double is the usual choiceژمارەی ؛ double هەڵبژاردنی ئاساییە32, 64 bits⁦32⁩، ⁦64⁩
charone characterیەک 16 bitsقەبارەی ⁦16⁩
booleantrue or falsetrue یان falsenot 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 −2n−1-2^{n-1} to 2n−1−12^{n-1}-1, because one bit holds the sign (Java uses two's complement, see The 2's complement wheel).

جۆرەکانی ژمارەی بێ هەڵدەگرن و ژمارەی نەرێنیش ڕێگە پێدراوە. تەنیا لە قەبارەدا جیاوازن: جۆرێک بە n لە −2n−1-2^{n-1} تا 2n−1−12^{n-1}-1 هەڵدەگرێت، چونکە یەک نیشانەکە هەڵدەگرێت (Java بەکار دەهێنێت، بڕوانە بازنەی تەواوکەری دوو).

Four rows of bit boxes: byte has 8 bits, short 16, int 32 and long 64; the first bit of each is marked as the sign bit. Under them the ranges: byte −128 to 127; short −32 768 to 32 767; int −2 147 483 648 to 2 147 483 647; long −9 223 372 036 854 775 808 to 9 223 372 036 854 775 807.چوار ڕیز سندووقی بیت: byte 8 بیت، short 16، int 32 و long 64؛ یەکەم بیتی هەریەکەیان بیتی نیشانەیە. لە ژێریاندا سنوورەکان: byte −128 تا 127؛ short −32 768 تا 32 767؛ int −2 147 483 648 تا 2 147 483 647؛ long −9 223 372 036 854 775 808 تا 9 223 372 036 854 775 807.
The four integer types and their rangesچوار جۆرەکەی و سنوورەکانیانCêIT
IntegerRanges.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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);
    }
}

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:

کە ڕاستەوخۆ لە کۆدەکەدا نووسرابێت . ئەم چوار ناساندنە هەریەکەیان یەکێک لە جۆرەکانی هەڵدەگرێت:

IntegerTypes.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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);
    }
}

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) نییە).

The way a literal is written decides its type: 100 is an int; 15000000000L and 8_200_000_000L are longs; 'A' is a char; 5.75 is a double; 5.75f is a float; "A" is a String; true is a boolean.شێوەی نووسینی لیتەراڵێک جۆرەکەی دیاری دەکات: 100 intـە؛ 15000000000L و 8_200_000_000L longـن؛ 'A' charـە؛ 5.75 doubleـە؛ 5.75f floatـە؛ "A" Stringـە؛ true booleanـە.
Literals and their types و جۆرەکانیانCêIT

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 گوێیان پێ نادات.

Two bars. The largest int, 2 147 483 647, is a short bar. The number of people on Earth (UN, 2024), about 8 200 000 000, is almost four times longer; a dashed line shows where int ends. Below: long people = 8_200_000_000L;دوو هێڵکاری. گەورەترین int، 2 147 483 647، هێڵکارییەکی کورتە. ژمارەی خەڵکی سەر زەوی (UN، 2024)، نزیکەی 8 200 000 000، نزیکەی چوار ئەوەندە درێژترە؛ هێڵێکی پچڕپچڕ پیشان دەدات int لە کوێ تەواو دەبێت. لە خوارەوە: long people = 8_200_000_000L;
The world's population needs a longژمارەی خەڵکی جیهان longـی پێویستەCêIT
People.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public 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);
    }
}

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 نە دەوەستێت و نە ئاگادارت دەکاتەوە: دەسوڕێتەوە بۆ سەری تری سنوورەکە، وەک کیلۆمەترپێوی ئۆتۆمبێل. ئەمە :

Overflow.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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);
    }
}
Animation of an int counter: 2147483645, 2147483646, then 2147483647, Integer.MAX_VALUE. Adding 1 makes it wrap round to −2147483648, Integer.MIN_VALUE, and it goes on counting to −2147483647. There is no error message.
Animation of an int counter: 2147483645, 2147483646, then 2147483647, Integer.MAX_VALUE. Adding 1 makes it wrap round to −2147483648, Integer.MIN_VALUE, and it goes on counting to −2147483647. There is no error message.
int wraps round like an odometerint وەک کیلۆمەترپێوی ئۆتۆمبێل دەسوڕێتەوەCêIT

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⁩ ڕۆژ چەند میلیچرکەیە؟

Month.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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);
    }
}

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سنووری نەرێنی
float1.4E-45 … 3.4028235E38-3.4028235E38 … -1.4E-45
double4.9E-324 … 1.7976931348623157E308-1.7976931348623157E308 … -4.9E-324
FloatRanges.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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);
    }
}

Numbers with a fraction: float and doubleژمارە کەرتییەکان: float و double

E notation: very big and very small numbersشێوازی زانستی: ژمارەی زۆر گەورە و زۆر بچووک

3.4028235E38 uses : it means 3.4028235×10383.4028235 \times 10^{38}. 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 بەکار دەهێنێت: واتە 3.4028235×10383.4028235 \times 10^{38}. دەتوانیت بەم شێوەیەش بنووسیت و println بۆ doubleـی ⁦10 000 000⁩ و زیاتر و بچووکتر لە ⁦0.001⁩ بەکاری دەهێنێت:

E notation, a number times a power of 10. 1.496E8 = 1.496 × 10 to the 8 = 149 600 000, the distance in km from the Earth to the Sun. 1.6E-19 = 1.6 × 10 to the −19 = 0.00000000000000000016, the charge of an electron. Below: println(12345678.0) prints 1.2345678E7.شێوازی زانستی، ژمارەیەک جارانی توانێکی 10. 1.496E8 = 1.496 × 10 بە توانی 8 = 149 600 000، دووری زەوی لە خۆر بە کیلۆمەتر. 1.6E-19 = 1.6 × 10 بە توانی −19 = 0.00000000000000000016، بارگەی ئەلیکترۆنێک. لە خوارەوە: println(12345678.0) دەنووسێت 1.2345678E7.
Reading E notationخوێندنەوەی CêIT
Notation.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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);
    }
}

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دا دەکەن:

FloatDouble.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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
    }
}

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ـی سەرەوە بەراورد بکە.

1.0f / 3 with float, 32 bits, prints 0.33333334: about 7 correct digits, then a wrong 4. 1.0 / 3 with double, 64 bits, prints 0.3333333333333333: about 16 correct digits.1.0f / 3 بە float، 32 بیت، دەنووسێت 0.33333334: نزیکەی 7 تاکژمارەی ڕاست، پاشان 4ـێکی هەڵە. 1.0 / 3 بە double، 64 بیت، دەنووسێت 0.3333333333333333: نزیکەی 16 تاکژمارەی ڕاست.
float keeps about 7 digits, double about 16float نزیکەی 7 دەپارێزێت، double نزیکەی ⁦16⁩CêIT

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⁩، هیچ شێوەیەکی ورد و تەواویان نییە (بڕوانە کەرتەکان کە کۆتاییان نایەت). هەریەکەیان وەک نزیکترین هەڵدەگیرێت و جیاوازییە زۆر بچووکەکان دەتوانن دەربکەون:

Precision.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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);
    }
}
A number line from 0 to 0.5 with a zoom on 0.3. In the zoom, three neighbouring doubles: 0.29999999999999993, 0.3 (the double nearest to 0.3) and 0.30000000000000004, which is the result of 0.1 + 0.2. No double lies between them: 0.1 + 0.2 lands one step to the right of 0.3.هێڵی ژمارەکان لە 0 تا 0.5 کە لەسەر 0.3 نزیک کراوەتەوە. لە بەشە نزیککراوەکەدا سێ doubleـی دراوسێ: 0.29999999999999993، 0.3 (نزیکترین double لە 0.3ـەوە) و 0.30000000000000004، کە ئەنجامی 0.1 + 0.2ـیە. هیچ doubleـێک لە نێوانیاندا نییە: 0.1 + 0.2 یەک هەنگاو لە لای ڕاستی 0.3 دەکەوێت.
0.1 + 0.2 lands on the next double after 0.3⁦0.1 + 0.2⁩ لەسەر doubleـی دوای ⁦0.3⁩ دەکەوێتCêIT

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ـن، بۆیە ناشتوانن ببن بە ناو.

BooleanDemo.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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);
    }
}
Two light switches: one switched on, labelled true, on or yes; one switched off, labelled false, off or no. Beside them the code boolean lightOn = true; and int age = 20; and age >= 18 gives true: a comparison gives a boolean, which has only two values, true and false.دوو سویچی گڵۆپ: یەکێکیان داگیرساو بە نیشانەی true، داگیرساو یان بەڵێ؛ ئەوی تر کوژاوە بە نیشانەی false، کوژاوە یان نەخێر. لە تەنیشتیانەوە کۆدی boolean lightOn = true; و int age = 20; و age >= 18 کە true دەدات: بەراوردکردن بوولیانێک دەدات، کە تەنیا دوو نرخی هەیە، true و false.
A boolean is like a light switch وەک سویچی گڵۆپ وایەCêIT

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. کە نووسینی قورس بێت بە دەنووسرێت: و پیتێک، هەمان ئەوانەی لە زنجیرەنووسەکاندا:

Six char literals and what they print between brackets: 'a', the letter a, prints [a]; '\t', a tab, prints a wide gap; '\n', a new line, breaks the line; '\\', a backslash, prints [\]; '\'', a single quote, prints [']; '\"', a double quote, prints ["].شەش لیتەراڵی char و ئەوەی لە نێوان کەوانەدا چاپی دەکەن: 'a'، پیتی a، [a] چاپ دەکات؛ '\t'، تاب، بۆشاییەکی پان؛ '\n'، دێڕی نوێ، دێڕەکە دەشکێنێت؛ '\\'، هێڵی لاری پێچەوانە، [\] چاپ دەکات؛ '\''، نیشانەی '، ['] چاپ دەکات؛ '\"'، نیشانەی "، ["] چاپ دەکات.
A char holds exactly one characterchar ڕێک یەک هەڵدەگرێتCêIT
CharType.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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 + "]");
    }
}

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ـێک بگۆڕیت بۆ ژمارەکەی و بە پێچەوانەوە:

CharNumber.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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
    }
}
Six chars and their numbers: 'A' is 65 (U+0041), 'B' 66, 'a' 97, '0' 48, the Kurdish letter ڤ 1700 (U+06A4) and ێ 1742 (U+06CE). Below, ڤ as 16 bits: 0000 0110 1010 0100. Latin and Kurdish letters both fit in one char.شەش char و ژمارەکانیان: 'A' 65ـە (U+0041)، 'B' 66، 'a' 97، '0' 48، پیتی کوردیی ڤ 1700 (U+06A4) و ێ 1742 (U+06CE). لە خوارەوە، ڤ وەک 16 بیت: 0000 0110 1010 0100. پیتی لاتینی و کوردی هەردووکیان لە یەک charـدا جێیان دەبێتەوە.
Every char is a numberهەموو charـێک ژمارەیەکەCêIT

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:

KurdishLetters.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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);
    }
}

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⁩، کە وەک ? چاپ دەبێت:

Smile.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Smile {
    public static void main(String[] args) {
        char smile = '😀';
        System.out.println((int) smile);
        System.out.println(smile);
    }
}
Left: the Kurdish letter ڤ is one char, 1700. Right: a smiling-face emoji needs two chars, 55357 and 56832, so it must go in a String.چەپ: پیتی کوردیی ڤ یەک charـە، 1700. ڕاست: ئیمۆجییەکی دەموچاوی پێکەنیناوی دوو charـی پێویستە، 55357 و 56832، بۆیە دەبێت بچێتە ناو Stringـێکەوە.
One char for ڤ, two for an emojiیەک char بۆ ڤ، دوو char بۆ CêIT

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 largeAdd L: 15000000000L.L زیاد بکە: 15000000000L.
byte b = 128;incompatible types: possible lossy conversion from int to byteUse a bigger type, such as short or int.جۆرێکی گەورەتر بەکار بهێنە، وەک short یان int.
float f = 5.75;incompatible types: possible lossy conversion from double to floatWrite 5.75f, or use double.5.75f بنووسە، یان double بەکار بهێنە.
char c = "A";incompatible types: String cannot be converted to charSingle quotes for a char: 'A'. بۆ char: 'A'.
char c = 'AB';unclosed character literalOne character only, or a String: "AB".تەنیا یەک ، یان String: "AB".
boolean on = 1;incompatible types: int cannot be converted to booleanUse 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ڕاهێنان

ExerciseڕاهێنانChoose a typeجۆرێک هەڵبژێرە

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وەڵامەکە پیشان بدە
Student.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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);
    }
}

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ـی پێویستە.

ExerciseڕاهێنانPredict the outputدەرخستەکە پێشبینی بکە

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وەڵامەکە پیشان بدە
Predict.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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);
    }
}
  • 'A' + 2 is an int: 65 + 2 = 67.
  • The cast turns 67 back into a letter: C.
  • Short.MAX_VALUE + 1 is 32768: the sum is calculated as an int, so nothing overflows.
  • A float shows about 7 digits: 0.6666667.
  • Two ints give an int: 3 (Operators).
  • 'A' + 2 intـە: ⁦65 + 2 = 67⁩.
  • گۆڕینەوەکە ⁦67⁩ دەکاتەوە بە پیت: C.
  • Short.MAX_VALUE + 1 دەکاتە ⁦32768⁩: کۆکردنەوەکە بە int دەکرێت، بۆیە هیچ نییە.
  • float نزیکەی 7 پیشان دەدات: ⁦0.6666667⁩.
  • دوو int intـێک دەدەن: 3 (کارپێکەرەکان).
ExerciseڕاهێنانA byte that wrapsbyteـێک کە دەسوڕێتەوە

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وەڵامەکە پیشان بدە
ByteWrap.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class ByteWrap {
    public static void main(String[] args) {
        byte b = 127;
        b++;
        System.out.println(b);
    }
}

127 is the largest byte; one more wraps round to the smallest, −128.

⁦127⁩ گەورەترین byteـە؛ یەکێکی تر دەسوڕێتەوە بۆ بچووکترین، ⁦−128⁩.

ExerciseڕاهێنانThe letters of the word Kurdپیتەکانی وشەی کورد

Print the Unicode number of each letter of «کورد» with (int), as in Kurdish letters in a char.

ژمارەی هەر پیتێکی «کورد» بە (int) چاپ بکە، وەک لە پیتی کوردی لە charـێکدا.

Show answerوەڵامەکە پیشان بدە
KurdishCodes.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
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) 'د');
    }
}

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) and boolean.
  • Integer literals are int unless they end in L; integer overflow wraps round silently.
  • Decimal literals are double; float needs f. Doubles are binary approximations: 0.1 + 0.2 is 0.30000000000000004.
  • A char is one 16-bit Unicode unit in single quotes: Kurdish letters fit, emoji need a String.
  • Java جۆری توند و بەکار دەهێنێت. جۆرەکانی یان سەرەتایین (خودی ) یان ئاماژەیی ( بۆ ، وەک String).
  • هەشت : byte، short، int، long (⁦8⁩ تا ⁦64⁩ )، float، double، char (⁦16⁩ ) و boolean.
  • intن مەگەر بە L کۆتایی بێن؛ بە بێدەنگی دەسوڕێتەوە.
  • دەیی doubleن؛ float fـی پێویستە. doubleـەکان نزیککراوەی : 0.1 + 0.2 دەکاتە 0.30000000000000004.
  • char یەک یەکەی ⁦16⁩ لە ناو : پیتی کوردی جێی دەبێتەوە، Stringـی پێویستە.
Review questionsپرسیارەکانی پێداچوونەوە
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زاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Type Casting: Widening and Narrowingگۆڕینی جۆر: فراوانکردنی جۆر و تەسککردنەوەی جۆر

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.

Appearanceڕووکار

Modeدۆخ
Styleشێواز
Coloursڕەنگەکان

Saved on this device only.تەنیا لەسەر ئەم ئامێرە پاشەکەوت دەکرێت.