JavaJava · Chapterبەش 4 · Lectureوانە 4.4

Arithmetic, Increment and Assignment Operatorsکارپێکەرە ژمێرەییەکان و کارپێکەرەکانی دانانی نرخ، ++ و --

Expressions and statements (and javac's not a statement); Java's five arithmetic operators, integer division (toward zero) and % with negative numbers (Math.floorMod, Math.floorDiv), numeric promotion, precedence and left-to-right order, brackets, compound assignment with its hidden cast, ++ and -- before and after a variable, and the lab tasks with their floating-point surprise.

و (هەروەها not a statementـی javac)؛ پێنج Java، (بەرەو سفر) و % لەگەڵ ژمارەی نەرێنی (Math.floorMod، Math.floorDiv)، یەکخستنی جۆری ، و ڕیزبەندیی لە چەپەوە بۆ ڕاست، کەوانە، += و هاوشێوەکانی لەگەڵ شاراوەیان، ++ و -- پێش و دوای و ئەرکەکانی تاقیگە لەگەڵ سەرسوڕمانی .

23 min read22 خولەک خوێندنەوە · 25 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Tell an expression from a statement, and explain javac's not a statement error.
  • Use + - * / % and predict integer division and remainders, also with negative numbers.
  • Work out expressions with precedence, left-to-right order and brackets.
  • Use compound assignment and ++/--, and explain the difference between i++ and ++i.
  • لە جیا بکەیتەوە و هەڵەی not a statementـی javac ڕوون بکەیتەوە.
  • + - * / % بەکار بهێنیت و و پێشبینی بکەیت، بە ژمارەی نەرێنیشەوە.
  • بە ، ڕیزبەندیی لە چەپەوە بۆ ڕاست و کەوانە بژمێریت.
  • += و هاوشێوەکانی و ++/-- بەکار بهێنیت و جیاوازیی i++ و ++i ڕوون بکەیتەوە.

Operators and operandsکارپێکەرەکان و کارپێکراوەکان

An is a symbol that does a job with values, such as + or *. The values it works on are its . Together they make an , which Java works out to one value. Most operators take two operands, such as a + b (a ); a few take one, such as -x (a ).

هێمایەکە کە کارێک بە دەکات، وەک + یان *. کار لەسەر چەند دەکات، کە پێیان دەگوترێت . پێکەوە دروست دەکەن کە Java دەیژمێرێت بۆ یەک . زۆربەی دوو وەردەگرن، وەک a + b ()؛ هەندێکیان یەکێک، وەک -x ().

The statement total = price * 3; with price * 3 marked as the expression: price and 3 are operands, * is the operator. Two cards: -x has one operand (unary operator); a + b has two operands (binary operator). Note: Java works out the expression, then = stores the value in total.ڕستەکە total = price * 3; کە price * 3 وەک دەربڕین نیشان دراوە: price و 3 کارپێکراوەکانن، * کارپێکەرەکەیە. دوو کارت: -x یەک کارپێکراوی هەیە (کارپێکەری یەکانی)؛ a + b دوو کارپێکراوی هەیە (کارپێکەری دووانی). تێبینی: Java دەربڕینەکە دەژمێرێت، پاشان = نرخەکە لە total هەڵدەگرێت.
An expression: operators and operands: و CêIT

This lesson teaches the arithmetic operators, ++ and --, and the assignment operators, as Python's do in Operators and operands. Java's comparison operators (==, <, …) come with if statements and the logical ones (&&, ||, !) in Logical operators.

ئەم وانەیە ، ++ و -- و فێر دەکات، وەک ئەوانەی Python لە کارپێکەرەکان و کارپێکراوەکان. Java (==، <، …) لەگەڵ ڕستەی if دێن و (&&، ||، !) لە کارپێکەرە لۆجیکییەکان.

Expressions and statementsدەربڕینەکان و ڕستەکان

An , as you saw above, works out to one value: a * c, a + b - (a * c), even a single 5 or x. A is something else: one complete instruction that does a job: it stores a value, prints something or calls a method. In Java a simple statement ends with a semicolon. A statement often holds expressions: in int z = a + b - (a * c); the part after = is an expression, and the whole line up to the ; is the statement.

، وەک لە سەرەوە بینیت، دەبێتە یەک : a * c، a + b - (a * c)، تەنانەت تەنیا 5 یان x. شتێکی ترە: یەک فەرمانی تەواوە کە کارێک ئەنجام دەدات: هەڵدەگرێت، شتێک چاپ دەکات یان بانگ دەکات. لە Javaـدا سادە بە کۆتایی دێت. زۆرجار لەخۆ دەگرێت: لە int z = a + b - (a * c);ـدا ئەو بەشەی دوای = و هەموو دێڕەکە تا ; .

The line int z = a + b - (a * c); with a green bracket over the whole line: statement, one complete instruction, ended by the semicolon. A blue box around a + b - (a * c): expression, works out to one value; for a = 6, b = 4, c = 2 it is -2. z is marked as the variable. Below, two cards. Expressions, each gives one value: a * c gives 12, 7 * x - 2 * (3 * x + 14) gives -23, x gives 5 (a = 6, c = 2, x = 5). Statements, each does a job: int z = a + b - (a * c); stores, System.out.println(z); prints, x++; changes x.دێڕی int z = a + b - (a * c); بە کەوانەیەکی سەوز بەسەر هەموو دێڕەکەدا: ڕستە، یەک فەرمانی تەواو کە بە خاڵبۆر کۆتایی دێت. چوارچێوەیەکی شین بە دەوری a + b - (a * c): دەربڕین، دەبێتە یەک نرخ؛ بۆ a = 6، b = 4، c = 2 دەکاتە -2. z وەک گۆڕاو نیشان دراوە. لە خوارەوە دوو کارت. دەربڕینەکان، هەریەکەیان یەک نرخ دەدات: a * c دەدات 12، 7 * x - 2 * (3 * x + 14) دەدات -23، x دەدات 5 (a = 6، c = 2، x = 5). ڕستەکان، هەریەکەیان کارێک دەکات: int z = a + b - (a * c); هەڵدەگرێت، System.out.println(z); چاپ دەکات، x++; x دەگۆڕێت.
The expression works out a value; the statement around it does the job دەژمێرێت؛ ئەو دەوری داوە کارەکە دەکاتCêIT

The word comes from maths, where 7x−2(3x+14)7x - 2(3x + 14) is an expression too: numbers, a variable and operators that together express one value. Java needs every operator written out, so the same expression is 7 * x - 2 * (3 * x + 14); 2(…) without *, an , is a . For x = 5 it is -23:

ئەم وشەیە لە بیرکارییەوە هاتووە، کە لەوێ 7x−2(3x+14)7x - 2(3x + 14) (یان بە نووسینی کوردی ٧س − ٢(٣س + ١٤)) هەر : ژمارە، و کە پێکەوە یەک دەردەبڕن. Java دەیەوێت هەموو بنووسرێت، بۆیە هەمان دەبێتە 7 * x - 2 * (3 * x + 14)؛ 2(…) بەبێ *، واتە ، . بۆ x = 5 دەکاتە ⁦-23⁩:

Expressions.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Expressions {
    public static void main(String[] args) {
        int a = 6, b = 4, c = 2;
        int z = a + b - (a * c);        // a statement: store the value of an expression
        System.out.println(z);          // a statement: print it
        int x = 5;
        System.out.println(7 * x - 2 * (3 * x + 14));   // maths: 7x - 2(3x + 14)
    }
}

Expressions and statementsدەربڕینەکان و ڕستەکان

A value nobody uses: not a statementنرخێک کە کەس بەکاری ناهێنێت: not a statement

In Java, not every expression may stand alone. Followed by a ;, an expression becomes a statement only when it does something: an assignment, ++ or --, a method call, or creating an object with new. A calculation whose value would just be lost is refused:

لە Javaـدا هەموو ناتوانێت بەتەنیا بێت. کە ;ـی لە دوا بێت تەنیا کاتێک دەبێتە کە کارێک بکات: ، ++ یان --، بانگکردنی یان دروستکردنی بە new. ژمێرکارییەک کە بەبێ سوود لەدەست دەچێت ڕەت دەکرێتەوە:

NotAStatement.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class NotAStatement {
    public static void main(String[] args) {
        int price = 500;
        price * 3;
    }
}
Two cards. Allowed, they do something, javac turns them into statements: total = price * 3; (assignment), count++; (++ or --), System.out.println(total); (method call). Refused, the value would be lost, javac stops with an error: price * 3;, total == 1500; and "Hi"; each give not a statement.دوو کارت. ڕێگەپێدراو، کارێک دەکەن و javac دەیانکات بە ڕستە: total = price * 3; (دانانی نرخ)، count++; (++ یان --)، System.out.println(total); (بانگکردنی میتۆد). ڕەتکراوە، نرخەکە لەدەست دەچێت و javac بە هەڵەیەک دەوەستێت: price * 3;، total == 1500; و "Hi"; هەریەکەیان not a statement دەدەن.
Only expressions that do something may stand aloneتەنیا ئەو کارێک دەکەن دەتوانن بەتەنیا بنCêIT

Use the value instead: store it (total = price * 3;) or print it (System.out.println(price * 3);). Python is less strict: there a line such as price * 3 is allowed, and its value is simply forgotten (Expressions in Python). The same rule explains why ?: cannot stand alone (Choosing a value in one line).

لە جیاتی ئەوە بەکار بهێنە: هەڵیبگرە (total = price * 3;) یان چاپی بکە (System.out.println(price * 3);). Python کەمتر توندە: لەوێ دێڕێکی وەک price * 3 ڕێگەپێدراوە و بە سادەیی لە بیر دەکرێت (دەربڕینەکان لە Pythonـدا). هەمان یاسا ڕوونی دەکاتەوە بۆچی ?: ناتوانێت بەتەنیا بێت (هەڵبژاردنی نرخێک لە یەک دێڕدا).

The five arithmetic operatorsپێنج کارپێکەرە ژمێرەییەکە

Java has five arithmetic operators. Two signs of school maths change: × is * and ÷ is / (Arithmetic signs). % is not percent: it gives the remainder of a division.

Java پێنج هەیە. دوو نیشانەی بیرکاریی قوتابخانە دەگۆڕێن: × دەبێتە * و ÷ دەبێتە / (نیشانەکانی ژماردن). % لە سەدا نییە: دابەشکردن دەدات.

A calculator showing 20 % 3 = 2, and five keys with Java's operators: + add, 34 + 1 = 35; - subtract, 34.0 - 0.1 = 33.9; * multiply (×), 300 * 30 = 9000; / divide (÷), 1.0 / 2.0 = 0.5; % remainder, not percent, 20 % 3 = 2.حاسیبەیەک کە 20 % 3 = 2 پیشان دەدات و پێنج دوگمە بە کارپێکەرەکانی Java: + کۆکردنەوە، 34 + 1 = 35؛ - لێدەرکردن، 34.0 - 0.1 = 33.9؛ * لێکدان (×)، 300 * 30 = 9000؛ / دابەشکردن (÷)، 1.0 / 2.0 = 0.5؛ % پاشماوە، نەک لە سەدا، 20 % 3 = 2.
Java's five arithmetic operators with an example eachپێنج Java، هەریەکە بە نموونەیەکCêIT
NumericOperators.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class NumericOperators {
    public static void main(String[] args) {
        System.out.println(34 + 1);        // addition
        System.out.println(34.0 - 0.1);    // subtraction
        System.out.println(300 * 30);      // multiplication
        System.out.println(1.0 / 2.0);     // division
        System.out.println(20 % 3);        // remainder
    }
}

Dividing whole numbersدابەشکردنی ژمارە تەواوەکان

When both operands of / are integers, the answer is an integer too: the fraction is cut off, toward zero. 5 / 2 is 2, not 2.5, and -5 / 2 is -2, not -2.5. This is . For a real division, make at least one operand a floating-point number: 5.0 / 2 is 2.5.

کاتێک هەردوو / بن، وەڵامەکەش : دەبڕدرێتەوە، بەرەو سفر. 5 / 2 دەکاتە 2، نەک ⁦2.5⁩ و -5 / 2 دەکاتە -2، نەک -2.5. ئەمە ـە. بۆ دابەشکردنی ڕاستەقینە، لانیکەم یەکێک لە بکە بە : 5.0 / 2 دەکاتە ⁦2.5⁩.

IntegerDivision.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class IntegerDivision {
    public static void main(String[] args) {
        System.out.println(5 / 2);      // both whole numbers
        System.out.println(-5 / 2);
        System.out.println(5.0 / 2);    // one double is enough
        System.out.println(5 / 2.0);
        System.out.println(1 / 2);
    }
}
A number line from -3 to 3. 5.0 / 2 = 2.5 sits at 2.5 and an arrow moves it to 2: 5 / 2 = 2. -5.0 / 2 = -2.5 sits at -2.5 and an arrow moves it to -2: -5 / 2 = -2. Both arrows point toward 0. Notes: when both numbers are int, the answer is an int and the fraction is dropped; one double is enough for a real division.هێڵی ژمارەکان لە -3 تا 3. 5.0 / 2 = 2.5 لەسەر 2.5ـە و تیرێک دەیبات بۆ 2: 5 / 2 = 2. -5.0 / 2 = -2.5 لەسەر -2.5ـە و تیرێک دەیبات بۆ -2: -5 / 2 = -2. هەردوو تیرەکە بەرەو 0ن. تێبینی: کاتێک هەردوو ژمارە intـن، ئەنجامەکە intـە و کەرتەکە فڕێ دەدرێت؛ یەک double بەسە بۆ دابەشکردنی ڕاستەقینە.
Integer division cuts toward 0 بەرەو 0 دەبڕێتCêIT

1 / 2 is 0: the classic bug of 9 / 5 is 1. Dividing an integer by 0 stops the program with an ArithmeticException (Runtime errors). Python's // rounds down instead, so there -5 // 2 is -3 (Python's division).

1 / 2 دەکاتە 0: هەمان ناسراوی ⁦9 / 5⁩ دەکاتە 1. دابەشکردنی بەسەر 0دا بە ArithmeticException ڕادەگرێت (هەڵەکانی کاتی جێبەجێکردن). //ـی Python لە جیاتی ئەوە بەرەو خوار نزیک دەکاتەوە، بۆیە لەوێ -5 // 2 دەکاتە -3 (دابەشکردن لە Pythonـدا).

Dividing whole numbersدابەشکردنی ژمارە تەواوەکان

Mixing types in a calculationتێکەڵکردنی جۆرەکان لە ژماردندا

Before + - * / %, Java gives both operands the same type: if one is a double, the other is widened to double; otherwise to float or long if one of them is; otherwise both become at least int (numeric promotion, a kind of widening). So byte + byte is an int, and a char takes part as its number (A char is a number):

پێش + - * / %، Java هەردوو دەکات بە هەمان جۆر: ئەگەر یەکێکیان double بێت، ئەوی تر دەبێتە double؛ ئەگەر نا، ئەگەر یەکێکیان float یان long بێت دەبنە ئەو جۆرە؛ ئەگەر نا، هەردووکیان لانیکەم دەبنە int (numeric promotion، جۆرێکە لە فراوانکردنی جۆر). بۆیە byte + byte جۆری intـە و char بە ژمارەکەی بەشداری دەکات (charـێک ژمارەیەکە):

Promotion.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Promotion {
    public static void main(String[] args) {
        int apples = 7;
        double kilos = 2.5;
        System.out.println(apples * kilos);      // int * double gives a double
        byte a = 10, b = 20;
        int sum = a + b;                         // byte + byte gives an int
        System.out.println(sum);
        char letter = 'A';
        System.out.println(letter + 1);          // char + int gives an int
        System.out.println((char) (letter + 1)); // back to a char
    }
}
Three rows: what you wrote, what Java uses, the result. int 7 * double 2.5 becomes double 7.0 * double 2.5, giving double 17.5: the bigger type wins. byte 10 + byte 20 becomes int 10 + int 20, giving int 30: at least int. char 'A' + int 1 becomes int 65 + int 1, giving int 66: a char is its number.سێ ڕیز: ئەوەی نووسیت، ئەوەی Java بەکاری دەهێنێت، ئەنجام. int 7 * double 2.5 دەبێتە double 7.0 * double 2.5 و double 17.5 دەدات: جۆرە گەورەکە دەیباتەوە. byte 10 + byte 20 دەبێتە int 10 + int 20 و int 30 دەدات: لانیکەم int. char 'A' + int 1 دەبێتە int 65 + int 1 و int 66 دەدات: char دەبێتە ژمارەکەی.
Both operands get the same type firstسەرەتا هەردوو هەمان جۆر وەردەگرنCêIT

That is why byte c = a + b; does not compile: incompatible types: possible lossy conversion from int to byte. Store the result in an int, or cast it with (byte).

بۆیە byte c = a + b; کۆمپایل نابێت: incompatible types: possible lossy conversion from int to byte. ئەنجامەکە لە intـدا هەڵبگرە، یان بە (byte) جۆرەکەی بگۆڕە.

The remainder operator %کارپێکەری پاشماوە: %

%, the remainder (or modulo) operator, gives what is left over after a whole-number division. 26 glasses of tea go on trays of 8: 26 / 8 is 3 full trays and 26 % 8 is 2 glasses left over. More examples: 7 % 3 is 1, 3 % 7 is 3 (7 does not fit even once), 12 % 4 is 0 and 20 % 13 is 7.

%، (یان مۆد)، ئەوە دەدات کە دوای دابەشکردنی دەمێنێتەوە. ⁦26⁩ پەرداخ چا لەسەر سینیی 8 پەرداخی دادەنرێن: 26 / 8 دەکاتە 3 سینیی پڕ و 26 % 8 دەکاتە 2 پەرداخ کە دەمێننەوە. نموونەی تر: 7 % 3 دەکاتە 1، 3 % 7 دەکاتە 3، چونکە 7 تەنانەت جارێکیش ناچێتە ناو 3، 12 % 4 دەکاتە 0 و 20 % 13 دەکاتە 7.

Animation with 26 tea glasses and trays that hold 8. One tray fills, then a second, then a third; 2 glasses are left. The last frame shows 26 / 8 = 3 and 26 % 8 = 2: quotient 3 is the full trays, remainder 2 the glasses left.
Animation with 26 tea glasses and trays that hold 8. One tray fills, then a second, then a third; 2 glasses are left. The last frame shows 26 / 8 = 3 and 26 % 8 = 2: quotient 3 is the full trays, remainder 2 the glasses left.
26 / 8 = 3 trays, 26 % 8 = 2 glasses left⁦26 / 8 = 3⁩ سینی، ⁦26 % 8 = 2⁩ پەرداخی بەجێماوCêIT
26=3⏟26 / 8×8+2⏟26 % 826 = \underbrace{3}_{26\,/\,8} \times 8 + \underbrace{2}_{26\,\%\,8}

dividend = quotient × divisor + remainder

دابەشکراو = ئەنجامی دابەشکردن × دابەشکەر +

Remainder.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Remainder {
    public static void main(String[] args) {
        System.out.println(7 % 3);
        System.out.println(3 % 7);
        System.out.println(12 % 4);
        System.out.println(26 % 8);
        System.out.println(20 % 13);
    }
}

The remainder operator %کارپێکەری پاشماوە: %

Taking a number apart: 1729لەیەککردنەوەی ژمارەیەک: ⁦1729⁩

With 10, / and % split a number into its digits. For n = 1729: n % 10 is always the last digit (9), n / 10 is n without its last digit (172), n % 100 the last two digits (29), and n / 10.0 keeps the fraction (172.9) because 10.0 is a double. n % 2 is 0 for an even number and 1 for an odd one.

بە ⁦10⁩، / و % ژمارەیەک دەکەن بە . بۆ n = 1729: n % 10 هەمیشە دوایین ⁦(9)⁩، n / 10 ژمارەکەیە بەبێ دوایین ⁦(172)⁩، n % 100 دوایین دوو ⁦(29)⁩ و n / 10.0 دەهێڵێتەوە ⁦(172.9)⁩ چونکە ⁦10.0⁩ جۆری doubleـە. n % 2 بۆ ژمارەی جووت 0ـە و بۆ ژمارەی تاک 1.

Digits.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Digits {
    public static void main(String[] args) {
        int n = 1729;
        System.out.println(n % 10);      // the last digit
        System.out.println(n / 10);      // n without its last digit
        System.out.println(n % 100);     // the last two digits
        System.out.println(n / 10.0);    // a double: the fraction stays
        System.out.println(-n % 10);     // a negative number on the left
        System.out.println(n % 2);       // 0 for even, 1 for odd
    }
}
Four panels with the digits 1 7 2 9. n % 10 = 9 highlights the last digit. n / 10 = 172 highlights the first three digits. n % 100 = 29 highlights the last two. n / 10.0 = 172.9 highlights all four and adds .9: a double keeps the fraction.چوار بەش بە تاکژمارەکانی 1 7 2 9. n % 10 = 9 دوایین تاکژمارە ڕووناک دەکاتەوە. n / 10 = 172 سێ تاکژمارەی یەکەم. n % 100 = 29 دوایین دووان. n / 10.0 = 172.9 هەر چواریان و .9 زیاد دەکات: double کەرتەکە دەهێڵێتەوە.
/ and % with 10 take 1729 apart/ و % بە ⁦10⁩، ⁦1729⁩ لەیەک دەکەنەوەCêIT

The remainder operator %کارپێکەری پاشماوە: %

Negative numbers and %ژمارەی نەرێنی و %

In Java the remainder takes the sign of the left operand: -n % 10 for n = 1729 is -9, and -7 % 3 is -1, while 7 % -3 is 1. Java keeps the rule (a / b) * b + (a % b) == a, with / cut toward zero. Languages do not agree here: Python's % takes the sign of the divisor (% and // in code). The modulo of mathematics, which has the sign of the divisor, is Math.floorMod; Math.floorDiv divides and rounds down:

لە Javaـدا نیشانەی چەپ وەردەگرێت: -n % 10 بۆ ⁦n = 1729⁩ دەکاتە -9 و -7 % 3 دەکاتە -1، بەڵام 7 % -3 دەکاتە ⁦1. Java⁩ ئەم یاسایە دەپارێزێت: (a / b) * b + (a % b) == a، کە / بەرەو سفر دەبڕێت. زمانەکان لێرەدا یەک ناگرنەوە: %ـی Python نیشانەی دابەشکەر وەردەگرێت (% و // لە کۆددا). مۆدی بیرکاری، کە نیشانەی دابەشکەری هەیە، Math.floorModـە؛ Math.floorDiv دابەش دەکات و بەرەو خوار نزیک دەکاتەوە:

NegativeRemainder.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class NegativeRemainder {
    public static void main(String[] args) {
        System.out.println(-7 % 3);                 // sign of the left number
        System.out.println(7 % -3);
        System.out.println(Math.floorMod(-7, 3));   // sign of the right number
        System.out.println(-7 / 3);                 // toward zero
        System.out.println(Math.floorDiv(-7, 3));   // down
        System.out.println(7.5 % 2);                // % works on doubles too
    }
}
Two number lines from -9 to 0. Top: -7 / 3 = -2 and -7 % 3 = -1: two jumps of -3 reach -6, then -1 more reaches -7; the remainder has the sign of the left number (cut toward 0). Bottom: Math.floorDiv(-7, 3) = -3 and Math.floorMod(-7, 3) = 2: three jumps of -3 reach -9, then +2 comes back to -7; floorMod has the sign of the right number (round down).دوو هێڵی ژمارەکان لە -9 تا 0. سەرەوە: -7 / 3 = -2 و -7 % 3 = -1: دوو بازی -3 دەگەنە -6، پاشان -1ی تر دەگاتە -7؛ پاشماوە نیشانەی ژمارەکەی لای چەپی هەیە (بڕین بەرەو 0). خوارەوە: Math.floorDiv(-7, 3) = -3 و Math.floorMod(-7, 3) = 2: سێ بازی -3 دەگەنە -9، پاشان +2 دەگەڕێتەوە بۆ -7؛ floorMod نیشانەی ژمارەکەی لای ڕاستی هەیە (بەرەو خوار).
% versus Math.floorMod for -7 and 3% بەراورد بە ⁦Math.floorMod⁩ بۆ ⁦-7⁩ و 3CêIT

Which operator goes first?کام کارپێکەر یەکەم ئەنجام دەدرێت؟

Java follows , like the order of operations of school maths (Order of operations): brackets first, then *, / and %, then + and -; the assignment = comes last. So 2 + 3 * 4 is 14, but (2 + 3) * 4 is 20.

Java پەیڕەوی دەکات، وەک ڕیزبەندیی کارەکان لە بیرکاریی قوتابخانەدا (ڕیزبەندیی کارەکان): سەرەتا کەوانە، پاشان *، / و %، پاشان + و -؛ =ـی دوایین دێت. بۆیە 2 + 3 * 4 دەکاتە ⁦14⁩، بەڵام (2 + 3) * 4 دەکاتە ⁦20⁩.

Stairs, highest step first: ( ) brackets; x++ x-- postfix; ++x --x +x -x (type) prefix, sign and cast; * / % multiply, divide, remainder; + - add, subtract; a faded step with < > == && || for comparison and logic in later lessons; = += -= *= /= %= assignment, last. Note: same step, left to right; only = and its friends go right to left.پەیژەیەک، بەرزترین پلە یەکەم: ( ) کەوانە؛ x++ x-- پاشگر؛ ++x --x +x -x (type) پێشگر، نیشانە و گۆڕینی جۆر؛ * / % لێکدان، دابەشکردن، پاشماوە؛ + - کۆکردنەوە، لێدەرکردن؛ پلەیەکی کاڵ بە < > == && || بۆ بەراورد و لۆجیک لە وانەکانی دواتر؛ = += -= *= /= %= دانانی نرخ، دوایین. تێبینی: لە هەمان پلەدا لە چەپەوە بۆ ڕاست؛ تەنیا = و هاوشێوەکانی لە ڕاستەوە بۆ چەپ.
The precedence stairsپەیژەی CêIT
Precedence.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Precedence {
    public static void main(String[] args) {
        System.out.println(2 + 3 * 4);
        System.out.println((2 + 3) * 4);
        System.out.println(10 / 3 * 3);
        System.out.println(10 * 3 / 3);
        System.out.println(10 - 2 + 5);
        System.out.println(10 - (2 + 5));
    }
}

Which operator goes first?کام کارپێکەر یەکەم ئەنجام دەدرێت؟

Same step: left to rightهەمان پلە: لە چەپەوە بۆ ڕاست

Operators on the same step are grouped from left to right: this is called . 10 - 4 - 3 means (10 - 4) - 3, which is 3. The order also decides whether + adds or joins: once a String is on the left, every later + joins text (Joining text with +). Among the binary operators, only the assignment operators go from right to left: a = b = c = 7; stores 7 in c, then in b, then in a.

لە هەمان پلەدا لە چەپەوە بۆ ڕاست گرووپ دەبن: پێی دەگوترێت . 10 - 4 - 3 واتە (10 - 4) - 3، کە دەکاتە 3. ڕیزبەندییەکە دیاری دەکات + کۆ دەکاتەوە یان دەلکێنێت: کاتێک Stringـێک لە چەپ بێت، هەموو +ـێکی دواتر دەق دەلکێنێت (لکاندنی دەق بە +). لە ، تەنیا لە ڕاستەوە بۆ چەپ دەڕۆن: a = b = c = 7; سەرەتا 7 لە c دادەنێت، پاشان لە b، پاشان لە a.

LeftToRight.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class LeftToRight {
    public static void main(String[] args) {
        System.out.println(10 - 4 - 3);     // (10 - 4) - 3
        System.out.println(100 / 10 / 5);   // (100 / 10) / 5
        System.out.println("1" + 2 + 3);    // ("1" + 2) + 3
        System.out.println(1 + 2 + "3");    // (1 + 2) + "3"
        int a, b, c;
        a = b = c = 7;                      // a = (b = (c = 7))
        System.out.println(a + b + c);
    }
}
Three rows worked out step by step. 10 - 4 - 3, then 6 - 3, then 3: numbers. "1" + 2 + 3, then "12" + 3, then "123": text first, so it joins. 1 + 2 + "3", then 3 + "3", then "33": it adds, then joins. Below: a = b = c = 7; is a = (b = (c = 7)); = goes right to left, c gets 7, then b, then a.سێ ڕیز کە هەنگاو بە هەنگاو دەژمێردرێن. 10 - 4 - 3، پاشان 6 - 3، پاشان 3: ژمارەکان. "1" + 2 + 3، پاشان "12" + 3، پاشان "123": سەرەتا دەق، بۆیە دەیلکێنێت. 1 + 2 + "3"، پاشان 3 + "3"، پاشان "33": کۆ دەکاتەوە، پاشان دەیلکێنێت. لە خوارەوە: a = b = c = 7; واتە a = (b = (c = 7))؛ = لە ڕاستەوە بۆ چەپ دەڕوات، سەرەتا c نرخی 7 وەردەگرێت، پاشان b، پاشان a.
Left to right, except =لە چەپەوە بۆ ڕاست، جگە لە =CêIT

Which operator goes first?کام کارپێکەر یەکەم ئەنجام دەدرێت؟

Brackets: every ( needs a )کەوانە: هەموو ( پێویستی بە ) هەیە

Brackets make the order clear to Java and to the next reader. Every opening bracket needs a closing one; a missing one is a , so javac stops:

کەوانە ڕیزبەندییەکە بۆ Java و بۆ خوێنەری داهاتوو ڕوون دەکاتەوە. هەموو کەوانەیەکی کراوە پێویستی بە کەوانەیەکی داخراو هەیە؛ نەبوونی یەکێکیان ، بۆیە javac دەوەستێت:

Formula.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Formula {
    public static void main(String[] args) {
        double a = 2, b = 4, t = 0.5;
        double r = ((a + b) * t / 2 * (1 - t);
        System.out.println(r);
    }
}
Top, with a cross: ((a + b) * t / 2 * (1 - t); where the first bracket has no partner: 3 open, only 2 close, and javac says ')' expected. Bottom, with a tick: (a + b) * t / 2 * (1 - t); where arcs join each bracket to its partner, and r is 0.75.سەرەوە، بە نیشانەی هەڵە: ((a + b) * t / 2 * (1 - t); کە یەکەم کەوانە هاوتای نییە: 3 دەکرێتەوە، تەنیا 2 دادەخرێت و javac دەڵێت ')' expected. خوارەوە، بە نیشانەی ڕاست: (a + b) * t / 2 * (1 - t); کە کەوانەکان بە هاوتاکانیانەوە بەستراون و r دەکاتە 0.75.
Count the bracketsکەوانەکان بژمێرەCêIT

With the extra bracket removed, (a + b) * t / 2 * (1 - t) gives 0.75 for a = 2, b = 4 and t = 0.5. Brackets also fix real bugs: a + b / 2 divides only b, so for 8 and 6 it gives 11 instead of the average 7, which is (a + b) / 2.

بە لابردنی کەوانە زیادەکە، (a + b) * t / 2 * (1 - t) بۆ ⁦a = 2⁩، ⁦b = 4⁩ و ⁦t = 0.5⁩ دەدات ⁦0.75⁩. کەوانە ڕاستەقینەش چاک دەکات: a + b / 2 تەنیا b دابەش دەکات، بۆیە بۆ 8 و 6 لە جیاتی تێکڕای 7 دەدات ⁦11⁩؛ تێکڕاکە (a + b) / 2ـە.

Compound assignment: += and friendsکارپێکەرەکانی دانانی نرخ: += و هاوشێوەکانی

A variable is often changed using its own old value: total = total + cans;. Java has a short form for it: total += cans;. There is one for each arithmetic operator: +=, -=, *=, /= and %=; total *= 2; is the short form of total = total * 2;.

زۆرجار بە ی خۆی دەگۆڕدرێت: total = total + cans;. Java شێوەیەکی کورتی بۆ هەیە: total += cans;. بۆ هەر یەکێک هەیە: +=، -=، *=، /= و %=؛ total *= 2; شێوە کورتەکەی total = total * 2;ـە.

Compound.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Compound {
    public static void main(String[] args) {
        int total = 10;
        int cans = 6;
        total += cans;           // total = total + cans
        System.out.println(total);
        total *= 2;              // total = total * 2
        System.out.println(total);
        total -= 2;              // total = total - 2
        System.out.println(total);
        total /= 3;              // total = total / 3
        System.out.println(total);
        total %= 4;              // total = total % 4
        System.out.println(total);
    }
}
total += cans; (short form) equals total = total + cans; (long form). Boxes: total 10 and cans 6 give the new total 16: 10 + 6 = 16 goes back into total. Below, the family: x += 3, x -= 3, x *= 3, x /= 3, x %= 3.total += cans; (شێوەی کورت) یەکسانە بە total = total + cans; (شێوەی درێژ). سندووقەکان: total 10 و cans 6 total ـی نوێی 16 دەدەن: 10 + 6 = 16 دەچێتەوە ناو total. لە خوارەوە، خێزانەکە: x += 3، x -= 3، x *= 3، x /= 3، x %= 3.
total += cans is total = total + cans⁦total += cans⁩ واتە ⁦total = total + cans⁩CêIT

Compound assignment: += and friendsکارپێکەرەکانی دانانی نرخ: += و هاوشێوەکانی

The hidden cast inside +=گۆڕینی جۆری شاراوە لە ناو +=

A compound assignment is not exactly its long form: it ends with a cast back to the variable's type. x op= y means x = (type of x) (x op y) (JLS §15.26.2). So with byte small = 10;, small += 5; compiles, but small = small + 5; does not: small + 5 is an int, and javac says incompatible types: possible lossy conversion from int to byte.

x += y ڕێک وەک شێوە درێژەکەی نییە: لە کۆتاییدا جۆرەکە دەگۆڕێتەوە بۆ جۆری . x op= y واتە x = (type of x) (x op y) (JLS بڕگەی ⁦15.26.2⁩). بۆیە بە byte small = 10;، small += 5; کۆمپایل دەبێت، بەڵام small = small + 5; نا: small + 5 جۆری intـە و javac دەڵێت incompatible types: possible lossy conversion from int to byte.

Left card, with a tick: small += 5; Java reads it as small = (byte) (small + 5); with (byte) in a dashed box: a hidden cast, so it compiles and small is 15. Right card, with a cross: small = small + 5; small + 5 is an int, an int does not fit into a byte: possible lossy conversion from int to byte. Bottom: careful, small += 200 compiles too, and wraps round to -41.کارتی چەپ، بە نیشانەی ڕاست: small += 5; Java وای دەخوێنێتەوە: small = (byte) (small + 5); کە (byte) لە چوارچێوەیەکی پچڕپچڕدایە: گۆڕینی جۆری شاراوە، بۆیە کۆمپایل دەبێت و small دەبێتە 15. کارتی ڕاست، بە نیشانەی هەڵە: small = small + 5; small + 5 جۆری intـە و int ناچێتە ناو byte: possible lossy conversion from int to byte. خوارەوە: ئاگادار بە، small += 200ـیش کۆمپایل دەبێت و دەسوڕێتەوە بۆ -41.
Compound assignment contains a cast+= شاراوەی تێدایەCêIT
HiddenCast.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class HiddenCast {
    public static void main(String[] args) {
        byte small = 10;
        small += 5;                  // small = (byte) (small + 5)
        System.out.println(small);
        small += 200;                // compiles too, and wraps round
        System.out.println(small);
        short x = 3;
        x += 4.6;                    // x = (short) (x + 4.6)
        System.out.println(x);
    }
}

The hidden cast can also hide a mistake: small += 200; compiles and wraps round to -41 (Narrowing can change the value), and x += 4.6; on a short quietly stores 7.

شاراوە دەتوانێت هەڵەیەکیش بشارێتەوە: small += 200; کۆمپایل دەبێت و دەسوڕێتەوە بۆ -41 (تەسککردنەوەی جۆر دەتوانێت نرخەکە بگۆڕێت) و x += 4.6; لەسەر shortـێک بە بێدەنگی 7 هەڵدەگرێت.

++ and --: add or take away 1++ و --: یەک زیاد یان کەم بکە

Adding or taking away 1 is so common that Java has two short operators for it: ++ adds 1 to a variable () and -- takes 1 away (). i++; does the same as i = i + 1; and i += 1;.

زیادکردن یان کەمکردنەوەی 1 هێندە باوە کە Java دوو کورتی بۆ هەیە: ++ یەک بۆ زیاد دەکات () و -- یەک کەم دەکاتەوە (). i++; هەمان کاری i = i + 1; و i += 1; دەکات.

IncDec.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class IncDec {
    public static void main(String[] args) {
        int i = 3, j = 3;
        i++;          // i becomes 4
        j--;          // j becomes 2
        System.out.println(i + " " + j);
    }
}
Two counters. The button i++; turns int i from 3 to 4. The button j--; turns int j from 3 to 2. Between them: the same as i = i + 1; and j = j - 1;.دوو ژمێرەر. دوگمەی i++; intـی i لە 3ـەوە دەکات بە 4. دوگمەی j--; intـی j لە 3ـەوە دەکات بە 2. لە نێوانیاندا: هەمان i = i + 1; و j = j - 1;.
++ adds 1, -- takes 1 away++ یەک زیاد دەکات، -- یەک کەم دەکاتەوەCêIT

They work only on variables (5++ is an error) and you will use them all the time to count in loops (The while loop). Python has no ++ (Short forms).

تەنیا لەسەر کار دەکەن (5++ هەڵەیە) و هەمیشە بۆ ژماردن لە ناو بەکاریان دەهێنیت (خولی while). Python ++ـی نییە (شێوە کورتەکان).

++ and --: add or take away 1++ و --: یەک زیاد یان کەم بکە

i++ or ++i?i++ یان ++i؟

The operator can come after the variable, i++ (postfix), or before it, ++i (prefix) (). On a line of its own, both just add 1. Inside a bigger expression they differ: postfix uses the old value and then adds 1; prefix adds 1 first and then uses the new value:

دەتوانێت دوای بێت، i++ (پاشگر)، یان پێشی بێت، ++i (پێشگر) (). لە دێڕێکی تەنیادا، هەردووکیان تەنیا یەک زیاد دەکەن. لە ناو گەورەتردا جیاوازن: پاشگر بەکار دەهێنێت و پاشان یەک زیاد دەکات؛ پێشگر سەرەتا یەک زیاد دەکات و پاشان بەکار دەهێنێت:

PostfixPrefix.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class PostfixPrefix {
    public static void main(String[] args) {
        int i = 10;
        int newNum = 10 * i++;        // use 10, then i becomes 11
        System.out.println("i is " + i + ", newNum is " + newNum);
        i = 10;
        newNum = 10 * (++i);          // i becomes 11, then use 11
        System.out.println("i is " + i + ", newNum is " + newNum);
    }
}
Animation in two columns, both starting with i = 10. Left, newNum = 10 * i++;: first the old value 10 is used, then i becomes 11, so newNum is 10 * 10 = 100. Right, newNum = 10 * (++i);: first i becomes 11, then the new value 11 is used, so newNum is 10 * 11 = 110. Both end with i = 11. Last frame: i++ means use, then add; ++i means add, then use; on its own line both do the same.
Animation in two columns, both starting with i = 10. Left, newNum = 10 * i++;: first the old value 10 is used, then i becomes 11, so newNum is 10 * 10 = 100. Right, newNum = 10 * (++i);: first i becomes 11, then the new value 11 is used, so newNum is 10 * 11 = 110. Both end with i = 11. Last frame: i++ means use, then add; ++i means add, then use; on its own line both do the same.
Postfix uses the old value, prefix the new oneپاشگر نرخە کۆنەکە بەکار دەهێنێت، پێشگر نرخە نوێیەکەCêIT

++ and --: add or take away 1++ و --: یەک زیاد یان کەم بکە

The four formsچوار شێوەکە

Java has four forms. In each example i is 1 before the line runs:

Java چوار شێوەی هەیە. لە هەر نموونەیەکدا i پێش جێبەجێبوونی دێڕەکە 1ـە:

OperatorNameناوWhat it doesچی دەکاتExampleنموونە
++varpreincrementزیادکردنی پێشگرadds 1, then uses the new valueیەک زیاد دەکات، پاشان نرخە نوێیەکە بەکار دەهێنێتint j = ++i; // j is 2, i is 2
var++postincrementزیادکردنی پاشگرuses the old value, then adds 1نرخە کۆنەکە بەکار دەهێنێت، پاشان یەک زیاد دەکاتint j = i++; // j is 1, i is 2
--varpredecrementکەمکردنەوەی پێشگرtakes 1 away, then uses the new valueیەک کەم دەکاتەوە، پاشان نرخە نوێیەکە بەکار دەهێنێتint j = --i; // j is 0, i is 0
var--postdecrementکەمکردنەوەی پاشگرuses the old value, then takes 1 awayنرخە کۆنەکە بەکار دەهێنێت، پاشان یەک کەم دەکاتەوەint j = i--; // j is 1, i is 0
Four timelines, each starting with i = 1. int j = ++i;: first i goes from 1 to 2, then j = 2. int j = i++;: first j = 1, then i goes from 1 to 2. int j = --i;: first i goes from 1 to 0, then j = 0. int j = i--;: first j = 1, then i goes from 1 to 0. Note: prefix changes i, then copies; postfix copies, then changes i.چوار هێڵی کات، هەریەکە بە i = 1 دەست پێ دەکات. int j = ++i;: سەرەتا i لە 1ـەوە دەبێتە 2، پاشان j = 2. int j = i++;: سەرەتا j = 1، پاشان i لە 1ـەوە دەبێتە 2. int j = --i;: سەرەتا i لە 1ـەوە دەبێتە 0، پاشان j = 0. int j = i--;: سەرەتا j = 1، پاشان i لە 1ـەوە دەبێتە 0. تێبینی: پێشگر i دەگۆڕێت، پاشان کۆپی دەکات؛ پاشگر کۆپی دەکات، پاشان i دەگۆڕێت.
When is j copied?j کەی کۆپی دەکرێت؟CêIT

++ and --: add or take away 1++ و --: یەک زیاد یان کەم بکە

Practice and styleڕاهێنان و شێواز

ExerciseڕاهێنانWhat does this print?ئەمە چی چاپ دەکات؟

Work out the four printed lines, then run the program to check.

چوار دێڕە چاپکراوەکە بژمێرە، پاشان جێبەجێ بکە بۆ پشکنین.

IncDecExercise.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class IncDecExercise {
    public static void main(String[] args) {
        int a = 6;
        int b = ++a;
        System.out.println("a = " + a);
        System.out.println(" b = " + b);
        a = 6;
        b = --a;
        System.out.println("a = " + a);
        System.out.println(" b = " + b);
    }
}
Show answerوەڵامەکە پیشان بدە
Outputدەرئەنجام
a = 7
 b = 7
a = 5
 b = 5

++a adds 1 first, so a and b are both 7; then --a (with a set back to 6) takes 1 away first, so both are 5. Type the quotes yourself: quotes copied from slides or PDFs (“a = ”) give illegal character: '\u201c' (Copy-paste traps).

++a سەرەتا یەک زیاد دەکات، بۆیە a و b هەردووکیان 7ن؛ پاشان --a (کە a گەڕێندرایەوە بۆ ⁦6⁩) سەرەتا یەک کەم دەکاتەوە، بۆیە هەردووکیان 5ن. خۆت بنووسە: ئەوانەی لە سلاید یان PDF کۆپی دەکرێن (“a = ”) دەدەن illegal character: '\u201c' (تەڵەکانی کۆپی و لکاندن).

Lab: four operations on two numbersتاقیگە: چوار کار لەسەر دوو ژمارە

ExerciseڕاهێنانLab task 1ئەرکی تاقیگەی 1

Declare variables for number1 = 240.8 and number2 = -6.25, print both, then print their sum, difference, quotient and product, each on its own line with a label.

دوو بۆ ⁦number1 = 240.8⁩ و ⁦number2⁩ = -6.25 بناسێنە، هەردووکیان چاپ بکە، پاشان کۆ، جیاوازی، ئەنجامی دابەشکردن و ئەنجامی لێکدانیان چاپ بکە، هەریەکە لە دێڕێکی خۆیدا لەگەڵ ناونیشانێک.

Hintئاماژە

Use double variables and four println statements.

double و چوار ڕستەی println بەکار بهێنە.

Show answerوەڵامەکە پیشان بدە
Task1.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Task1 {
    public static void main(String[] args) {
        double number1 = 240.8;
        double number2 = -6.25;
        System.out.println("Number1 = " + number1);
        System.out.println("Number2 = " + number2);
        System.out.println("******** Task1 ******");
        System.out.println("Summation = " + (number1 + number2));
        System.out.println("Subtraction = " + (number1 - number2));
        System.out.println("Division = " + (number1 / number2));
        System.out.println("Multiplication = " + (number1 * number2));
    }
}

Each calculation is in brackets, so it is done before the + that joins it to the label.

هەر ژماردنێک لە ناو کەوانەدایە، بۆیە پێش ئەو +ـە ئەنجام دەدرێت کە بە ناونیشانەکەوە دەیلکێنێت.

Lab: the volume of two water tanksتاقیگە: قەبارەی دوو تانکی ئاو

Many houses keep water in round tanks on the roof. A tank is a cylinder, and its volume is π × radius × radius × height:

زۆر ماڵ ئاو لە تانکی خڕی سەربان هەڵدەگرن. تانکێک سیلیندەرێکە و قەبارەکەی π × نیوەتیرە × نیوەتیرە × بەرزییە:

V=πr2hV = \pi r^{2} h

π is about 3.14; r is the radius and h the height

π نزیکەی ⁦3.14⁩ـە؛ r نیوەتیرە و h بەرزییە

Two cylinders. Tank 1: r = 15, h = 75; 3.14 × 15 × 15 × 75 = 52987.5. Tank 2: r = 12, h = 80; 3.14 × 12 × 12 × 80 = 36172.8, but Java prints 36172.799999999996.دوو سیلیندەر. تانکی 1: r = 15، h = 75؛ 3.14 × 15 × 15 × 75 = 52987.5. تانکی 2: r = 12، h = 80؛ 3.14 × 12 × 12 × 80 = 36172.8، بەڵام Java چاپی دەکات 36172.799999999996.
V = π r² h for the two tanks⁦V = π r² h⁩ بۆ دوو تانکەکەCêIT
ExerciseڕاهێنانLab task 2ئەرکی تاقیگەی 2

Write a program that finds the volume of tank 1 (radius 15, height 75) and tank 2 (radius 12, height 80), using a constant PI = 3.14, and prints the radius, height and volume of each.

بنووسە کە قەبارەی تانکی 1 (نیوەتیرە ⁦15⁩، بەرزی ⁦75⁩) و تانکی 2 (نیوەتیرە ⁦12⁩، بەرزی ⁦80⁩) بدۆزێتەوە، بە بەکارهێنانی وەک PI = 3.14 و نیوەتیرە، بەرزی و قەبارەی هەریەکەیان چاپ بکات.

Hintئاماژە

final double PI = 3.14; and PI * radius1 * radius1 * height1.

final double PI = 3.14; و PI * radius1 * radius1 * height1.

Show answerوەڵامەکە پیشان بدە
Task2.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Task2 {
    public static void main(String[] args) {
        final double PI = 3.14;
        int radius1 = 15, height1 = 75;
        int radius2 = 12, height2 = 80;
        double volume1 = PI * radius1 * radius1 * height1;
        double volume2 = PI * radius2 * radius2 * height2;
        System.out.println("***** Task2 *****");
        System.out.println("Cylinder 1 radius = " + radius1 + " height = " + height1);
        System.out.println("The volume for Cylinder1 =  " + volume1);
        System.out.println("----------------------------------");
        System.out.println("Cylinder 2 radius = " + radius2 + " height = " + height2);
        System.out.println("The volume for Cylinder2 =  " + volume2);
    }
}

Lab: the volume of two water tanksتاقیگە: قەبارەی دوو تانکی ئاو

Why 36172.799999999996?بۆچی ⁦36172.799999999996⁩؟

The exact answer is 36172.8, but a double stores numbers in binary, and 3.14 has no exact binary form. The tiny shows in the last digits, depending on the order of the multiplications (Why 0.1 + 0.2 is not 0.3):

وەڵامی ورد ⁦36172.8⁩ـە، بەڵام double ژمارەکان بە شێوەی هەڵدەگرێت و ⁦3.14⁩ شێوەیەکی وردی نییە. لە دوایین دەردەکەوێت و بە ڕیزبەندیی لێکدانەکانەوە بەندە (بۆچی ⁦0.1 + 0.2⁩ نابێتە ⁦0.3⁩):

Tail.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Tail {
    public static void main(String[] args) {
        System.out.println(3.14 * 12 * 12 * 80);
        System.out.println(3.14 * (12 * 12 * 80));
        System.out.println(3.14 * 11520);
    }
}

Two improvements: Java's own π, Math.PI, is far more exact than 3.14 (The Math class), and printf("%.2f%n", x) prints exactly two decimal places (Two decimal places, rounded):

دوو باشترکردن: Math.PI، کە πـی خودی Javaـیە، زۆر وردترە لە ⁦3.14⁩ (کلاسەکەی Math) و printf("%.2f%n", x) ڕێک دوو چاپ دەکات (دوو خانەی دەیی، نزیککراوە):

Cylinders.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Cylinders {
    public static void main(String[] args) {
        int radius1 = 15, height1 = 75;
        int radius2 = 12, height2 = 80;
        double volume1 = Math.PI * radius1 * radius1 * height1;
        double volume2 = Math.PI * radius2 * radius2 * height2;
        System.out.println(volume1);
        System.out.println(volume2);
        System.out.printf("%.2f%n", volume1);     // two decimal places
        System.out.printf("%.2f%n", volume2);
    }
}

Lab: Celsius to kelvinتاقیگە: سیلیزی بۆ کێلڤن

Scientists measure temperature in kelvin (K). The kelvin scale starts at the coldest possible temperature, so 0 °C is 273.15 K:

زانایان پلەی گەرمی بە کێلڤن ⁦(K)⁩ دەپێون. پێوانەی کێلڤن لە ساردترین پلەی گەرمی کە هەبێت دەست پێ دەکات، بۆیە ⁦0 °C⁩ دەکاتە ⁦273.15 K⁩:

K=C+273.15K = C + 273.15
Two thermometers side by side, Celsius and kelvin, with K = C + 273.15. Matching marks: 100 °C and 373.15 K; 15 °C and 288.15 K, highlighted as 15 °C = 288.15 K; 0 °C and 273.15 K; after a break in the scale, -273.15 °C and 0 K.دوو گەرماپێو لە تەنیشت یەک، سیلیزی و کێلڤن، لەگەڵ K = C + 273.15. نیشانە هاوتاکان: 100 °C و 373.15 K؛ 15 °C و 288.15 K، کە وەک 15 °C = 288.15 K ڕووناک کراوەتەوە؛ 0 °C و 273.15 K؛ دوای بڕانێک لە پێوانەکەدا، -273.15 °C و 0 K.
15 °C is 288.15 K⁦15 °C⁩ دەکاتە ⁦288.15 K⁩CêIT
ExerciseڕاهێنانLab task 3ئەرکی تاقیگەی 3

Write a program that converts 15 °C to kelvin and prints 15 Celsius = 288.15 Kelvin.

بنووسە کە ⁦15 °C⁩ بگۆڕێت بۆ کێلڤن و چاپ بکات 15 Celsius = 288.15 Kelvin.

Show answerوەڵامەکە پیشان بدە
Task3.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Task3 {
    public static void main(String[] args) {
        int celsius = 15;
        double kelvin = celsius + 273.15;
        System.out.println("******* Task 3 *******");
        System.out.println(celsius + " Celsius =  " + kelvin + "  Kelvin");
    }
}

celsius + 273.15 mixes an int and a double, so the result is a double.

celsius + 273.15 جۆرێکی int و جۆرێکی double تێکەڵ دەکات، بۆیە ئەنجامەکە doubleـە.

Common mistakesهەڵە باوەکان

You wroteنووسیتWhat happensچی ڕوو دەداتFixچارەسەر
2 ^ 108: ^ is XOR, not a power8: ^ بریتییە لە XOR، نەک Math.pow(2, 10) (The Math class)Math.pow(2, 10) (کلاسەکەی Math)
"Sum: " + a + bjoins: Sum: 53دەلکێنێت: Sum: 53"Sum: " + (a + b)"Sum: " + (a + b)
sum / count // both intthe fraction is cut off دەبڕدرێتەوە(double) sum / count or / 2.0(double) sum / count یان / 2.0
n % 2 == 1false for negative odd numbersبۆ ژمارە تاکە نەرێنییەکان falsen % 2 != 0n % 2 != 0
x = x++;x does not changex ناگۆڕێتx++;x++;
x =+ 5;stores +5: x becomes 5⁦+5⁩ هەڵدەگرێت: x دەبێتە 5x += 5;x += 5;
b = b + 1; // byte bpossible lossy conversion from int to bytepossible lossy conversion from int to byteb += 1; or b++;b += 1; یان b++;
((a + b) * t')' expected')' expectedCount the brackets.کەوانەکان بژمێرە.
1 / 0ArithmeticException: / by zeroArithmeticException: / by zeroCheck the divisor first (Runtime errors).سەرەتا دابەشکەرەکە بپشکنە (هەڵەکانی کاتی جێبەجێکردن).
price * 3;not a statement: the value would be lostnot a statement: لەدەست دەچێتStore or print it: total = price * 3; (not a statement)هەڵیبگرە یان چاپی بکە: total = price * 3; (not a statement)
2(x + 1)';' expected and not a statement: Java has no ';' expected و not a statement: Java نییە2 * (x + 1)2 * (x + 1)

The x =+ 5 trap is real: Java reads it as x = +5, so the old value is lost without any message.

تەڵەی x =+ 5 ڕاستەقینەیە: Java وەک x = +5 دەیخوێنێتەوە، بۆیە بێ هیچ پەیامێک لەدەست دەچێت.

EqualsPlus.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class EqualsPlus {
    public static void main(String[] args) {
        int x = 10;
        x =+ 5;            // means x = +5
        System.out.println(x);
        x += 5;            // means x = x + 5
        System.out.println(x);
    }
}

Practiceڕاهێنان

ExerciseڕاهێنانMinutes and secondsخولەک و چرکە

A song lasts 200 seconds. Use / and % to print it as minutes and seconds: 3 min 20 s.

گۆرانییەک ⁦200⁩ چرکە دەخایەنێت. بە / و % وەک خولەک و چرکە چاپی بکە: 3 min 20 s.

Show answerوەڵامەکە پیشان بدە
Minutes.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Minutes {
    public static void main(String[] args) {
        int seconds = 200;
        int minutes = seconds / 60;
        int rest = seconds % 60;
        System.out.println(minutes + " min " + rest + " s");
    }
}
ExerciseڕاهێنانSum of the digitsکۆی تاکژمارەکان

Print the sum of the digits of 472 (4 + 7 + 2 = 13), using % and / only.

کۆی ⁦472⁩ چاپ بکە (4 + 7 + 2 = 13)، تەنیا بە % و /.

Show answerوەڵامەکە پیشان بدە
DigitSum.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class DigitSum {
    public static void main(String[] args) {
        int n = 472;
        int ones = n % 10;            // 2
        int tens = n / 10 % 10;       // 7
        int hundreds = n / 100;       // 4
        System.out.println(ones + tens + hundreds);
    }
}

n / 10 % 10 first removes the last digit (47), then takes the new last digit (7).

n / 10 % 10 سەرەتا دوایین لادەبات ⁦(47)⁩، پاشان دوایین نوێ وەردەگرێت ⁦(7)⁩.

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

What does each line print? 7 / 2 * 2 · 7 % 2 · 7.0 / 2 · -7 / 2 · -7 % 2 · 1 + 2 + "3" + 4 + 5

هەر دێڕێک چی چاپ دەکات؟ 7 / 2 * 2 · 7 % 2 · 7.0 / 2 · -7 / 2 · -7 % 2 · 1 + 2 + "3" + 4 + 5

Show answerوەڵامەکە پیشان بدە
Predict.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Predict {
    public static void main(String[] args) {
        System.out.println(7 / 2 * 2);
        System.out.println(7 % 2);
        System.out.println(7.0 / 2);
        System.out.println(-7 / 2);
        System.out.println(-7 % 2);
        System.out.println(1 + 2 + "3" + 4 + 5);
    }
}

7 / 2 is 3, so 7 / 2 * 2 is 6; the last line adds 1 + 2 = 3, then joins "3", 4 and 5: "3345".

7 / 2 دەکاتە 3، بۆیە 7 / 2 * 2 دەکاتە 6؛ دوایین دێڕ 1 + 2 = 3 کۆ دەکاتەوە، پاشان ⁦"3"⁩، 4 و 5 دەلکێنێت: ⁦"3345"⁩.

ExerciseڕاهێنانFix the averageتێکڕاکە چاک بکە

Karwan's marks are 70, 85 and 90. This line prints 185.0 instead of the average. Fix it.

نمرەکانی کاروان ⁦70⁩، ⁦85⁩ و ⁦90⁩ن. ئەم دێڕە لە جیاتی تێکڕا ⁦185.0⁩ چاپ دەکات. چاکی بکە.

Average3.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Average3 {
    public static void main(String[] args) {
        int mark1 = 70, mark2 = 85, mark3 = 90;
        double average = mark1 + mark2 + mark3 / 3;
        System.out.println(average);
    }
}
Show answerوەڵامەکە پیشان بدە
Average3.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Average3 {
    public static void main(String[] args) {
        int mark1 = 70, mark2 = 85, mark3 = 90;
        double average = (mark1 + mark2 + mark3) / 3.0;
        System.out.println(average);
    }
}

Only mark3 / 3 (30) was divided. Brackets make the sum first, and 3.0 keeps the fraction.

تەنیا mark3 / 3 ⁦(30)⁩ دابەش کرابوو. کەوانە سەرەتا کۆکردنەوەکە دەکات و ⁦3.0⁩ دەهێڵێتەوە.

ExerciseڕاهێنانA tricky lineدێڕێکی فێڵاوی

int x = 5; int y = x++ + ++x; What are x and y? Then say why you should not write lines like this.

int x = 5; int y = x++ + ++x; x و y چەندن؟ پاشان بڵێ بۆچی نابێت دێڕی وا بنووسیت.

Show answerوەڵامەکە پیشان بدە
Tricky.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Tricky {
    public static void main(String[] args) {
        int x = 5;
        int y = x++ + ++x;
        System.out.println("x = " + x + ", y = " + y);
    }
}

Left to right: x++ gives 5 and makes x 6; ++x makes x 7 and gives 7; 5 + 7 = 12. Correct, but hard to read: one change per line is clearer.

لە چەپەوە بۆ ڕاست: x++ نرخی 5 دەدات و x دەکات بە 6؛ ++x x دەکات بە 7 و 7 دەدات؛ 5 + 7 = 12. دروستە، بەڵام خوێندنەوەی قورسە: یەک گۆڕان لە هەر دێڕێکدا ڕوونترە.

Summaryکورتە

  • An expression works out to one value; a statement does a job and ends with ;. Only assignments, ++/--, method calls and new may stand alone (otherwise: not a statement).
  • Arithmetic operators: + - * / %. % gives the remainder, with the sign of the left operand (Math.floorMod gives the maths modulo).
  • int / int is integer division: the fraction is cut off toward zero. One double operand makes it real division.
  • Precedence: brackets, then * / %, then + -, then =. Same step: left to right; assignment: right to left.
  • x += y is x = (type) (x + y): short, and with a hidden cast.
  • i++ uses the old value, then adds 1; ++i adds 1, then uses the new value. Alone on a line they are the same.
  • دەبێتە یەک ؛ کارێک دەکات و بە ; کۆتایی دێت. تەنیا ، ++/--، بانگکردنی و new دەتوانن بەتەنیا بن (ئەگینا: not a statement).
  • : + - * / %. % دەدات، بە نیشانەی چەپ (Math.floorMod مۆدی بیرکاری دەدات).
  • int / int : بەرەو سفر دەبڕدرێتەوە. یەک double دەیکات بە دابەشکردنی ڕاستەقینە.
  • : کەوانە، پاشان * / %، پاشان + -، پاشان =. هەمان پلە: لە چەپەوە بۆ ڕاست؛ : لە ڕاستەوە بۆ چەپ.
  • x += y واتە x = (type) (x + y): کورتە و شاراوەی تێدایە.
  • i++ نرخە کۆنەکە بەکار دەهێنێت، پاشان یەک زیاد دەکات؛ ++i یەک زیاد دەکات، پاشان نرخە نوێیەکە بەکار دەهێنێت. لە دێڕێکی تەنیادا وەک یەکن.
Review questionsپرسیارەکانی پێداچوونەوە
Why is -7 / 2 equal to -3 in Java, while Python's -7 // 2 is -4?بۆچی -7 / 2 لە Javaـدا -3ـە، بەڵام -7 // 2ـی Python -4ـە؟

Java's integer division cuts toward zero; Python's // rounds down. Use Math.floorDiv(-7, 2) for -4 in Java.

لە Javaـدا بەرەو سفر دەبڕێت؛ //ـی Python بەرەو خوار نزیک دەکاتەوە. بۆ -4 لە Javaـدا Math.floorDiv(-7, 2) بەکار بهێنە.

What does n % 10 give?n % 10 چی دەدات؟

The last digit of a positive whole number n.

دوایین ئەرێنیی n.

Why does small += 5 compile for a byte but small = small + 5 does not?بۆچی small += 5 بۆ byte کۆمپایل دەبێت بەڵام small = small + 5 نا؟

small + 5 is an int. The compound form contains a cast back to byte; the long form needs one written by hand.

small + 5 جۆری intـە. شێوە کورتەکە (+=) (byte)ـی تێدایە؛ شێوە درێژەکە پێویستی بە نووسینی بە دەستە.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

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

The Math Classکلاسەکەی Math

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.تەنیا لەسەر ئەم ئامێرە پاشەکەوت دەکرێت.