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

Variables, Names and Constantsگۆڕاوەکان، ناوەکان و نەگۆڕەکان

What a Java variable is and how to declare one, assignment and why = is not equality, tracing a swap, declaring before use and giving a first value, final constants, var, the rules and conventions for names, and Java 17's keywords.

Java چییە و چۆن دەیناسێنیت، و بۆچی = یەکسانی نییە، بەدواداچوونی گۆڕینەوەیەک، ناساندن پێش بەکارهێنان و پێدانی یەکەم ، final، var، یاسا و نەریتەکانی ناونان و ⁦Java 17⁩.

17 min read16 خولەک خوێندنەوە · 19 stepsهەنگاو

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

  • Declare variables with a type, a name and a first value, and change them with assignment statements.
  • Trace a program with a trace table and swap two values with a temporary variable.
  • Fix cannot find symbol and might not have been initialized, and use final constants and var.
  • Choose legal, readable names that follow Java's rules and conventions, and recognise its keywords.
  • بە جۆر، ناو و یەکەم بناسێنیت و بە بیانگۆڕیت.
  • بە بەدوادا بچیت و دوو بە کاتی بگۆڕیتەوە.
  • cannot find symbol و might not have been initialized چاک بکەیتەوە و final و var بەکار بهێنیت.
  • ناوی دروست و خوێنراوە هەڵبژێریت کە پەیڕەوی یاسا و نەریتەکانی Java دەکەن و بناسیتەوە.

What is a variable?گۆڕاو چییە؟

A is a storage place in a program. It has a name and holds a value, and it is called a variable because its value can change while the program runs. In Java every variable also has a type, which says what kind of value it may hold. (In Python you wrote age = 21; in Java you also give the type, int age = 21;. See Python next to Java.)

شوێنێکی هەڵگرتنە لە . ناوێکی هەیە و هەڵدەگرێت و پێی دەگوترێت چونکە لە کاتی کارکردنی دەگۆڕێت. لە Javaـدا هەر جۆرێکیشی هەیە، کە دەڵێت چ جۆرە هەڵدەگرێت. (لە Pythonـدا دەتنووسی age = 21؛ لە Javaـدا جۆرەکەش دەنووسیت: int age = 21;. بڕوانە Python لە تەنیشت Javaـەوە.)

A parking spaceشوێنێکی پارکینگ

A variable is like a numbered space in a multi-storey car park. The space has a number, such as J 053, and it can hold a car. A variable has a name, such as age, and holds a value, such as 21. When another car parks there, the space keeps its number but holds something new.

وەک شوێنێکی ژمارەدار وایە لە پارکینگێکی چەند نهۆمیدا. شوێنەکە ژمارەیەکی هەیە (وەک ⁦J 053⁩) و دەتوانێت ئۆتۆمبێلێک هەڵبگرێت. ناوێکی هەیە (وەک age) و هەڵدەگرێت (وەک ⁦21⁩). کاتێک ئۆتۆمبێلێکی تر لەوێ دەوەستێت، شوێنەکە ژمارەکەی خۆی دەهێڵێتەوە بەڵام شتێکی نوێ هەڵدەگرێت.

Where the analogy stops workingسنووری ئەم نموونەیە

A parking space takes any car, but a Java variable takes only values of its own type: an int variable cannot hold text.

شوێنی پارکینگ هەر ئۆتۆمبێلێک وەردەگرێت، بەڵام Java تەنیا جۆری خۆی وەردەگرێت: int ناتوانێت دەق هەڵبگرێت.

Top: three parking spaces numbered J 053, J 054 and J 055; cars stand in J 053 and J 055, and J 054 is empty. Bottom: three variable boxes under them: age of type int holds 21, total of type double has no value yet, price of type double holds 2.5. Notes on the left: space number = variable name; the car = the value; the box's label = its type.سەرەوە: سێ شوێنی پارکینگ بە ژمارەی J 053، J 054 و J 055؛ ئۆتۆمبێل لە J 053 و J 055دا وەستاوە و J 054 بەتاڵە. خوارەوە: سێ سندووقی گۆڕاو لە ژێریاندا: age بە جۆری int 21 هەڵدەگرێت، total بە جۆری double هێشتا بێ نرخە، price بە جۆری double 2.5 هەڵدەگرێت. تێبینییەکانی لای چەپ: ژمارەی شوێن = ناوی گۆڕاو؛ ئۆتۆمبێلەکە = نرخەکە؛ نیشانەی سندووقەکە = جۆرەکەی.
Variables are like numbered parking spaces وەک شوێنی ژمارەداری پارکینگ وانCêIT

Declaring a variableناساندنی گۆڕاوێک

Before you use a variable you declare it: you write its type, then its name, and usually a first value. The pattern is typeName variableName = value;, or typeName variableName; when the value comes later. A declaration is a statement, so it ends with ;. In this lesson int holds whole numbers, double numbers with a decimal point, String text and char one character; Primitive data types explains them all.

پێش ئەوەی بەکار بهێنیت دەیناسێنیت: جۆرەکەی دەنووسیت، پاشان ناوەکەی و زۆرجار یەکەم . شێوەکەی typeName variableName = value;ـە، یان typeName variableName; کاتێک دواتر دێت. ، بۆیە بە ; کۆتایی دێت. لەم وانەیەدا int هەڵدەگرێت، double ژمارەی بە ، String دەق و char یەک ؛ جۆرەکانی دراوە هەموویان ڕوون دەکاتەوە.

The declaration int age = 21; in large letters with labels: int is the type, the kind of value; age is the name, an identifier; = stores a value; 21 is the first value; the semicolon ends the statement. Below: double price; is declared but has no value yet.ناساندنی گۆڕاوەکە int age = 21; بە پیتی گەورە لەگەڵ ناونیشانەکان: int جۆرەکەیە، جۆری نرخەکە؛ age ناوەکەیە، ناسێنەرێک؛ = نرخێک هەڵدەگرێت؛ 21 یەکەم نرخە؛ خاڵبۆرەکە ڕستەکە کۆتایی پێ دەهێنێت. لە خوارەوە: double price; ناسێنراوە بەڵام هێشتا نرخی نییە.
The parts of a declarationبەشەکانی CêIT
Declare.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Declare {
    public static void main(String[] args) {
        int age = 21;        // type, name and a first value
        double price;        // type and name only: no value yet
        price = 2.5;         // the first value comes later
        System.out.println(age);
        System.out.println(price);
    }
}

A first program with variablesیەکەم بەرنامە بە گۆڕاوەکان

This program stores an age, prints it, and works out the age after four more years of study:

ئەم تەمەنێک هەڵدەگرێت، چاپی دەکات و تەمەنەکە دوای چوار ساڵی تری خوێندن دەژمێرێت:

CalculateMyAge.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class CalculateMyAge {
    public static void main(String[] args) {
        int myAge = 18;
        System.out.println("My current age: " + myAge);
        int graduateAge = myAge + 4;
        System.out.println("My age after I graduate: " + graduateAge);
    }
}

The + sign has two jobs. Between two numbers it adds: myAge + 4 is 22. When one side is text, it joins: "My current age: " + myAge makes one string. Java works from left to right, so "Age: " + 18 + 4 prints Age: 184; write "Age: " + (18 + 4) for Age: 22 (see Joining text with +).

نیشانەی + دوو کاری هەیە. لە نێوان دوو ژمارەدا کۆ دەکاتەوە: myAge + 4 دەکاتە ⁦22⁩. کاتێک لایەکی دەق بێت، دەیانلکێنێت: "My current age: " + myAge یەک دروست دەکات. Java لە چەپەوە بۆ ڕاست کار دەکات، بۆیە "Age: " + 18 + 4 Age: 184 چاپ دەکات؛ بۆ Age: 22 بنووسە "Age: " + (18 + 4) (بڕوانە پێکەوەلکاندنی دەق بە +).

Two panels. Number plus number: myAge + 4 gives 22, it adds 18 + 4. Text plus number: "My current age: " + myAge gives "My current age: 18", it joins the number to the text.دوو بەش. ژمارە + ژمارە: myAge + 4 دەکاتە 22، 18 + 4 کۆ دەکاتەوە. دەق + ژمارە: "My current age: " + myAge دەکاتە "My current age: 18"، ژمارەکە بە دەقەکەوە دەلکێنێت.
The two jobs of +دوو کارەکەی +CêIT

Several variables in one lineچەند گۆڕاوێک لە یەک دێڕدا

Variables of the same type can be declared together, separated by commas. One value can also go into several variables at once: Java works from right to left, so 8 goes into monday, then into sunday, then into saturday. One variable per line is often easier to read, so keep this for short, closely related variables.

دەتوانیت چەند هەمان جۆر پێکەوە بناسێنیت و بە کۆما لێکیان جیا بکەیتەوە. هەروەها دەتوانیت یەک بە یەکجار بخەیتە چەند : Java لە ڕاستەوە بۆ چەپ کار دەکات، بۆیە 8 دەچێتە ناو monday، پاشان sunday و پاشان saturday. یەک لە هەر دێڕێکدا زۆرجار ئاسانتر دەخوێنرێتەوە، بۆیە ئەمە تەنیا بۆ کورت و نزیک لە یەک بەکار بهێنە.

TeaOrder.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class TeaOrder {
    public static void main(String[] args) {
        int teas = 3, coffees = 2, juices = 1;     // three variables in one line
        System.out.println(teas + coffees + juices);

        int saturday, sunday, monday;              // three variables, no values yet
        saturday = sunday = monday = 8;            // the same value for all three
        System.out.println(saturday + sunday + monday);
    }
}
Top: int teas = 3, coffees = 2, juices = 1; creates three boxes holding 3, 2 and 1. Bottom: saturday = sunday = monday = 8; the value 8 moves from right to left into monday, sunday and saturday, so all three hold 8.سەرەوە: int teas = 3, coffees = 2, juices = 1; سێ سندووق دروست دەکات کە 3، 2 و 1 هەڵدەگرن. خوارەوە: saturday = sunday = monday = 8; نرخی 8 لە ڕاستەوە بۆ چەپ دەچێتە ناو monday، sunday و saturday، بۆیە هەر سێکیان 8 هەڵدەگرن.
Several declarations, and one value for several variablesچەند ناساندنێک و یەک بۆ چەند CêIT

Assignment: putting in a new valueدانانی نرخ: دانانی نرخێکی نوێ

An statement puts a new value into a variable that already exists: variable = expression;. The left side must be a variable. The right side is an , anything that has a value, such as 19, myAge + 1 or bottles * 2. That value is stored in the variable and overwrites what was there before.

نوێ دەخاتە ناو کە پێشتر هەیە: variable = expression;. لای چەپ دەبێت بێت. لای ڕاست ، هەر شتێک کە هەبێت، وەک 19، myAge + 1 یان bottles * 2. ئەو لە هەڵدەگیرێت و جێگەی ئەوەی پێشتر تێیدا بوو دەگرێتەوە.

Declaration or assignment? int cansPerPack = 6; is a declaration: it creates the variable and gives it a first value. cansPerPack = 8; is an assignment: the variable exists already, and 8 replaces 6. The type is written only once, in the declaration.

ناساندن یان ؟ int cansPerPack = 6; : دروست دەکات و یەکەم دەداتێ. cansPerPack = 8; : پێشتر هەیە و 8 جێگەی 6 دەگرێتەوە. جۆرەکە تەنیا یەک جار دەنووسرێت، لە ناساندنەکەدا.

Assignment.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Assignment {
    public static void main(String[] args) {
        int myAge;               // declaration: a new box, no value yet
        myAge = 19;              // assignment: put 19 into it
        int cansPerPack = 6;     // declaration with a first value
        cansPerPack = 8;         // assignment: 8 replaces 6
        System.out.println(myAge + " " + cansPerPack);
    }
}

Assignment: putting in a new valueدانانی نرخ: دانانی نرخێکی نوێ

Each assignment replaces the old valueهەر دانانی نرخ جێگەی نرخی پێشوو دەگرێتەوە

Follow total through three statements. The first creates it with the value 0. The second replaces 0 with bottles * bottleVolume, which is 8.0. In the third, the same name is on both sides: Java reads the old total, adds the cans and stores the result in total again:

بەدوای totalـدا بڕۆ بە سێ . یەکەمیان بە 0 دروستی دەکات. دووەمیان 0 بە bottles * bottleVolume دەگۆڕێت، کە دەکاتە ⁦8.0⁩. لە سێیەمدا هەمان ناو لە هەردوو لادایە: Java totalـی کۆن دەخوێنێتەوە، قوتووەکانی بۆ زیاد دەکات و ئەنجامەکە دووپاتە لە totalـدا هەڵدەگرێت:

Volume.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Volume {
    public static void main(String[] args) {
        double bottleVolume = 2;      // litres in one bottle
        double canVolume = 0.355;     // litres in one can
        int bottles = 4;
        int cans = 10;

        double total = 0;                    // declared with a first value
        System.out.println(total);
        total = bottles * bottleVolume;      // assignment: 0 is replaced
        System.out.println(total);
        total = total + cans * canVolume;    // the same name on both sides
        System.out.println(total);
    }
}
Animation of the variable total. double total = 0; creates the box with 0.0. total = bottles * bottleVolume; works out 4 * 2.0 = 8.0 and replaces 0.0 with 8.0. total = total + cans * canVolume; works out 8.0 + 10 * 0.355 = 11.55 and replaces 8.0. The output shows 0.0, 8.0 and 11.55.
Animation of the variable total. double total = 0; creates the box with 0.0. total = bottles * bottleVolume; works out 4 * 2.0 = 8.0 and replaces 0.0 with 8.0. total = total + cans * canVolume; works out 8.0 + 10 * 0.355 = 11.55 and replaces 8.0. The output shows 0.0, 8.0 and 11.55.
One box, three values over timeیەک سندووق، سێ بە درێژایی کاتCêIT

= is not "equals"= واتای «یەکسانە» نییە

In Java, = does not say that the two sides are equal. It is an order: work out the expression on the right, then put its value into the variable on the left. Do not confuse it with the = of algebra, where x = x + 1 would be impossible. In Java, score = score + 1; is an ordinary line: it adds 1 to score. (To compare two values Java uses ==, which comes in The if statement.)

لە Javaـدا = ناڵێت هەردوو لا یەکسانن. فەرمانێکە: لای ڕاست بژمێرە، پاشان بخە ناو لای چەپەوە. تێکەڵی مەکە لەگەڵ =ـی جەبر، کە تێیدا x = x + 1 مەحاڵە. لە Javaـدا score = score + 1; دێڕێکی ئاساییە: 1 بۆ score زیاد دەکات. (بۆ بەراوردکردنی دوو Java == بەکار دەهێنێت، کە لە ڕستەی ifدا دێت.)

Left, in algebra: x = x + 1 means is equal to, and no number can do this. Right, in Java: score = score + 1; in three steps: read score, 5; work out 5 + 1 = 6; store 6 in score. An arrow goes from the right side to the variable on the left; the box score now holds 6 and the old 5 is crossed out.چەپ، لە جەبردا: x = x + 1 واتە یەکسانە بە و هیچ ژمارەیەک ئەمە ناکات. ڕاست، لە Javaـدا: score = score + 1; بە سێ هەنگاو: score بخوێنەوە، 5؛ 5 + 1 = 6 بژمێرە؛ 6 لە score هەڵبگرە. تیرێک لە لای ڕاستەوە دەچێت بۆ گۆڕاوەکەی لای چەپ؛ سندووقی score ئێستا 6 هەڵدەگرێت و 5ـی کۆن سڕاوەتەوە.
In Java, = means: work out the right side, store it on the leftلە Javaـدا = واتە: لای ڕاست بژمێرە و لە چەپ هەڵیبگرەCêIT
Score.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Score {
    public static void main(String[] args) {
        int score = 5;
        score = score + 1;    // read 5, add 1, store 6
        System.out.println(score);
    }
}

Tracing: swapping two valuesبەدواداچوون: گۆڕینەوەی دوو نرخ

Tracing means following a program line by line and writing down the value of every variable. Try it: a is 1234 and b is 99. What do the three statements int t = a; a = b; b = t; do?

بەدواداچوون واتە دێڕ بە دێڕ بخوێنیتەوە و هەموو بنووسیت. تاقی بکەرەوە: a ⁦1234⁩ـە و b ⁦99⁩ـە. ئەم سێ چی دەکەن: int t = a; a = b; b = t;؟

Animation with three boxes a, b and t, one statement per frame. int a, b; gives two boxes with no value yet. a = 1234; b = 99; fill them. int t = a; copies 1234 into t. a = b; puts 99 into a while 1234 is safe in t. b = t; puts 1234 into b. Last frame: swapped, a = 99 and b = 1234.
Animation with three boxes a, b and t, one statement per frame. int a, b; gives two boxes with no value yet. a = 1234; b = 99; fill them. int t = a; copies 1234 into t. a = b; puts 99 into a while 1234 is safe in t. b = t; puts 1234 into b. Last frame: swapped, a = 99 and b = 1234.
Swapping with a temporary variableگۆڕینەوە بە کاتیCêIT
TraceSwap.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class TraceSwap {
    public static void main(String[] args) {
        int a, b;
        a = 1234;
        b = 99;
        int t = a;
        a = b;
        b = t;
        System.out.println("a = " + a + ", b = " + b + ", t = " + t);
    }
}

They swap the two values. t is a temporary variable: it keeps a copy of a, so 1234 is not lost when a gets the value of b.

ئەوان دوو دەگۆڕنەوە. t کاتییە: کۆپییەکی a دەپارێزێت، بۆیە کاتێک a b وەردەگرێت، ⁦1234⁩ ون نابێت.

Tracing: swapping two valuesبەدواداچوون: گۆڕینەوەی دوو نرخ

The swap as a trace tableگۆڕینەوەکە وەک خشتەی بەدواداچوون

On paper the same steps make a : one row per statement, one column per variable. A dash means "no value yet": Java would not even let you read such a variable (see No value yet).

لەسەر کاغەز هەمان هەنگاوەکان دروست دەکەن: ڕیزێک بۆ هەر و ستوونێک بۆ هەر . هێڵێکی کورت واتە «هێشتا بێ »: Java تەنانەت ڕێگە نادات ئەو جۆرە بخوێنیتەوە (بڕوانە هێشتا بێ نرخ).

Statementabt
int a, b;——
a = 1234;1234—
b = 99;123499
int t = a;1234991234
a = b;99991234
b = t;9912341234

Declare before you useپێش بەکارهێنان بیناسێنە

You must declare a variable before you use it for the first time. javac reads the statements in order: when it reaches line 3, it does not know yet that literPerOunce will be declared on line 4, so it stops:

دەبێت پێش یەکەم بەکارهێنانی بناسێنیت. javac بە ڕیز دەخوێنێتەوە: کاتێک دەگاتە دێڕی 3، هێشتا نازانێت کە literPerOunce لە دێڕی 4دا دەناسێنرێت، بۆیە دەوەستێت:

CanVolume.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class CanVolume {
    public static void main(String[] args) {
        double canVolume = 12 * literPerOunce;
        double literPerOunce = 0.0296;
        System.out.println(canVolume);
    }
}
javac reads from the top down. Wrong order: line 3 double canVolume = 12 * literPerOunce; uses a name that javac has not met yet, so it says cannot find symbol. Right order: double literPerOunce = 0.0296; first, then double canVolume = 12 * literPerOunce; which gives 0.3552.javac لە سەرەوە بۆ خوارەوە دەخوێنێتەوە. ڕیزی هەڵە: دێڕی 3 double canVolume = 12 * literPerOunce; ناوێک بەکار دەهێنێت کە javac هێشتا نەیبینیوە، بۆیە دەڵێت cannot find symbol. ڕیزی ڕاست: سەرەتا double literPerOunce = 0.0296;، پاشان double canVolume = 12 * literPerOunce; کە 0.3552 دەدات.
Declare first, then useسەرەتا بیناسێنە، پاشان بەکاری بهێنەCêIT

Swap the two lines and it compiles: canVolume is 0.3552. The same message comes when a name is misspelt or never declared: age = 21; alone gives cannot find symbol with symbol: variable age.

دوو دێڕەکە بگۆڕەوە و کۆمپایل دەبێت: canVolume دەکاتە 0.3552. هەمان پەیام دێت کاتێک ناوێک هەڵە نووسرابێت یان هەرگیز نەناسێنرابێت: age = 21; بە تەنیا cannot find symbol لەگەڵ symbol: variable age دەدات.

Declare before you useپێش بەکارهێنان بیناسێنە

No value yetهێشتا بێ نرخ

A related error is a variable that is declared but has no value yet. Java gives the variables inside a method (, like all the variables in this lesson) no automatic value, and javac will not let you read one before it has been given a value, its :

هەڵەیەکی پەیوەندیدار کە ناسێنراوە بەڵام هێشتا نییە. Java هیچ خۆکار نادات بە ناو (، وەک هەموو ئەم وانەیە) و javac ڕێگە نادات یەکێکیان بخوێنیتەوە پێش ئەوەی پێ درابێت، واتە پێش :

Bottles.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Bottles {
    public static void main(String[] args) {
        int bottles;
        int bottleVolume = bottles * 2;
        System.out.println(bottleVolume);
    }
}
Left: int bottles; gives a box labelled int with a question mark inside; reading it gives variable bottles might not have been initialized. Right: int bottles = 4; gives the box the value 4: a first value, ready to use.چەپ: int bottles; سندووقێکی int دەدات کە نیشانەی پرسیاری تێدایە؛ خوێندنەوەی variable bottles might not have been initialized دەدات. ڕاست: int bottles = 4; نرخی 4 دەداتە سندووقەکە: یەکەم نرخ، ئامادەیە بۆ بەکارهێنان.
A local variable must get a value before it is read پێش خوێندنەوە دەبێت وەربگرێتCêIT

The remedy is to give the variable a value before you use it, best in its declaration: with int bottles = 4; the program prints 8. (Variables that belong to objects do get default values such as 0; they come with classes and objects.)

چارەسەرەکە ئەوەیە پێش بەکارهێنان بدەیت بە ، باشترە لە ناساندنەکەیدا: بە int bottles = 4; 8 چاپ دەکات. ( ناو وەک 0 وەردەگرن؛ لەگەڵ کلاسەکان و ئۆبجێکتەکان دێن.)

Constants: values that never changeنەگۆڕەکان: نرخێک کە هەرگیز ناگۆڕێت

A named holds a value that never changes while the program runs, such as π. Instead of typing 3.14159 again and again, you declare it once with final: final datatype CONSTANTNAME = value;. By convention the name is in capital letters, with underscores between words: PI, CAN_VOLUME.

ناودار هەڵدەگرێت کە لە کاتی کارکردنی هەرگیز ناگۆڕێت، وەک π. لە جیاتی ئەوەی ⁦3.14159⁩ جار لە دوای جار بنووسیت، یەک جار بە final دەیناسێنیت: final datatype CONSTANTNAME = value;. بەپێی نەریت ناوەکەی بە پیتی گەورە دەنووسرێت و لە نێوان وشەکاندا دادەنرێت: PI، CAN_VOLUME.

Circle.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Circle {
    public static void main(String[] args) {
        final double PI = 3.14159;
        double radius = 2;
        double area = PI * radius * radius;
        System.out.println(area);
        System.out.println(Math.PI);
    }
}
The constant PI declared with final double PI = 3.14159; is a box with a padlock. The line PI = 3; bounces off it and javac says cannot assign a value to final variable PI. Beside it: Java's own π, Math.PI, is 3.141592653589793, with more digits than 3.14159, and is a constant too.نەگۆڕی PI کە بە final double PI = 3.14159; ناسێنراوە سندووقێکە بە قوفڵێکەوە. دێڕی PI = 3; لێی دەگەڕێتەوە و javac دەڵێت cannot assign a value to final variable PI. لە تەنیشتیەوە: πـی خودی Java، Math.PI، 3.141592653589793ـە، تاکژمارەی زیاتری هەیە لە 3.14159 و ئەویش نەگۆڕە.
A final variable is locked final قوفڵ دراوەCêIT

If you try to change it, javac stops you: cannot assign a value to final variable PI. Java also has its own, more precise π, Math.PI (more in The Math class). Constants make a program easier to read and easier to change: to use another value, you edit one line. Later, final also locks the fields of an object (final fields).

ئەگەر هەوڵ بدەیت بیگۆڕیت، javac ڕێت لێ دەگرێت: cannot assign a value to final variable PI. Java πـی خۆشی هەیە کە وردترە: Math.PI (زیاتر لە کلاسی Math). ئاسانتر دەکەن بۆ خوێندنەوە و بۆ گۆڕین: بۆ بەکارهێنانی تر، تەنیا یەک دێڕ دەستکاری دەکەیت. دواتر final قوفڵ دەدات (تایبەتمەندییەکانی final).

Let the compiler find the type: varبا کۆمپایلەرەکە جۆرەکە بدۆزێتەوە: var

Since Java 10 you may write var instead of the type of a local variable. The compiler looks at the first value and works out the type itself; this is called . The type is still fixed afterwards: Java stays statically typed (Static and dynamic typing).

لە ⁦Java 10⁩ـەوە دەتوانیت لە جیاتی جۆری var بنووسیت. سەیری یەکەم دەکات و خۆی جۆرەکە دەدۆزێتەوە؛ ئەمە پێی دەگوترێت . جۆرەکە دواتر هەر جێگیرە: Java هەر بە دەمێنێتەوە (جۆرداریی جێگیر و جۆرداریی ناجێگیر).

VarDemo.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class VarDemo {
    public static void main(String[] args) {
        var count = 10;          // 10 is an int, so count is an int
        var price = 2.5;         // a double
        var name = "Shilan";     // a String
        count = 12;              // fine: still an int
        System.out.println(count + " " + price + " " + name);
    }
}
The compiler looks at the first value. var count = 10; looks at 10 and makes count an int. var price = 2.5; looks at 2.5 and makes price a double. var name = "Shilan"; makes name a String. Below: the type is fixed, so count = 9.99; gives possible lossy conversion.کۆمپایلەرەکە سەیری یەکەم نرخ دەکات. var count = 10; سەیری 10 دەکات و count دەکات بە int. var price = 2.5; سەیری 2.5 دەکات و price دەکات بە double. var name = "Shilan"; name دەکات بە String. لە خوارەوە: جۆرەکە جێگیرە، بۆیە count = 9.99; possible lossy conversion دەدات.
var: the type comes from the first valuevar: جۆرەکە لە یەکەم دێتCêIT

Rules: var needs a first value (var total; gives cannot infer type for local variable total), it cannot start from null, and it works only for local variables. After var count = 10; the line count = 9.99; fails, because count is an int. While you are learning, write the type itself: it tells the reader more.

یاساکان: var پێویستی بە یەکەم هەیە (var total; دەدات cannot infer type for local variable total)، ناتوانێت بە null دەست پێ بکات و تەنیا بۆ کار دەکات. دوای var count = 10; دێڕی count = 9.99; شکست دەهێنێت، چونکە count intـە. تا فێر دەبیت، خودی جۆرەکە بنووسە: زانیاری زیاتر دەداتە خوێنەر.

Rules for names (identifiers)یاساکانی ناو (ناسێنەرەکان)

The names you give to classes, methods and variables are called . Every identifier must follow these rules:

ئەو ناوانەی دەیدەیت بە ، و پێیان دەگوترێت . هەموو دەبێت پەیڕەوی ئەم یاسایانە بکات:

  • It may contain letters, digits, the underscore _ and the dollar sign $; no spaces or other symbols.
  • It cannot start with a digit.
  • It cannot be a keyword (see Keywords), nor true, false or null.
  • It can be of any length, and capital letters matter: area, Area and AREA are three names.
  • دەتوانێت پیت، ، _ و نیشانەی دۆلار $ـی تێدا بێت؛ بێ بۆشایی و هێمای تر.
  • ناتوانێت بە دەست پێ بکات.
  • ناتوانێت بێت (بڕوانە وشە کلیلەییەکان)، هەروەها نە true، false و نە null.
  • دەتوانێت بە هەر درێژییەک بێت و پیتی گەورە گرنگە: area، Area و AREA سێ ناوی جیاوازن.
The rules on top: letters, digits, _ and $; never a digit first; no spaces or other symbols; not a keyword nor true, false, null; capital letters matter. Legal names with ticks: radius, _total, area2, $4 (legal, but avoid $) and the Kurdish word tamen (legal, but English is better). Illegal names with crosses: 2A starts with a digit, d+4 has a +, my var has a space, class is a keyword, _ alone is a keyword.یاساکان لە سەرەوە: پیت، تاکژمارە، _ و $؛ هەرگیز تاکژمارە لە سەرەتادا نا؛ بێ بۆشایی و هێمای تر؛ نەک وشەی کلیلەیی و نەک true، false، null؛ پیتی گەورە گرنگە. ناوە دروستەکان بە نیشانەی ✓: radius، _total، area2، $4 (دروستە، بەڵام $ بەکار مەهێنە) و وشەی کوردیی تەمەن (دروستە، بەڵام ئینگلیزی باشترە). ناوە هەڵەکان بە نیشانەی ✗: 2A بە تاکژمارە دەست پێ دەکات، d+4 +ـی تێدایە، my var بۆشایی تێدایە، class وشەی کلیلەییە، _ بە تەنیا وشەی کلیلەییە.
Legal and illegal identifiers دروست و هەڵەCêIT

ComputeArea, area and radius are legal. javac stops at 2A (not a statement, ';' expected) and at d+4 (';' expected). The $ is legal but meant for names that tools generate, and a lone _ is a keyword since Java 9. Letters of any alphabet are allowed, so int تەمەن = 20; compiles, but programmers write names in English.

ComputeArea، area و radius دروستن. javac لە 2A (not a statement، ';' expected) و لە d+4 (';' expected) دەوەستێت. $ دروستە بەڵام بۆ ئەو ناوانەیە کە ئامرازەکان دروستیان دەکەن و _ بە تەنیا لە ⁦Java 9⁩ـەوە . پیتی هەموو ئەلفوبێیەک ڕێگە پێدراوە، بۆیە int تەمەن = 20; کۆمپایل دەبێت، بەڵام ناو بە ئینگلیزی دەنووسن.

Good names: Java's conventionsناوی باش: نەریتەکانی Java

Rules decide what compiles; conventions decide what other Java programmers expect to read, and following them makes programs easier to read and avoids mistakes. Choose descriptive names with a clear meaning, such as graduateAge, not ga. Then use Java's usual styles:

یاساکان بڕیار دەدەن چی کۆمپایل دەبێت؛ نەریتەکان بڕیار دەدەن تری Java چاوەڕێی خوێندنەوەی چی دەکەن و پەیڕەوکردنیان ئاسانتر دەکات بۆ خوێندنەوە و ڕێگە لە هەڵە دەگرێت. ناوی ڕوون و بە واتا هەڵبژێرە، وەک graduateAge نەک ga. پاشان شێوازە باوەکانی Java بەکار بهێنە:

Three columns. camelCase for variables and methods: myAge, graduateAge, computeArea(). PascalCase for classes: CalculateMyAge, ComputeArea, String. UPPER_SNAKE_CASE for constants: CAN_VOLUME, MAX_SPEED, PI. The capital letters and underscores are coloured. Below: packages are all small letters, java.util.سێ ستوون. camelCase بۆ گۆڕاوەکان و میتۆدەکان: myAge، graduateAge، computeArea(). PascalCase بۆ کلاسەکان: CalculateMyAge، ComputeArea، String. UPPER_SNAKE_CASE بۆ نەگۆڕەکان: CAN_VOLUME، MAX_SPEED، PI. پیتە گەورەکان و بنهێڵەکان ڕەنگ کراون. لە خوارەوە: پاکێجەکان هەمووی پیتی بچووکن، java.util.
Java's naming stylesشێوازەکانی ناونانی JavaCêIT
  • for variables and methods: start small, then a capital for each new word: myAge, computeArea.
  • for classes: every word starts with a capital: CalculateMyAge.
  • UPPER_SNAKE_CASE for constants: CAN_VOLUME; packages are all small letters: java.util.
  • بۆ و : بە پیتی بچووک دەست پێ بکە، پاشان پیتی گەورە بۆ هەر وشەیەکی نوێ: myAge، computeArea.
  • بۆ : هەموو وشەیەک بە پیتی گەورە دەست پێ دەکات: CalculateMyAge.
  • ⁦UPPER_SNAKE_CASE⁩ بۆ : CAN_VOLUME؛ هەمووی پیتی بچووکن: java.util.
Case.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Case {
    public static void main(String[] args) {
        int area = 1;
        int Area = 2;
        int AREA = 3;
        System.out.println(area + Area + AREA);
    }
}

Keywords: words Java keeps for itselfوشە کلیلەییەکان: ئەو وشانەی Java بۆ خۆی هەڵیگرتوون

, also called reserved words, have a fixed meaning for the compiler, so they cannot be used for anything else, such as names. When javac sees class, for example, it knows that the next word is the name of a class. Java 17 has 51 of them:

واتایەکی جێگیریان هەیە بۆ ، بۆیە ناتوانرێت بۆ هیچ شتێکی تر بەکار بهێنرێن، وەک ناو. بۆ نموونە کاتێک javac class دەبینێت، دەزانێت وشەی دواتر ناوی . ⁦Java 17⁩ کۆی ⁦51⁩ هەیە:

The 51 reserved keywords of Java 17 as chips in six groups. Types: boolean, byte, char, short, int, long, float, double, void. Decisions and loops: if, else, switch, case, default, for, do, while, break, continue, return. Classes and objects: class, interface, enum, extends, implements, new, this, super, package, import, instanceof. Words like public and static: public, private, protected, static, final, abstract, native, synchronized, transient, volatile, strictfp. Errors: try, catch, finally, throw, throws, assert. Reserved, never used: const, goto and _. Not in the 51: the literals true, false and null, also never names; and contextual words such as var, record, yield and sealed, which are keywords only in some places.51 وشەی کلیلەیی کە Java 17 پاراستوونی، لە شەش گرووپدا. جۆرەکان: boolean، byte، char، short، int، long، float، double، void. بڕیار و خولەکان: if، else، switch، case، default، for، do، while، break، continue، return. کلاسەکان و ئۆبجێکتەکان: class، interface، enum، extends، implements، new، this، super، package، import، instanceof. وشەی وەک public و static: public، private، protected، static، final، abstract، native، synchronized، transient، volatile، strictfp. هەڵەکان: try، catch، finally، throw، throws، assert. پارێزراو و هەرگیز بەکارنەهاتوو: const، goto و _. لە ناو 51ـەکەدا نین: لیتەراڵەکان true، false و null کە ئەوانیش هەرگیز ناو نین؛ و وشەی وەک var، record، yield و sealed کە تەنیا لە هەندێک شوێندا وشەی کلیلەیین.
The keywords of Java 17 ⁦Java 17⁩CêIT

const and goto are reserved but never used, strictfp is obsolete since Java 17, and _ became a keyword in Java 9. true, false and null are , not keywords, but they cannot be names either. Words such as var, record and yield are contextual: special only in certain places. A keyword used as a name confuses javac completely:

const و goto پارێزراون بەڵام هەرگیز بەکار نایەن، strictfp لە ⁦Java 17⁩ـەوە بەسەرچووە و _ لە ⁦Java 9⁩دا بوو بە . true، false و null نەک ، بەڵام ئەوانیش ناتوانن ببن بە ناو. وشەی وەک var، record و yield بەپێی شوێن تایبەتن: تەنیا لە هەندێک شوێندا . کە وەک ناو بەکار بێت javac بە تەواوی سەری لێ دەشێوێنێت:

Names.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Names {
    public static void main(String[] args) {
        int class = 1;
    }
}

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

Every message below is the real output of javac 17.

هەموو پەیامێکی خوارەوە ڕاستەقینەی ⁦javac 17⁩ـە.

Mistakeهەڵەjavac saysjavac دەڵێتFixچارەسەر
Using a variable above its declarationبەکارهێنانی لە سەرووی ناساندنەکەیەوەcannot find symbol symbol: variable literPerOunceDeclare it first, above its first use.سەرەتا بیناسێنە، لە سەرووی یەکەم بەکارهێنانییەوە.
Reading a variable that has no value yetخوێندنەوەی کە هێشتا نییەvariable bottles might not have been initializedGive it a first value.یەکەم پێ بدە.
Declaring the same name twiceناساندنی هەمان ناو دوو جارvariable x is already defined in method main(String[])Declare once; later lines assign without the type.یەک جار بیناسێنە؛ دێڕەکانی دواتر بێ جۆر دادەنێن.
Changing a final constantگۆڕینی finalcannot assign a value to final variable PIDo not change it, or remove final if it is not a constant.مەیگۆڕە، یان ئەگەر نییە final لاببە.
var without a first valuevar بێ یەکەم cannot infer type for local variable totalGive it a value, or write the type. پێ بدە، یان جۆرەکە بنووسە.
A value of the wrong type لە جۆری هەڵەincompatible types: String cannot be converted to intUse a matching type: int age = 20; (see Data types).جۆرێکی گونجاو بەکار بهێنە: int age = 20; (بڕوانە جۆرەکانی دراوە).
A name that starts with a digit or is a keywordناوێک کە بە دەست پێ دەکات یان not a statement ';' expectedChoose another name: secondPlace.ناوێکی تر هەڵبژێرە: secondPlace.
Swapping with a = b; b = a;گۆڕینەوە بە a = b; b = a;(no message: both hold the same value)(هیچ پەیامێک: هەردووکیان هەمان هەڵدەگرن)Use a temporary variable t. کاتی t بەکار بهێنە.
"Age: " + 18 + 4"Age: " + 18 + 4(no message: prints Age: 184)(هیچ پەیامێک: Age: 184 چاپ دەکات)Add brackets: "Age: " + (18 + 4).کەوانە دابنێ: "Age: " + (18 + 4).

Practiceڕاهێنان

ExerciseڕاهێنانWhich names are valid?کام ناوانە دروستن؟

Which of these identifiers are valid? Which are Java keywords? miles, Test, a++, --a, 4#R, $4, #44, apps, Class, public, int, x, y, radius. Check your answers by declaring each one as int name = 1;.

کام لەم ناوانە دروستن؟ کامیان Javaـن؟ miles، Test، a++، --a، 4#R، $4، #44، apps، Class، public، int، x، y، radius. بە ناساندنی هەریەکەیان وەک int name = 1; وەڵامەکانت بپشکنە.

Show answerوەڵامەکە پیشان بدە
NameناوAnswerوەڵامjavac saysjavac دەڵێت
milesvalidدروستە—
Testvalidدروستە—
a++invalidنادروستە';' expected
--ainvalidنادروستەnot a statement ';' expected
4#Rinvalidنادروستەnot a statement ';' expected illegal character: '#'
$4valid (avoid $)دروستە ($ بەکار مەهێنە)—
#44invalidنادروستەillegal character: '#' not a statement
appsvalidدروستە—
Classvalid, but confusing: Java has a class called Classدروستە، بەڵام سەرلێشێوێنە: Java بە ناوی Classـی هەیە—
publickeywordnot a statement ';' expected … 5 errors
intkeywordnot a statement ';' expected … 4 errors
xvalidدروستە—
yvalidدروستە—
radiusvalidدروستە—
ExerciseڕاهێنانLab: swap two lettersتاقیگە: گۆڕینەوەی دوو پیت

Write a program that swaps the values of the variables var1 = 'M' and var2 = 'J', and prints them before and after the swap.

بنووسە کە var1 = 'M' و var2 = 'J' بگۆڕێتەوە و پێش و دوای گۆڕینەوەکە چاپیان بکات.

Hintئاماژە

Use a third variable, as in the trace. A char holds one character in single quotes, such as 'M' (The char type).

سێیەم بەکار بهێنە، وەک لە بەدواداچوونەکەدا. char یەک لە ناو تاکدا هەڵدەگرێت، وەک 'M' (جۆری char).

Show answerوەڵامەکە پیشان بدە
Task5.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Task5 {
    public static void main(String[] args) {
        char var1 = 'M';
        char var2 = 'J';
        System.out.println("****** Task 5 *******");
        System.out.println("Before swapping: var1 = " + var1 + ", var2 = " + var2);
        char temp = var1;
        var1 = var2;
        var2 = temp;
        System.out.println("After swapping:  var1 = " + var1 + ", var2 = " + var2);
    }
}

The same three steps as the trace above, with a temporary char variable.

هەمان سێ هەنگاوی بەدواداچوونەکەی سەرەوە، بە کاتیی char.

ExerciseڕاهێنانTrace itبەدوایدا بچۆ

Trace these lines with a trace table, then predict the output: int x = 3; int y = x * 2; x = y + 1; y = x - y; and System.out.println("x = " + x + ", y = " + y);

ئەم دێڕانە بە بەدواداچوونیان بۆ بکە، پاشان پێشبینی بکە: int x = 3; int y = x * 2; x = y + 1; y = x - y; و System.out.println("x = " + x + ", y = " + y);

Show answerوەڵامەکە پیشان بدە
Statementxy
int x = 3;3—
int y = x * 2;36
x = y + 1;76
y = x - y;71
Outputدەرئەنجام
x = 7, y = 1
ExerciseڕاهێنانRotate three valuesسوڕاندنەوەی سێ نرخ

first, second and third hold 10, 20 and 30. Move every value one place to the left, so that they hold 20, 30 and 10. Use one temporary variable.

first، second و third ⁦10⁩، ⁦20⁩ و ⁦30⁩ هەڵدەگرن. هەموو یەک شوێن بەرەو چەپ بجووڵێنە، بۆ ئەوەی ⁦20⁩، ⁦30⁩ و ⁦10⁩ هەڵبگرن. یەک کاتی بەکار بهێنە.

Show answerوەڵامەکە پیشان بدە
Swap3.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Swap3 {
    public static void main(String[] args) {
        int first = 10, second = 20, third = 30;
        int temp = first;
        first = second;
        second = third;
        third = temp;
        System.out.println(first + " " + second + " " + third);
    }
}
ExerciseڕاهێنانUse a constantنەگۆڕێک بەکار بهێنە

Write a program with the constant MINUTES_PER_HOUR that prints how many minutes there are in 3 hours.

بنووسە بە MINUTES_PER_HOUR کە چاپ بکات 3 کاتژمێر چەند خولەکە.

Show answerوەڵامەکە پیشان بدە
Minutes.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class Minutes {
    public static void main(String[] args) {
        final int MINUTES_PER_HOUR = 60;
        int hours = 3;
        int minutes = hours * MINUTES_PER_HOUR;
        System.out.println(hours + " hours = " + minutes + " minutes");
    }
}

Summaryپوختە

  • A variable has a name, a type and a value. Declare it as type name = value; before you use it.
  • = assigns: work out the right side, store it on the left, overwriting the old value. To swap two values, use a temporary variable.
  • A local variable must get a value before it is read; final makes a constant, named in UPPER_SNAKE_CASE.
  • var (Java 10+) lets the compiler infer the type from the first value; the type stays fixed.
  • Names: letters, digits, _, $; no digit first; not one of the 51 keywords; case matters. Use camelCase for variables, PascalCase for classes.
  • ناوێک، جۆرێک و هەیە. پێش بەکارهێنان وەک type name = value; بیناسێنە.
  • = دادەنێت: لای ڕاست بژمێرە و لە لای چەپ هەڵیبگرە، لە جیاتی پێشوو. بۆ گۆڕینەوەی دوو ، کاتی بەکار بهێنە.
  • پێش خوێندنەوە دەبێت وەربگرێت؛ final دروست دەکات کە بە ⁦UPPER_SNAKE_CASE⁩ ناو دەنرێت.
  • var (⁦Java 10⁩ و دواتر) ڕێگە دەدات جۆرەکە لە یەکەم بدۆزێتەوە؛ جۆرەکە جێگیر دەمێنێتەوە.
  • ناوەکان: پیت، ، _، $؛ لە سەرەتادا نا؛ نەک یەکێک لە ⁦51⁩ ؛ پیتی گەورە گرنگە. بۆ و بۆ بەکار بهێنە.
Review questionsپرسیارەکانی پێداچوونەوە
What is the difference between a declaration and an assignment?جیاوازیی نێوان ناساندن و دانانی نرخ چییە؟

A declaration creates a variable and names its type (int n = 5;); an assignment gives an existing variable a new value (n = 6;).

ناساندن دروست دەکات و جۆرەکەی دەڵێت (int n = 5;)؛ نوێ دەدات بە کە هەیە (n = 6;).

Why does int bottles; int v = bottles * 2; not compile?بۆچی int bottles; int v = bottles * 2; کۆمپایل نابێت؟

bottles is a local variable with no value yet, and Java does not let you read it until it gets one.

bottles کە هێشتا نییە و Java ڕێگە نادات بیخوێنیتەوە تا وەربگرێت.

Is var a type?ئایا var جۆرە؟

No. It asks the compiler to work out the type from the first value; the variable then has that type for good.

نەخێر. داوا لە دەکات جۆرەکە لە یەکەم بدۆزێتەوە؛ پاشان بۆ هەمیشە ئەو جۆرەی هەیە.

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

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

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

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

Primitive Data Typesجۆرە سەرەتاییەکان

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