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.
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 symbolandmight not have been initialized, and usefinalconstants andvar. - 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 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 ناتوانێت دەق هەڵبگرێت.
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 یەک ؛ جۆرەکانی دراوە هەموویان ڕوون دەکاتەوە.
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);
}
}$ java Declare 21 2.5
A first program with variablesیەکەم بەرنامە بە گۆڕاوەکان
This program stores an age, prints it, and works out the age after four more years of study:
ئەم تەمەنێک هەڵدەگرێت، چاپی دەکات و تەمەنەکە دوای چوار ساڵی تری خوێندن دەژمێرێت:
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);
}
}$ java CalculateMyAge My current age: 18 My age after I graduate: 22
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) (بڕوانە پێکەوەلکاندنی دەق بە +).
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. یەک لە هەر دێڕێکدا زۆرجار ئاسانتر دەخوێنرێتەوە، بۆیە ئەمە تەنیا بۆ کورت و نزیک لە یەک بەکار بهێنە.
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);
}
}$ java TeaOrder 6 24
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 دەگرێتەوە. جۆرەکە تەنیا یەک جار دەنووسرێت، لە ناساندنەکەدا.
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);
}
}$ java Assignment 19 8
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ـدا هەڵدەگرێت:
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);
}
}$ java Volume 0.0 8.0 11.55


= 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دا دێت.)
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);
}
}$ java Score 6
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;؟


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);
}
}$ java TraceSwap a = 99, b = 1234, t = 1234
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 تەنانەت ڕێگە نادات ئەو جۆرە بخوێنیتەوە (بڕوانە هێشتا بێ نرخ).
| Statement | a | b | t |
|---|---|---|---|
int a, b; | — | — | |
a = 1234; | 1234 | — | |
b = 99; | 1234 | 99 | |
int t = a; | 1234 | 99 | 1234 |
a = b; | 99 | 99 | 1234 |
b = t; | 99 | 1234 | 1234 |
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دا دەناسێنرێت، بۆیە دەوەستێت:
public class CanVolume {
public static void main(String[] args) {
double canVolume = 12 * literPerOunce;
double literPerOunce = 0.0296;
System.out.println(canVolume);
}
}$ javac CanVolume.java
CanVolume.java:3: error: cannot find symbol
double canVolume = 12 * literPerOunce;
^
symbol: variable literPerOunce
location: class CanVolume
1 errorSwap 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 ڕێگە نادات یەکێکیان بخوێنیتەوە پێش ئەوەی پێ درابێت، واتە پێش :
public class Bottles {
public static void main(String[] args) {
int bottles;
int bottleVolume = bottles * 2;
System.out.println(bottleVolume);
}
}$ javac Bottles.java
Bottles.java:4: error: variable bottles might not have been initialized
int bottleVolume = bottles * 2;
^
1 errorThe 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.
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);
}
}$ java Circle 12.56636 3.141592653589793
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 هەر بە دەمێنێتەوە (جۆرداریی جێگیر و جۆرداریی ناجێگیر).
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);
}
}$ java VarDemo 12 2.5 Shilan
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,falseornull. - It can be of any length, and capital letters matter:
area,AreaandAREAare three names.
- دەتوانێت پیت، ،
_و نیشانەی دۆلار$ـی تێدا بێت؛ بێ بۆشایی و هێمای تر. - ناتوانێت بە دەست پێ بکات.
- ناتوانێت بێت (بڕوانە وشە کلیلەییەکان)، هەروەها نە
true،falseو نەnull. - دەتوانێت بە هەر درێژییەک بێت و پیتی گەورە گرنگە:
area،AreaوAREAسێ ناوی جیاوازن.
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 بەکار بهێنە:
- 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.
public class Case {
public static void main(String[] args) {
int area = 1;
int Area = 2;
int AREA = 3;
System.out.println(area + Area + AREA);
}
}$ java Case 6
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 هەیە:
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 بە تەواوی سەری لێ دەشێوێنێت:
public class Names {
public static void main(String[] args) {
int class = 1;
}
}$ javac Names.java
Names.java:3: error: not a statement
int class = 1;
^
Names.java:3: error: ';' expected
int class = 1;
^
Names.java:3: error: <identifier> expected
int class = 1;
^
3 errorsCommon 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 literPerOunce | Declare it first, above its first use.سەرەتا بیناسێنە، لە سەرووی یەکەم بەکارهێنانییەوە. |
| Reading a variable that has no value yetخوێندنەوەی کە هێشتا نییە | variable bottles might not have been initialized | Give 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گۆڕینی final | cannot assign a value to final variable PI | Do not change it, or remove final if it is not a constant.مەیگۆڕە، یان ئەگەر نییە final لاببە. |
var without a first valuevar بێ یەکەم | cannot infer type for local variable total | Give it a value, or write the type. پێ بدە، یان جۆرەکە بنووسە. |
| A value of the wrong type لە جۆری هەڵە | incompatible types: String cannot be converted to int | Use 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
';' expected | Choose 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ڕاهێنان
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 دەڵێت |
|---|---|---|
miles | validدروستە | — |
Test | validدروستە | — |
a++ | invalidنادروستە | ';' expected |
--a | invalidنادروستە | not a statement
';' expected |
4#R | invalidنادروستە | not a statement
';' expected
illegal character: '#' |
$4 | valid (avoid $)دروستە ($ بەکار مەهێنە) | — |
#44 | invalidنادروستە | illegal character: '#'
not a statement |
apps | validدروستە | — |
Class | valid, but confusing: Java has a class called Classدروستە، بەڵام سەرلێشێوێنە: Java بە ناوی Classـی هەیە | — |
public | keyword | not a statement
';' expected
…
5 errors |
int | keyword | not a statement
';' expected
…
4 errors |
x | validدروستە | — |
y | validدروستە | — |
radius | validدروستە | — |
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وەڵامەکە پیشان بدە
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);
}
}$ java Task5 ****** Task 5 ******* Before swapping: var1 = M, var2 = J After swapping: var1 = J, var2 = M
The same three steps as the trace above, with a temporary char variable.
هەمان سێ هەنگاوی بەدواداچوونەکەی سەرەوە، بە کاتیی char.
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وەڵامەکە پیشان بدە
| Statement | x | y |
|---|---|---|
int x = 3; | 3 | — |
int y = x * 2; | 3 | 6 |
x = y + 1; | 7 | 6 |
y = x - y; | 7 | 1 |
x = 7, y = 1
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وەڵامەکە پیشان بدە
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);
}
}$ java Swap3 20 30 10
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وەڵامەکە پیشان بدە
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");
}
}$ java Minutes 3 hours = 180 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;
finalmakes 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 ؛ پیتی گەورە گرنگە. بۆ و بۆ بەکار بهێنە.
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زاراوە سەرەکییەکان
Primitive Data Typesجۆرە سەرەتاییەکان
Related lecturesوانە پەیوەندیدارەکان
- Primitive Data Typesجۆرە سەرەتاییەکان
- Errors and Debugging: Syntax, Runtime and Logic Errorsهەڵەکان و کرمەکوژی: هەڵەی داڕستن، هەڵەی کاتی جێبەجێکردن و هەڵەی لۆجیکی
- Variables and Data Typesگۆڕاوەکان و جۆرەکانی دراوە
- Python Next to JavaPython لە تەنیشت Javaـەوە
- Algorithms, Pseudocode and Flowchartsئەلگۆریزمەکان، نیمچەکۆد و هێڵکارییەکانی ڕەوت
- The Math Classکلاسەکەی Math
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- W3Schools: Java Variables
- W3Schools: Declare Many Variables
- W3Schools: Java Identifiers
- W3Schools: Java Constants (final)
- W3Schools: The var Keyword
- The Java Language Specification, Java SE 17 — chapter 3: identifiers (§3.8) and keywords (§3.9)
- The Java Language Specification, Java SE 17 — §4.12.4 final variables and §4.12.5 initial values
- The Java Language Specification, Java SE 17 — §14.4 local variable declarations and var
- JEP 286: Local-Variable Type Inference (Java 10)
- dev.java: Using the var type identifier
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.