PythonPython · Chapterبەش 12 · Lectureوانە 12.3
Python Next to JavaPython لە تەنیشت Javaـەوە
Read Java by comparing it with the Python you know: Hello World and the class with main, javac and the JVM, declared types and compile-time errors, comments, if/else if, for/while/do-while, methods, arrays and ArrayList — the course report's examples corrected and run — then comparison tables on syntax, speed and uses, the course's other tracks (MATLAB today, perhaps Java and C# later), and a summary of the whole Python track.
Java بخوێنەرەوە بە بەراوردکردنی لەگەڵ ئەو Pythonـەی دەیزانیت: Hello World و لەگەڵ main، javac و JVM، جۆری ناسێنراو و ، ، if/else if، for/while/do-while، ، و ArrayList — نموونەکانی ڕاپۆرتی کۆرسەکە ڕاستکراو و جێبەجێکراو — پاشان خشتەی بەراورد بۆ ، خێرایی و بەکارهێنان، ڕێڕەوەکانی تری کۆرسەکە (MATLAB ئەمڕۆ، لەوانەیە Java و C# دواتر) و پوختەی هەموو ڕێڕەوی .
What you will learnچی فێر دەبیت
- Read a small Java program: the class, the main method, declarations, braces and semicolons.
- Explain how javac and the JVM run Java, and how Python runs a file, in today's bytecode model.
- Translate variables, decisions, loops, functions and lists between Python and Java.
- Tell compile-time errors from runtime errors, and fix the errors in the course report's Java.
- Compare the two languages on typing, syntax, compilation, speed and uses, and choose what to learn next.
- بچووکی Java بخوێنیتەوە: ، main، ناساندنەکان، کەوانە و .
- ڕوونی بکەیتەوە javac و JVM چۆن Java جێبەجێ دەکەن و چۆن جێبەجێ دەکات، بە شێوازی ئەمڕۆی .
- ، بڕیاردان، ، و لە نێوان و Javaـدا وەربگێڕیت.
- لە جیا بکەیتەوە و هەڵەکانی Javaـی ڕاپۆرتی کۆرسەکە چاک بکەیتەوە.
- دوو زمانەکە لە جۆرداری، ، کۆمپایلکردن، خێرایی و بەکارهێناندا بەراورد بکەیت و هەڵبژێریت دواتر فێری چی ببیت.
Why look at Java?بۆچی سەیری Java بکەین؟
You have finished the Python track. Before you go on, it helps to see how the same ideas look in another language. Java is a good partner: it is one of the most used languages for servers, banks and big companies, and it makes different choices from Python.
ڕێڕەوی Pythonـت تەواو کرد. پێش ئەوەی بڕۆیتە پێشەوە، بەسوودە ببینیت هەمان بیرۆکەکان لە زمانێکی تردا چۆن دەردەکەون. هاوەڵێکی باشە: یەکێکە لە بەکارهاتووترین زمانەکان بۆ ، بانک و کۆمپانیا گەورەکان و هەڵبژاردنی جیاواز لە دەکات.
This lesson takes the examples of the course report “Python and Java” — variables, printing, comments, decisions, loops, functions, lists and arrays — and puts the Python code and the Java code side by side. Every Java example was corrected, compiled and run.
ئەم وانەیە نموونەکانی ڕاپۆرتی کۆرسەکە «Python and Java» وەردەگرێت — ، چاپکردن، ، بڕیاردان، ، ، و — و کۆدی و کۆدی لە تەنیشت یەکەوە دادەنێت. هەموو نموونەیەکی ڕاست کرایەوە، کۆمپایل کرا و جێبەجێ کرا.
In Kurdish: what Java is — its start in 1995, its owner Oracle, that it is object-oriented, and the kinds of apps people build with it.
بە کوردی: چییە: لە ساڵی 1995 دەستی پێکرد، خاوەنەکەی Oracleـە، و چ جۆرە پێی دروست دەکرێت.
Kurdish (Sorani). Watch 10:15-12:33: the Java slide — made in 1995 by James Gosling's team, now owned by Oracle, object-oriented, and used for desktop, web, mobile and business apps and games. The earlier part is a general introduction to programming that students of this lesson already know.
ڤیدیۆیەکی کوردی (سۆرانی)، لە 10:15 تا 12:33 سەیر بکە: : لە ساڵی 1995 تیمەکەی James Gosling دروستیان کرد، ئێستا هی Oracleـە، و بۆ ، ، مۆبایل و بازرگانی، هەروەها بەکاردێت. بەشی پێشووتر ناساندنێکی گشتییە بۆ کە قوتابیانی ئەم وانەیە پێشتر دەیزانن.
Hello World in both languagesHello World لە هەردوو زماندا
The smallest program shows the biggest difference. In Python one line is a complete program. In Java every program lives inside a class, and running starts in a special method called main.
بچووکترین گەورەترین جیاوازی پیشان دەدات. لە Pythonـدا یەک دێڕ تەواوە. لە Javaـدا هەموو لە ناو دا دەژی و جێبەجێکردن لە تایبەتدا بە ناوی main دەست پێ دەکات.
print("Hello World")Hello World
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World");
}
}Hello World
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
public class Hello— the class name must match the file nameHello.java.public class Hello— ناوی دەبێت وەک ناویHello.javaبێت.- Lineدێڕی 2
public static void main(String[] args)— the fixed first line of every program's start.void: it returns nothing;String[] args: words typed after the program name.public static void main(String[] args)— دێڕی جێگیری دەستپێکی هەموو .void: هیچ ناگەڕێنێتەوە؛String[] args: ئەو وشانەی دوای ناوی دەنووسرێن.- Lineدێڕی 3
System.out.println= “print line”: it prints the text and then a new line, like Python'sprint(). The;ends the statement.System.out.println= «print line»: دەقەکە چاپ دەکات و پاشان دێڕێکی نوێ، وەکprint()ـی .;تەواو دەکات.
What Java is: code compiled to bytecode that the JVM runs, a class with a main method, then compiling with javac and running with java.
چییە: کۆد بۆ کۆمپایل دەکرێت و JVM جێبەجێی دەکات، بە main، پاشان کۆمپایلکردن بە javac و جێبەجێکردن بە java.
A fast 2:25 overview: how Java works (0:28), the class and its main method (1:11) and compile and run (2:00).
پێداچوونەوەیەکی خێرای 2:25: چۆن کار دەکات (0:28)، و mainـەکەی (1:11) و کۆمپایلکردن و جێبەجێکردن (2:00).
Hello World in both languagesHello World لە هەردوو زماندا
Compiling and runningکۆمپایلکردن و جێبەجێکردن
Java has two commands. javac (the Java compiler) checks the whole file and translates it into bytecode in a .class file. java starts the Java virtual machine (JVM), which runs that bytecode. Python does both steps with one command.
دوو هەیە. javac ( ) هەموو دەپشکنێت و دەیکات بە لە .classـدا. java (JVM) دەست پێ دەکات کە ئەو جێبەجێ دەکات. هەردوو هەنگاو بە یەک دەکات.
$ javac Hello.java $ ls Hello.class Hello.java $ java Hello Hello World
$ python3 hello.py Hello World
Since Java 11 you can also type java Hello.java: the launcher compiles the file in memory and runs it at once. That is handy for small programs; bigger programs are still compiled with javac (or a build tool).
لە Java 11ـەوە دەتوانیت java Hello.javaـیش بنووسیت: لە کۆمپایل دەکات و یەکسەر جێبەجێی دەکات. ئەمە بۆ بچووک بەسوودە؛ گەورەتر هێشتا بە javac (یان ئامرازێکی دروستکردن) کۆمپایل دەکرێن.
Hello World in both languagesHello World لە هەردوو زماندا
How the two languages run todayدوو زمانەکە ئەمڕۆ چۆن کار دەکەن
Both languages translate your code before it runs; they differ in when they do it and what they check first.
هەردوو زمان پێش جێبەجێکردن کۆدەکەت وەردەگێڕن؛ جیاوازییەکەیان لەوەدایە کەی ئەمە دەکەن و سەرەتا چی دەپشکنن.
| Topicبابەت | Python (CPython) | Java |
|---|---|---|
| You typeدەنووسیت | python3 hello.py | javac Hello.java then java Hello |
| Translationوەرگێڕان | Python compiles the file to bytecode by itself, every run (cached in .pyc files for imported modules) خۆی لە هەموو جێبەجێکردنێکدا دەکات بە (بۆ کە import دەکرێن لە .pycـدا هەڵدەگیرێت) | You compile first with javac; the .class files keep the bytecodeسەرەتا خۆت بە javac کۆمپایل دەکەیت؛ .class هەڵدەگرن |
| Who runs the bytecodeکێ جێبەجێ دەکات | The Python virtual machine, instruction by instruction (an experimental JIT exists since Python 3.13; it is still switched off by default) ، ڕێنمایی بە ڕێنمایی ( JITـی تاقیکاری لە Python 3.13ـەوە هەیە، بەڵام هێشتا بە شێوەی بنەڕەت کوژاوەیە) | The JVM: it interprets, then compiles busy (“hot”) methods to machine code with its JIT compilerJVM: سەرەتا ئینتەرپرێت دەکات، پاشان («گەرم») بە دەکات بە |
| Grammar and type mistakesهەڵەی و جۆر | Grammar: before the file runs. Types and unknown names: only when that line runs: پێش جێبەجێبوونی . جۆر و ناوی نەناسراو: تەنها کاتێک ئەو دێڕە جێبەجێ دەبێت | All of them before anything runs (compile-time errors)هەموویان پێش ئەوەی هیچ جێبەجێ بێت () |
Same program, two languagesهەمان بەرنامە، دوو زمان
Here is a slightly bigger job: add the numbers 1 to 5. Watch how each Python line has a partner line in Java, and what Java adds around them.
ئەمە کارێکی کەمێک گەورەترە: ژمارەکانی 1 تا 5 کۆ بکەرەوە. سەیر بکە چۆن هەر دێڕێکی دێڕێکی هاوتای لە Javaـدا هەیە و چی بە دەوریاندا زیاد دەکات.


total = 0
for i in range(1, 6):
total += i
print("Total:", total)Total: 15
public class Total {
public static void main(String[] args) {
int total = 0;
for (int i = 1; i <= 5; i++) {
total += i;
}
System.out.println("Total: " + total);
}
}Total: 15
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
A variable needs its type:
int= whole number.جۆرەکەی پێویستە:
int= .- Lineدێڕی 4
Start
int i = 1; conditioni <= 5; stepi++(add 1). Python'srange(1, 6)says the same.دەستپێک
int i = 1؛i <= 5؛ هەنگاوi++(1 زیاد بکە).range(1, 6)ـی هەمان شت دەڵێت.- Lineدێڕی 5-6
The body is between
{and}; the indentation is only for people.جەستە لە نێوان
{و}ـدایە؛ تەنها بۆ خەڵکە.- Lineدێڕی 7
+joins text and a number into one string.+دەق و ژمارەیەک دەکات بە یەک .
Variables and typesگۆڕاوەکان و جۆرەکان
In Python you create a variable by giving it a value. In Java you first declare it: you write its type, its name, and usually a first value, and end with ;.
لە Pythonـدا بە پێدانی دروست دەکەیت. لە Javaـدا سەرەتا دەیناسێنیت: جۆرەکەی، ناوەکەی و زۆرجار یەکەم دەنووسیت و بە ; کۆتایی پێ دەهێنیت.
| Topicبابەت | Python | Java |
|---|---|---|
| Syntax | var_name = value | datatype var_name = value; |
| Exampleنموونە | age = 21 | int age = 21; |
age = 21
price = 2.5
is_raining = True
grade = "A"
name = "Shilan"
print(name, "is", age)
print(price, is_raining, grade)
age = 22
total = float(age) # Python converts only when you ask
print(total)
age = "twenty-two" # allowed: the name now points to a string
print(type(age))Shilan is 21 2.5 True A 22.0 <class 'str'>
public class Variables {
public static void main(String[] args) {
int age = 21; // whole number
double price = 2.5; // decimal number
boolean isRaining = true; // true or false
char grade = 'A'; // one character, single quotes
String name = "Shilan"; // text: a class, capital S
System.out.println(name + " is " + age);
System.out.println(price + " " + isRaining + " " + grade);
age = 22; // a new value of the same type is fine
double total = age; // an int widens to a double by itself
System.out.println(total);
}
}Shilan is 21 2.5 true A 22.0
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-7
Each declaration names a type.
charuses single quotes;Stringuses double quotes and starts with a capital S.هەر ناساندنێک ناوی جۆرێک دەهێنێت.
charکۆتەیشنی تاک بەکار دەهێنێت؛Stringکۆتەیشنی جووت بەکار دەهێنێت و بە Sـی گەورە دەست پێ دەکات.- Lineدێڕی 9
In Java,
+turns the number into text when one side is a String.لە Javaـدا، کاتێک لایەکی String بێت،
+ژمارەکە دەکات بە دەق.- Lineدێڕی 12
A new value of the same type is fine.
age = "twenty-two";would not compile.نوێ لە هەمان جۆر کێشە نییە.
age = "twenty-two";کۆمپایل نابێت.- Lineدێڕی 13-14
An
intfits into adoubleby itself (widening): 22 becomes 22.0.intخۆی دەچێتە ناوdouble(فراوانکردن): 22 دەبێت بە 22.0.
Variables and typesگۆڕاوەکان و جۆرەکان
Java's eight primitive typesهەشت جۆرە سەرەتاییەکانی Java
Java has eight primitive types for simple values. Their names are written in small letters. Everything else — text, arrays, lists, your own classes — is an object; that is why String starts with a capital letter. Java is case-sensitive, so string is not the same name as String.
لە Javaـدا هەشت هەیە بۆ . ناوەکانیان بە پیتی بچووک دەنووسرێن. هەموو شتێکی تر — دەق، ، ، خۆت — ؛ بۆیە String بە پیتی گەورە دەست پێ دەکات. ، بۆیە string هەمان ناوی String نییە.
| Java primitive type لە Javaـدا | Holdsهەڵدەگرێت | Nearest Python typeنزیکترین جۆری Python |
|---|---|---|
byte | whole number, 8 bits ( to 127)، 8 بیت ( تا 127) | int |
short | whole number, 16 bits، 16 بیت | int |
int | whole number, 32 bits — the usual choice، 32 بیت — هەڵبژاردنی ئاسایی | int |
long | whole number, 64 bits، 64 بیت | int |
float | decimal number, 32 bits، 32 بیت | float |
double | decimal number, 64 bits — the usual choice، 64 بیت — هەڵبژاردنی ئاسایی | float |
char | one character, in single quotes: 'A'یەک ، لە ناو کۆتەیشنی تاکدا: 'A' | a string of length 1 بە درێژیی 1 |
boolean | true or false (small letters)true یان false (پیتی بچووک) | bool: True / Falsebool: True / False |
Variables and typesگۆڕاوەکان و جۆرەکان
Type mistakes: before or during the run?هەڵەی جۆر: پێش جێبەجێکردن یان لە کاتیدا؟
Because Java knows every variable's type, javac finds type mistakes before the program runs: a compile-time error. The course report itself had one — it declared a temperature as boolean:
لەبەر ئەوەی جۆری هەموو دەزانێت، javac هەڵەی جۆر پێش جێبەجێبوونی دەدۆزێتەوە: . ڕاپۆرتی کۆرسەکە خۆی یەکێکی تێدا بوو — پلەی گەرمی بە boolean ناساندبوو:
Main.java:4: error: incompatible types: int cannot be converted to boolean
boolean temp = 30;
^
1 errorboolean holds only true or false; a temperature is a whole number, so the fix is int temp = 30;. The same check stops text in a number variable:
boolean تەنها true یان false هەڵدەگرێت؛ پلەی گەرمی ، بۆیە چارەسەرەکە int temp = 30;ـە. هەمان پشکنین ڕێگە لە دەق دەگرێت لە ژمارەدا:
Main.java:3: error: incompatible types: String cannot be converted to int
int age = "twenty";
^
1 errorPython checks types while the program runs. The mistake below is found only when line 2 is reached. Notice also that Python's + does not turn numbers into text; use str(age) or an f-string.
جۆرەکان لە کاتی جێبەجێبوونی دەپشکنێت. هەڵەی خوارەوە تەنها کاتێک دەدۆزرێتەوە کە دەگاتە دێڕی 2. هەروەها تێبینی بکە کە +ـی ژمارە ناکات بە دەق؛ str(age) یان بەکار بهێنە.
age = 21
print("Age: " + age)Traceback (most recent call last):
File "/Users/aram/python/concat.py", line 2, in <module>
print("Age: " + age)
~~~~~~~~^~~~~
TypeError: can only concatenate str (not "int") to strAn that the finds while it translates the , before the program runs: a missing ;, a misspelt name or a wrong type such as int age = "twenty";. Nothing runs until it is fixed. Compare .
کە لە کاتی وەرگێڕانی دەیدۆزێتەوە، پێش ئەوەی کار بکات: ;ـێکی ونبوو، ناوێکی هەڵە نووسراو یان جۆرێکی هەڵە وەک int age = "twenty";. تا چاک نەکرێت هیچ جێبەجێ نابێت. بەراوردی بکە لەگەڵ .
Printing and commentsچاپکردن و لێدوانەکان
| Topicبابەت | Python | Java |
|---|---|---|
| Print, then a new lineچاپکردن، پاشان دێڕی نوێ | print("Hi") | System.out.println("Hi"); |
| Print, stay on the lineچاپکردن، مانەوە لە هەمان دێڕ | print("Hi", end="") | System.out.print("Hi"); |
| Text and a numberدەق و ژمارە | print("Age:", age) | System.out.println("Age: " + age); |
| One-line comment یەک دێڕی | # comment | // comment |
| Comment over several lines بە چەند دێڕ | # on every line# لەسەر هەموو دێڕێک | /* comment */ |
| Documentation | """docstring""" as the first line of a function"""docstring""" وەک یەکەم دێڕی | /** javadoc comment */ |
/**
* Prints greetings. The javadoc tool turns this
* documentation comment into a web page.
*/
public class Greeter {
public static void main(String[] args) {
// This is a one-line comment
/*
A multiline
comment
*/
System.out.println("Hello World"); // a comment can follow code
}
}Hello World
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-4
A documentation comment starts with
/**. Thejavadoctool reads these comments and builds web pages that describe the classes and methods.بە
/**دەست پێ دەکات. ئامرازیjavadocئەم دەخوێنێتەوە و پەڕەی وێب دروست دەکات کە و وەسف دەکەن.- Lineدێڕی 7
//hides the rest of the line from the compiler.//باقیی دێڕەکە لە دەشارێتەوە.- Lineدێڕی 8-11
/* ... */can cover several lines./* ... */دەتوانێت چەند دێڕێک بگرێتەوە.- Lineدێڕی 12
A comment can also follow code on the same line.
دەتوانێت دوای کۆد لە هەمان دێڕدا بێت.
Decisions: if, else if, elseبڕیاردان: if، else if، else
The idea is the same in both languages; only the spelling changes. Python writes elif, Java writes else if. Java needs parentheses around every condition and braces around every block. Python needs neither, only the colon and the indentation.
بیرۆکەکە لە هەردوو زماندا وەک یەکە؛ تەنها نووسینەکە دەگۆڕێت. elif دەنووسێت، else if. کەوانەی بچووکی بە دەوری هەموو و کەوانەی گەورەی بە دەوری هەموو پێویستە. هیچیانی پێویست نییە، تەنها دوو خاڵ و .
From here on, the short Java examples show only the lines that go inside main. To run one, put it inside the class and the main method of Hello World.
لێرەوە بە دواوە، نموونە کورتەکانی Java تەنها ئەو دێڕانە پیشان دەدەن کە دەچنە ناو main. بۆ جێبەجێکردنی یەکێکیان، بیخەرە ناو و mainـی Hello World.
num = 80
if num >= 80:
print("Excellent!")
elif num >= 50:
print("Good.")
else:
print("Failed!")Excellent!
int num = 80;
if (num >= 80) {
System.out.println("Excellent!");
} else if (num >= 50) {
System.out.println("Good.");
} else {
System.out.println("Failed!");
}Excellent!
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The condition in parentheses, the block in braces.
لە ناو کەوانەی بچووکدا، لە ناو کەوانەی گەورەدا.
- Lineدێڕی 4
} else if (...) {is Java'selif.} else if (...) {هاوتایelifـی Javaـیە.
| Topicبابەت | Python | Java |
|---|---|---|
| and / or / notو / یان / نا | a and b, a or b, not a | a && b, a || b, !a |
| True / Falseڕاست / هەڵە | True, False | true, false |
| Equal / not equal (numbers)یەکسان / نایەکسان (ژمارە) | ==, != | ==, != |
Decisions: if, else if, elseبڕیاردان: if، else if، else
A nested if: what to wearڕستەی ifـی ناوەکی: چی لەبەر بکەین
The report's second example chooses advice by the weather. Its messages are in Kurdish written with Latin letters:
نموونەی دووەمی ڕاپۆرتەکە بەپێی کەش و هەوا ئامۆژگاری هەڵدەبژێرێت. پەیامەکانی بە کوردیی نووسراو بە پیتی لاتینین:
| Message in the codeپەیام لە کۆدەکەدا | Meaningواتا | Whenکەی |
|---|---|---|
Ştêkî tenîk leber bike. | Wear something light.شتێکی تەنک لەبەر بکە. | raining, above 20°باران، سەرووی 20° |
Cilêkî estûr leber bike. | Wear thick clothes.جلێکی ئەستوور لەبەر بکە. | raining, 20° or lessباران، 20° یان کەمتر |
Keşekey xirap nîye. | The weather is not bad.کەشەکە خراپ نییە. | snowing, above 10°بەفر، سەرووی 10° |
Agadarî tendrûstît be. | Look after your health.ئاگاداری تەندروستیت بە. | snowing, 10° or lessبەفر، 10° یان کەمتر |
Rojêkî xweşe! | It's a nice day!ڕۆژێکی خۆشە! | no rain, no snowنە باران، نە بەفر |
is_raining = True
is_snowing = False
temp = 30
if is_raining:
if temp > 20:
print("Ştêkî tenîk leber bike.")
else:
print("Cilêkî estûr leber bike.")
elif is_snowing:
if temp > 10:
print("Keşekey xirap nîye.")
else:
print("Agadarî tendrûstît be.")
else:
print("Rojêkî xweşe!")Ştêkî tenîk leber bike.
boolean isRaining = true;
boolean isSnowing = false;
int temp = 30;
if (isRaining) {
if (temp > 20) {
System.out.println("Ştêkî tenîk leber bike.");
} else {
System.out.println("Cilêkî estûr leber bike.");
}
} else if (isSnowing) {
if (temp > 10) {
System.out.println("Keşekey xirap nîye.");
} else {
System.out.println("Agadarî tendrûstît be.");
}
} else {
System.out.println("Rojêkî xweşe!");
}Ştêkî tenîk leber bike.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1-3
Corrected declarations:
isSnowingis now declared, andtempis anint.ناساندنە ڕاستکراوەکان:
isSnowingئێستا ناسێنراوە وtempintـە.- Lineدێڕی 4-9
An
ifinside anif: each inner block has its own braces.ifـێک لە ناوifـێکدا: هەر بلۆکێکی ناوەوە کەوانەی خۆی هەیە.
The report never declared isSnowing in either language. The two languages react very differently. Java refuses to compile the file at all:
ڕاپۆرتەکە لە هیچ کام لە دوو زمانەکەدا isSnowingـی نەناساندبوو. دوو زمانەکە زۆر بە جیاوازی کاردانەوەیان هەیە. بە هیچ شێوەیەک کۆمپایل ناکات:
Main.java:7: error: cannot find symbol
} else if (isSnowing) {
^
symbol: variable isSnowing
location: class Main
1 errorPython runs happily while it is raining, because the elif line is never reached. Only on a dry day does it crash:
تا باران ببارێت بە ئاسایی کار دەکات، چونکە هەرگیز ناگاتە دێڕی elif. تەنها لە ڕۆژێکی وشکدا دەوەستێت:
isRaining = False
temp = 30
if (isRaining):
if (temp > 20):
print("Ştêkî tenîk leber bike.")
else:
print("Cilêkî estûr leber bike.")
elif (isSnowing):
if (temp > 10):
print("Keşekey xirap nîye.")
else:
print("Agadarî tendrûstît be.")
else:
print("Rojêkî xweşe!")Traceback (most recent call last):
File "/Users/aram/python/weather.py", line 8, in <module>
elif (isSnowing):
^^^^^^^^^
NameError: name 'isSnowing' is not definedLoops: while and forخولەکان: while و for
A while loop looks almost the same in both languages. Java adds parentheses, braces and ;, and it has the short form i++ for “add 1 to i”; Python writes i += 1.
لە هەردوو زماندا نزیکەی وەک یەکە. Java کەوانەی بچووک، کەوانەی گەورە و ; زیاد دەکات و شێوە کورتەکەی i++ـی هەیە بۆ «1 بخەرە سەر i»؛ i += 1 دەنووسێت.
i = 1
while i <= 10:
print(i)
i += 11 2 3 4 5 6 7 8 9 10
int i = 1;
while (i <= 10) {
System.out.println(i);
i++; // the same as i = i + 1;
}1 2 3 4 5 6 7 8 9 10
The counting for loop is where the languages differ most. Java's classic for has three parts in its header, separated by ;: start (int i = 1), condition (i <= 10) and step (i++). Python's for walks through a sequence, and range(1, 11) makes the numbers 1 to 10.
بۆ ژماردن ئەو شوێنەیە کە زمانەکان زۆرترین جیاوازییان هەیە. لە Javaـدا کلاسیک سێ بەشی لە سەرەکەیدا هەیە کە بە ; جیا دەکرێنەوە: دەستپێک (int i = 1)، (i <= 10) و هەنگاو (i++). لە Pythonـدا بە ناو دەڕوات و range(1, 11) ژمارەکانی 1 تا 10 دروست دەکات.
for i in range(1, 11):
print("Example", i)Example 1 Example 2 Example 3 Example 4 Example 5 Example 6 Example 7 Example 8 Example 9 Example 10
for (int i = 1; i <= 10; i++) {
System.out.println("Example " + i);
}Example 1 Example 2 Example 3 Example 4 Example 5 Example 6 Example 7 Example 8 Example 9 Example 10
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
start; condition; step
دەستپێک؛ ؛ هەنگاو
- Lineدێڕی 2
The report forgot this
;."Example " + ineeds the space inside the quotes, because Java's+adds none.ڕاپۆرتەکە ئەم
;ـەی لەبیر کردبوو."Example " + iپێویستی بە لە ناو کۆتەیشنەکاندا هەیە، چونکە+ـی Java هیچ زیاد ناکات.
| Java loop Java | When to use itکەی بەکاری دەهێنیت | Python wayڕێگای Python |
|---|---|---|
for (int i = 1; i <= 10; i++) { … } | You know how many timesدەزانیت چەند جار | for i in range(1, 11): |
while (condition) { … } | You do not know how many times; test firstنازانیت چەند جار؛ سەرەتا پشکنین | while condition: |
do { … } while (condition); | The body must run at least once; test lastجەستە دەبێت لانیکەم یەک جار جێبەجێ بێت؛ پشکنین لە کۆتاییدا | while True: … if not condition: breakwhile True: … if not condition: break |
for (String w : words) { … } | Visit every item of an array or listسەردانی هەموو یان | for w in words: |
for (;;) / while (true) / do { … } while (true); | A loop that runs until break کە تا break کار دەکات | while True: |
Loops: while and forخولەکان: while و for
do … while: Java onlydo … while: تەنها لە Javaـدا
Java has a third loop that Python does not have: do … while. It runs the body first and tests the condition afterwards, so the body always runs at least once — useful for menus (“show the menu, then ask whether to show it again”).
Java سێیەمی هەیە کە نییەتی: do … while. سەرەتا جەستەکە جێبەجێ دەکات و دواتر دەپشکنێت، بۆیە جەستەکە هەمیشە لانیکەم یەک جار جێبەجێ دەبێت — بۆ بەسوودە (« پیشان بدە، پاشان بپرسە دووبارە پیشانی بدەیتەوە یان نا»).
int n = 10;
do {
System.out.println("n is " + n);
n++;
} while (n < 5);
System.out.println("Finished with n = " + n);n is 10 Finished with n = 11
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-5
The body runs once with
n = 10. Only then isn < 5tested: false, so the loop ends.جەستەکە یەک جار بە
n = 10جێبەجێ دەبێت. تەنها پاشانn < 5دەپشکنرێت: هەڵەیە، بۆیە کۆتایی دێت.- Lineدێڕی 5
A
do … whileends with;after the condition.do … whileدوای بە;کۆتایی دێت.
n = 10
while True:
print("n is", n)
n += 1
if not n < 5: # the do-while test, at the bottom
break
print("Finished with n =", n)n is 10 Finished with n = 11
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Python: start an endless loop …
: بێکۆتا دەست پێ بکە …
- Lineدێڕی 5-6
… and test at the bottom, with
break.… و لە خوارەوە بە
breakبپشکنە.
Loops: while and forخولەکان: while و for
Where a variable livesگۆڕاوێک لە کوێ دەژی
In Java a variable declared inside a block { … } — or in a for header — exists only inside that block (block scope). Using it after the block is a compile-time error, and you cannot declare the same name twice in one method:
لە Javaـدا کە لە ناو بلۆکێکی { … } — یان لە سەرەی forـدا — ناسێنرابێت تەنها لە ناو ئەو بلۆکەدا بوونی هەیە (). بەکارهێنانی دوای بلۆکەکە و ناتوانیت هەمان ناو دوو جار لە یەک بناسێنیت:
public class Main {
public static void main(String[] args) {
for (int k = 1; k <= 3; k++) {
int square = k * k;
}
System.out.println(k);
}
}Main.java:6: error: cannot find symbol
System.out.println(k);
^
symbol: variable k
location: class Main
1 errorpublic class Main {
public static void main(String[] args) {
int i = 1;
while (i <= 3) {
i++;
}
for (int i = 1; i <= 3; i++) {
System.out.println(i);
}
}
}Main.java:7: error: variable i is already defined in method main(String[])
for (int i = 1; i <= 3; i++) {
^
1 errorPython has no block scope: a loop or an if does not make a new scope, so the loop variable is still there after the loop (see Variable scope).
نییە: یان if نوێ دروست ناکات، بۆیە دوای هەر دەمێنێتەوە (بڕوانە مەودا).
for k in range(1, 4):
square = k * k
print(k, square) # both names still exist after the loop3 9
Functions and methodsکردارەکان و میتۆدەکان
Python lets you write a function anywhere in a file. In Java every function lives inside a class and is called a method. For now you write static before it, which means “it belongs to the class; no object is needed”. Java also writes the return type: void for nothing, int for a whole number, and so on.
ڕێگەت پێ دەدات لە هەر شوێنێکی بنووسیت. لە Javaـدا هەموو لە ناو دەژی و پێی دەوترێت . بۆ ئێستا static لە پێشیەوە دەنووسیت، واتە «سەر بە ؛ هیچ پێویست نییە». Java هەروەها جۆری دەنووسێت: void بۆ هیچ، int بۆ و هتد.
def say_hello():
print("Hello World!")
def square(n):
return n * n
say_hello()
print(square(7))Hello World! 49
public class Main {
public static void main(String[] args) {
sayHello();
System.out.println(square(7));
}
static void sayHello() {
System.out.println("Hello World!");
}
static int square(int n) {
return n * n;
}
}Hello World! 49
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-4
maincalls the two methods, like the last lines of the Python file.mainبانگی دوو دەکات، وەک دوا دێڕەکانی Python.- Lineدێڕی 7
void: returns nothing. Method names use camelCase in Java:sayHello, notsay_hello.void: هیچ ناگەڕێنێتەوە. ناوی لە Javaـدا camelCaseـن:sayHello، نەکsay_hello.- Lineدێڕی 11-12
int square(int n): the parameter has a type, and so does the result.int square(int n): جۆری هەیە، هەروەها.
Python lists and Java arraysلیستەکانی Python و ڕیزەکەکانی Java
A Python list grows as you append(). A Java array is created with a fixed length: new String[2] makes exactly two boxes, numbered 0 and 1. Every element must have the array's type. The boxes sit next to each other in memory, so Java finds any box from its index at once.
بە append() گەورە دەبێت. Java بە درێژییەکی جێگیر دروست دەکرێت: new String[2] ڕێک دوو خانە دروست دەکات بە ژمارەکانی 0 و 1. هەموو دەبێت جۆری هەبێت. خانەکان لە لە تەنیشت یەکەوەن، بۆیە Java هەر خانەیەک لە ڕێگەی یەکسەر دەدۆزێتەوە.
country = []
country.append("Kurdistan")
country.append("Egypt")
for j in country:
print(j)Kurdistan Egypt
String[] country = new String[2];
country[0] = "Kurdistan";
country[1] = "Egypt";
for (String j : country) {
System.out.println(j);
}
System.out.println("Length: " + country.length);Kurdistan Egypt Length: 2
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
String[]= “array of Strings”;new String[2]reserves 2 boxes. The report wrotenew string[2].String[]= « String»؛new String[2]دوو خانە دیاری دەکات. ڕاپۆرتەکەnew string[2]ـی نووسیبوو.- Lineدێڕی 4
The enhanced
for: “for each String j in country”.forـی پەرەپێدراو: «بۆ هەر String j لە country».- Lineدێڕی 7
.lengthgives the fixed size..lengthقەبارە جێگیرەکە دەدات.
Asking for a box that does not exist stops both programs, but with different messages:
داواکردنی خانەیەک کە بوونی نییە هەردوو دەوەستێنێت، بەڵام بە پەیامی جیاواز:
public class Main {
public static void main(String[] args) {
String[] country = new String[2];
country[0] = "Kurdistan";
country[1] = "Egypt";
country[2] = "Iraq"; // there is no box number 2
}
}Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2 at Main.main(Main.java:6)
country = ["Kurdistan", "Egypt"]
print(country[2])Traceback (most recent call last):
File "/Users/aram/python/bounds.py", line 2, in <module>
print(country[2])
~~~~~~~^^^
IndexError: list index out of rangeThe report adds: an array stores elements of one type (homogeneous), while a Python list can mix types (heterogeneous), for example [21, "Aram", 3.5]. That is true, but in practice most Python lists also hold one kind of thing — a list of marks, a list of names — because that is easier to work with.
ڕاپۆرتەکە زیاد دەکات: یەک جۆر هەڵدەگرێت (هاوجۆر)، لە کاتێکدا دەتوانێت جۆرەکان تێکەڵ بکات (جۆراوجۆر)، بۆ نموونە [21, "Aram", 3.5]. ئەمە ڕاستە، بەڵام لە ڕاستیدا زۆربەی Pythonـیش یەک جۆر شت هەڵدەگرن — نمرە، ناو — چونکە کارکردن لەگەڵیان ئاسانترە.
Python lists and Java arraysلیستەکانی Python و ڕیزەکەکانی Java
ArrayList: Java's growing listArrayList: لیستی گەورەبووی Java
When Java programmers need a list that grows, they use ArrayList from the library java.util. It is the closest partner of a Python list.
کاتێک Java پێویستیان بە کە گەورە بێت، ArrayList لە java.util بەکار دەهێنن. ئەمە نزیکترین هاوتای Pythonـە.
import java.util.ArrayList;
public class Countries {
public static void main(String[] args) {
ArrayList<String> country = new ArrayList<>();
country.add("Kurdistan");
country.add("Egypt");
country.add("Japan"); // it grows, like append()
System.out.println(country);
System.out.println(country.get(0) + ", size " + country.size());
for (String j : country) {
System.out.println(j);
}
}
}[Kurdistan, Egypt, Japan] Kurdistan, size 3 Kurdistan Egypt Japan
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Like Python's
import: bringArrayListin from the library.وەک
importـی :ArrayListلە بهێنە.- Lineدێڕی 5
<String>says what the list holds. It holds objects only: for whole numbers writeArrayList<Integer>.<String>دەڵێت چی هەڵدەگرێت. تەنها هەڵدەگرێت: بۆ بنووسەArrayList<Integer>.- Lineدێڕی 6-8
add()is Python'sappend().add()هاوتایappend()ـی Pythonـە.- Lineدێڕی 10
get(0)iscountry[0];size()islen(country).get(0)هاوتایcountry[0]ـە؛size()هاوتایlen(country)ـە.
country = []
country.append("Kurdistan")
country.append("Egypt")
country.append("Japan")
print(country)
print(country[0] + ", size", len(country))
for j in country:
print(j)['Kurdistan', 'Egypt', 'Japan'] Kurdistan, size 3 Kurdistan Egypt Japan
How a fixed-size Java array and a growing ArrayList are created and used: length versus size(), adding and removing elements.
بە قەبارەی جێگیر و ArrayListـێک کە گەورە دەبێت چۆن دروست دەکرێن و بەکاردێن: length بەرامبەر size()، زیادکردن و لابردنی .
Watch 0:50-15:37: creating arrays and ArrayLists (diamond operator at 4:09), length versus size(), adding (10:40), removing (13:29) and printing (14:20): an ArrayList prints all its elements with println. Stops before his recommendation.
لە 0:50 تا 15:37 سەیر بکە: دروستکردنی و ArrayList ( diamond <> لە 4:09)، length بەرامبەر size()، زیادکردن (10:40)، لابردن (13:29) و پیشاندان (14:20): ArrayList بە println هەموو خۆی پیشان دەدات. پێش ئامۆژگارییەکەی ڕادەوەستێت.
The report's examples as one programنموونەکانی ڕاپۆرتەکە وەک یەک بەرنامە
Putting all the corrected examples together gives one Python file and one Java file. Run them both: they print exactly the same 26 lines.
کۆکردنەوەی هەموو نموونە ڕاستکراوەکان یەک و یەک Java دەدات. هەردووکیان جێبەجێ بکە: ڕێک هەمان 26 دێڕ چاپ دەکەن.
# The report's examples as one Python program (corrected)
age = 21
print("Hello World")
num = 80
if num >= 80:
print("Excellent!")
elif num >= 50:
print("Good.")
else:
print("Failed!")
is_raining = True
is_snowing = False
temp = 30
if is_raining:
if temp > 20:
print("Ştêkî tenîk leber bike.")
else:
print("Cilêkî estûr leber bike.")
elif is_snowing:
if temp > 10:
print("Keşekey xirap nîye.")
else:
print("Agadarî tendrûstît be.")
else:
print("Rojêkî xweşe!")
i = 1
while i <= 10:
print(i)
i += 1
for i in range(1, 11):
print("Example", i)
def func_name():
print("Hello World!")
func_name()
country = []
country.append("Kurdistan")
country.append("Egypt")
for j in country:
print(j)Hello World Excellent! Ştêkî tenîk leber bike. 1 2 3 4 5 6 7 8 9 10 Example 1 Example 2 Example 3 Example 4 Example 5 Example 6 Example 7 Example 8 Example 9 Example 10 Hello World! Kurdistan Egypt
// The report's examples as one Java program (corrected)
public class Main {
public static void main(String[] args) {
int age = 21;
System.out.println("Hello World");
int num = 80;
if (num >= 80) {
System.out.println("Excellent!");
} else if (num >= 50) {
System.out.println("Good.");
} else {
System.out.println("Failed!");
}
boolean isRaining = true;
boolean isSnowing = false;
int temp = 30;
if (isRaining) {
if (temp > 20) {
System.out.println("Ştêkî tenîk leber bike.");
} else {
System.out.println("Cilêkî estûr leber bike.");
}
} else if (isSnowing) {
if (temp > 10) {
System.out.println("Keşekey xirap nîye.");
} else {
System.out.println("Agadarî tendrûstît be.");
}
} else {
System.out.println("Rojêkî xweşe!");
}
int i = 1;
while (i <= 10) {
System.out.println(i);
i++;
}
for (int k = 1; k <= 10; k++) {
System.out.println("Example " + k);
}
funcName();
String[] country = new String[2];
country[0] = "Kurdistan";
country[1] = "Egypt";
for (String j : country) {
System.out.println(j);
}
}
private static void funcName() {
System.out.println("Hello World!");
}
}Hello World Excellent! Ştêkî tenîk leber bike. 1 2 3 4 5 6 7 8 9 10 Example 1 Example 2 Example 3 Example 4 Example 5 Example 6 Example 7 Example 8 Example 9 Example 10 Hello World! Kurdistan Egypt
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
int age = 21;is declared but not used, likeage = 21in Python; Java allows that.int age = 21;ناسێنراوە بەڵام بەکار نەهاتووە، وەکage = 21لە Pythonـدا؛ Java ڕێگەی پێ دەدات.- Lineدێڕی 41-43
In one
main,ialready exists, so theforloop uses a new namek(block scope).لە یەک
mainـداiپێشتر هەیە، بۆیە ناوێکی نوێ بەکار دەهێنێت،k().- Lineدێڕی 55-57
A
private staticmethod: only this class can call it.private static: تەنها ئەم دەتوانێت بانگی بکات.
Python and Java side by sidePython و Java لە تەنیشت یەکەوە
Here is the lesson in three tables: how the code looks (below), how fast it runs and what each language is used for (the next two screens). How each one runs a program is in How the two languages run today.
ئەمە وانەکەیە لە سێ خشتەدا: کۆدەکە چۆن دەردەکەوێت (خوارەوە)، چەند خێرایە و هەر زمانێک بۆ چی بەکار دێت (دوو شاشەی داهاتوو). هەر یەکەیان چۆن جێبەجێ دەکات لە دوو زمانەکە ئەمڕۆ چۆن کار دەکەندایە.
| Topicبابەت | Python | Java |
|---|---|---|
| Typesجۆرەکان | : no declarations; checked while running: بێ ناساندن؛ لە کاتی جێبەجێکردندا دەپشکنرێت | : every variable declared with a type; checked by the compiler: هەموو بە جۆرەکەیەوە دەناسێنرێت؛ دەیپشکنێت |
| A variable | age = 21 | int age = 21; |
| Blocks | : and indentation: و | braces { }کەوانەی { } |
| End of a statementکۆتایی | end of the lineکۆتایی دێڕەکە | ;; |
| Where code livesکۆد لە کوێ دەژی | anywhere in a .py fileلە هەر شوێنێکی .py | inside a class; the program starts in mainلە ناو ؛ لە main دەست پێ دەکات |
| Namesناوەکان | snake_case for variables and functionssnake_case بۆ و | camelCase for variables and methods, PascalCase for classescamelCase بۆ و ، PascalCase بۆ |
| True / falseڕاست / هەڵە | True, False | true, false |
| Logicلۆجیک | and, or, not | &&, ||, ! |
| Counting loop ژماردن | for i in range(1, 11): | for (int i = 1; i <= 10; i++) { |
| Collectionsکۆکراوەکان | list: grows, may mix typeslist: گەورە دەبێت، دەتوانێت جۆرەکان تێکەڵ بکات | array: fixed length, one type; ArrayList grows: درێژیی جێگیر، یەک جۆر؛ ArrayList گەورە دەبێت |
| Key and value pairsجووتی و | dict: {"Aram": 21} | HashMap<String, Integer> with put() and get()HashMap<String, Integer> بە put() و get() |
| Stacks and queues و | a list as a stack, collections.deque as a queuelist وەک ستاک، collections.deque وەک کیوو | ArrayDeque for bothArrayDeque بۆ هەردووکیان |
| Length of programsدرێژیی | fewer lines (Total example: 4)دێڕی کەمتر (نموونەی Total: 4) | more lines (Total example: 9)دێڕی زیاتر (نموونەی Total: 9) |
Python and Java side by sidePython و Java لە تەنیشت یەکەوە
How fast?چەند خێرا؟
| Topicبابەت | Python | Java |
|---|---|---|
| Plain loops and arithmetic و ژماردنی ساکار | usually slowerزۆرجار خاوترە | usually much faster, thanks to the JITزۆرجار زۆر خێراترە، بە هۆی |
| Heavy number workکاری قورسی ژمارە | hand it to libraries written in C, such as NumPy, which run at near-C speedبیدە بە کە بە C نووسراوە، وەک NumPy، کە نزیکەی بە خێرایی C کار دەکات | written directly in Javaڕاستەوخۆ بە Java دەنووسرێت |
| Programs that wait (for a user, a network, a disk) کە چاوەڕێ دەکات (بۆ ، ، دیسک) | fast enoughبە ئەندازەی پێویست خێرایە | fast enoughبە ئەندازەی پێویست خێرایە |
| Time to write the programکاتی نووسینی | usually shorterزۆرجار کورتترە | usually longerزۆرجار درێژترە |
Python and Java side by sidePython و Java لە تەنیشت یەکەوە
What each language is used forهەر زمانێک بۆ چی بەکار دێت
| Topicبابەت | Python | Java |
|---|---|---|
| Typical usesبەکارهێنانە باوەکان | data science, machine learning and AI, scripts and automation, web back ends, science, teachingزانستی ، و ، و ، وێب، زانست، فێرکردن | large business and bank systems, server back ends, Android apps, big-data tools, desktop programsسیستەمی گەورەی کار و بانک، ، Android، ئامرازەکانی گەورە، سەرمێز |
| Mobile apps مۆبایل | not the usual choiceهەڵبژاردنی ئاسایی نییە | still supported on Android, but Android has been Kotlin-first since 2019هێشتا لە Androidـدا پشتگیری دەکرێت، بەڵام Android لە 2019ـەوە سەرەتا Kotlinـە |
Common mistakesهەڵە باوەکان
Most of these come from the course report or from Python habits that do not work in Java. Each message is real javac or java output.
زۆربەی ئەمانە لە ڕاپۆرتی کۆرسەکەوە یان لە خووەکانی Pythonـەوە دێن کە لە Javaـدا کار ناکەن. هەر پەیامێک ڕاستەقینەی javac یان javaـە.
| Mistakeهەڵە | What happensچی ڕوودەدات | Fixچارەسەر |
|---|---|---|
System.out.printIn(...) | error: cannot find symbol | println — small Lprintln — lـی بچووک |
System.out.println("Hi") | error: ';' expected | End every statement with ;هەموو بە ; کۆتایی پێ بهێنە |
boolean temp = 30; | incompatible types: int cannot be converted to boolean | int temp = 30; |
new string[2] | cannot find symbol … class string | new String[2] |
boolean isRaining = True; | cannot find symbol … variable True | true |
public class Hello saved as Main.javapublic class Hello لە Main.javaـدا هەڵگیراوە | class Hello is public, should be declared in a file named Hello.java | Rename the file or the class یان ناو بنێرەوە |
An if without braces, two indented linesif بێ کەوانە، دوو دێڕی بەرەوناوچوو | Only the first line belongs to the ifتەنها دێڕی یەکەم سەر بە ifـە | Always use { }هەمیشە { } بەکار بهێنە |
if (a == b) with two Stringsif (a == b) بۆ دوو String | Compares objects, not text: can be false for equal text بەراورد دەکات، نەک دەق: بۆ دەقی یەکسانیش دەتوانێت false بێت | a.equals(b) |
names[names.length] | ArrayIndexOutOfBoundsException | The last index is length - 1دوا length - 1ـە |
elif in Javaelif لە Javaـدا | Not a Java wordوشەیەکی Java نییە | else if |
Two of these deserve a closer look. Without braces, the indentation lies: only the first line is inside the if.
دووان لەمانە شایەنی سەیرکردنی نزیکترن. بەبێ کەوانە، درۆ دەکات: تەنها دێڕی یەکەم لە ناو ifـدایە.
int x = -5;
if (x > 0)
System.out.println("positive");
System.out.println("checked");checked
And == on two Strings asks “is this the same object?”, not “is this the same text?”. Use .equals() for text:
هەروەها == لەسەر دوو String دەپرسێت «ئایا ئەمە هەمان ە؟»، نەک «ئایا هەمان دەقە؟». بۆ دەق .equals() بەکار بهێنە:
String a = "loop";
String b = new String("loop");
System.out.println(a == b); // are they the same object?
System.out.println(a.equals(b)); // do they hold the same text?false true
Other tracks: MATLAB today, perhaps Java and C# laterڕێڕەوەکانی تر: MATLAB ئەمڕۆ، لەوانەیە Java و C# دواتر
This course is built so that other languages can join it. The shared chapters (what programming is, algorithms and flowcharts) teach ideas without any language's code, and each technology track teaches the same ideas through one language. Today there are two tracks: Python, which you have just finished, and MATLAB, the “matrix laboratory” that engineers and scientists use for calculations and plots. A Java or C# track may come later with the same chapters; in a Java track this lesson would become “Java next to Python”.
ئەم کۆرسە وا دروست کراوە کە زمانی تر بتوانن بێنە ناوی. بەشە هاوبەشەکان ( چییە، و ) بیرۆکەکان بەبێ کۆدی هیچ زمانێک فێر دەکەن و هەر ڕێڕەوێکی تەکنەلۆجی هەمان بیرۆکەکان لە ڕێگەی یەک زمانەوە فێر دەکات. ئەمڕۆ دوو ڕێڕەو هەیە: Python (ئەوەی ئێستا تەواوت کرد) و MATLAB، «تاقیگەی »، کە ئەندازیاران و زانایان بۆ ژماردن و بەکاری دەهێنن. لەوانەیە دواتر ڕێڕەوێکی Java یان C# بە هەمان بەشەکان بێت؛ لە ڕێڕەوی Javaـدا ئەم وانەیە دەبێت بە «Java لە تەنیشت Pythonـەوە».
C# (said “C sharp”) is Microsoft's language for the .NET platform. It belongs to the same family as Java: braces, ;, static types and classes. Once you can read the Java in this lesson, C# will look familiar:
C# (دەخوێنرێتەوە «سی شارپ») زمانی Microsoftـە بۆ .NET. سەر بە هەمان خێزانی Javaـیە: کەوانە، ;، جۆری جێگیر و . کاتێک بتوانیت Javaـی ئەم وانەیە بخوێنیتەوە، C# ناسراو دەردەکەوێت:
| Languageزمان | Print one lineچاپکردنی یەک دێڕ |
|---|---|
| Python | print("Hello, World") |
| Java | System.out.println("Hello, World"); |
| C# | Console.WriteLine("Hello, World"); |
Exercisesڕاهێنانەکان
These lines are typed as in the course report. javac reports three errors. Explain each one and write the corrected lines.
ئەم دێڕانە وەک ڕاپۆرتی کۆرسەکە نووسراون. javac سێ ڕاپۆرت دەکات. هەر یەکێکیان ڕوون بکەرەوە و دێڕە ڕاستکراوەکان بنووسە.
class Main {
public static void main(String[] args) {
int age = 21;
System.out.printIn("Hello World");
boolean temp = 30;
String[] country = new string[2];
}
}Show answerوەڵامەکە پیشان بدە
Main.java:4: error: cannot find symbol
System.out.printIn("Hello World");
^
symbol: method printIn(String)
location: variable out of type PrintStream
Main.java:5: error: incompatible types: int cannot be converted to boolean
boolean temp = 30;
^
Main.java:6: error: cannot find symbol
String[] country = new string[2];
^
symbol: class string
location: class Main
3 errors- Line 4:
printIndoes not exist —println. - Line 5: a temperature is a whole number —
int temp = 30;. - Line 6: Java is case-sensitive —
new String[2].
- دێڕی 4:
printInبوونی نییە —println. - دێڕی 5: پلەی گەرمی —
int temp = 30;. - دێڕی 6: Java —
new String[2].
System.out.println("Hello World");
int temp = 30;
String[] country = new String[2];Translate this Python loop into a Java for loop: for i in range(10, 0, -2): print(i).
ئەم Python وەربگێڕە بۆ لە Javaـدا: for i in range(10, 0, -2): print(i).
Hintئاماژە
Write the start, the condition and the step of the for header.
دەستپێک، و هەنگاوی سەرەی for بنووسە.
Show answerوەڵامەکە پیشان بدە
for (int i = 10; i > 0; i -= 2) {
System.out.println(i);
}10 8 6 4 2
Start at 10, keep going while i > 0 (Python's stop value 0 is not included), and step with i -= 2.
لە 10ـەوە دەست پێ بکە، تا i > 0 بەردەوام بە ( وەستانی Python، واتە 0، ناگیرێتەوە) و بە i -= 2 هەنگاو بنێ.
Write an if-else-if ladder in Java that prints positive, negative or zero for int number = -7;. Then write the same in Python.
پەیژەیەکی if-else-if لە Javaـدا بنووسە کە بۆ int number = -7; positive، negative یان zero چاپ بکات. پاشان هەمان شت بە Python بنووسە.
Show answerوەڵامەکە پیشان بدە
int number = -7;
if (number > 0) {
System.out.println("positive");
} else if (number < 0) {
System.out.println("negative");
} else {
System.out.println("zero");
}negative
number = -7
if number > 0:
print("positive")
elif number < 0:
print("negative")
else:
print("zero")negative
What does this print? int n = 3; do { System.out.println("n is " + n); n++; } while (n < 5);
ئەمە چی چاپ دەکات؟ int n = 3; do { System.out.println("n is " + n); n++; } while (n < 5);
Show answerوەڵامەکە پیشان بدە
int n = 3;
do {
System.out.println("n is " + n);
n++;
} while (n < 5);n is 3 n is 4
The body runs with 3 and with 4. After the second round n is 5, 5 < 5 is false and the loop stops. With int n = 10; it would still print once (see the do-while screen).
جەستەکە بە 3 و بە 4 جێبەجێ دەبێت. دوای جاری دووەم n دەبێت بە 5، 5 < 5 هەڵەیە و دەوەستێت. بە int n = 10; هێشتا یەک جار چاپ دەکات (بڕوانە شاشەی do-while).
For each line say whether it is Python, Java or neither: (a) int[] a = {1, 2}; (b) a = [1, 2] (c) if x > 0 { (d) for (String s : names) { (e) elif (x > 0):
بۆ هەر دێڕێک بڵێ Pythonـە، Javaـیە یان هیچیان: (أ) int[] a = {1, 2}; (ب) a = [1, 2] (ج) if x > 0 { (د) for (String s : names) { (هـ) elif (x > 0):
Show answerوەڵامەکە پیشان بدە
- (a) Java: an array of two
intvalues. - (b) Python: a list.
- (c) Neither: Java needs
if (x > 0) {, Python needsif x > 0:. - (d) Java: an enhanced
forover an array or list of Strings. - (e) Python (legal, though the parentheses are not needed).
- (أ) Java: لە دوو
int. - (ب) Python: ئەمە .
- (ج) هیچیان: Java پێویستی بە
if (x > 0) {ـە، Python پێویستی بەif x > 0:ـە. - (د) Java:
forـی پەرەپێدراو بە ناو یان Stringـدا. - (هـ) Python (دروستە، هەرچەندە کەوانەکان پێویست نین).
Summaryپوختە
- A Java program lives in a class; it starts in
public static void main(String[] args);System.out.printlnprints a line. - Java declares types (
int age = 21;), ends statements with;and marks blocks with{ }; Python uses neither declarations nor braces. javaccompiles to bytecode in.classfiles and finds compile-time errors;javaruns the bytecode on the JVM. Python compiles to bytecode by itself on every run.- Java has
else if, a three-partfor, a for-each loop anddo … while; Python haselif,for … in range()andwhile True+break. - A Java array has a fixed length and one type;
ArrayListgrows like a Python list. - The course report's Java was corrected:
println, semicolons,int temp,String, a declaredisSnowing; Android is Kotlin-first since 2019.
- Java لە ناو دەژی؛ لە
public static void main(String[] args)دەست پێ دەکات؛System.out.printlnدێڕێک چاپ دەکات. - Java جۆرەکان دەناسێنێت (
int age = 21;)، بە;کۆتایی پێ دەهێنێت و بە{ }دیاری دەکات؛ نە ناساندن و نە کەوانە بەکار دەهێنێت. javacبۆ لە.classـدا کۆمپایل دەکات و دەدۆزێتەوە؛javaلەسەر JVM جێبەجێ دەکات. لە هەموو جێبەجێکردنێکدا خۆی دەیکات بە .- Java
else if،forـی سێ بەشی، for-each وdo … whileـی هەیە؛elif،for … in range()وwhile True+breakـی هەیە. - Java درێژییەکی جێگیر و یەک جۆری هەیە؛
ArrayListوەک گەورە دەبێت. - Javaـی ڕاپۆرتی کۆرسەکە ڕاست کرایەوە:
println، ،int temp،String،isSnowingـی ناسێنراو؛ Android لە 2019ـەوە سەرەتا Kotlinـە.
What is the difference between javac and java?جیاوازیی نێوان javac و java چییە؟
javac compiles .java source files into .class bytecode; java starts the JVM, which runs that bytecode.
javac سەرچاوەی .java دەکات بە .class؛ java JVM دەست پێ دەکات کە ئەو جێبەجێ دەکات.
Why is String written with a capital S?بۆچی String بە Sـی گەورە دەنووسرێت؟
It is a class, not a primitive type, and Java is case-sensitive: string is an unknown name.
، نەک . هەروەها Java : string ناوێکی نەناسراوە.
How do you write a do-while loop in Python?چۆن خولێکی do-while لە Pythonـدا دەنووسیت؟
With while True: and, at the end of the body, if not condition: break.
بە while True: و لە کۆتایی جەستەکەدا if not condition: break.
When does each language find a misspelt variable name?هەر زمانێک کەی ناوی گۆڕاوێکی هەڵە نووسراو دەدۆزێتەوە؟
Java: at compile time, before anything runs. Python: only when the line with the name runs.
Java: لە کاتی کۆمپایلکردندا، پێش ئەوەی هیچ جێبەجێ بێت. : تەنها کاتێک ئەو دێڕەی ناوەکەی تێدایە جێبەجێ دەبێت.
You finished the Python trackڕێڕەوی Pythonـت تەواو کرد
Well done! This is the last lesson of the Python track. Here is what you learned, chapter by chapter; each chapter name opens its first lesson.
دەستخۆش! ئەمە دوا وانەی ڕێڕەوی Pythonـە. ئەمە ئەوەیە کە فێری بوویت، بەش بە بەش؛ ناوی هەر بەشێک یەکەم وانەکەی دەکاتەوە.
| Chapterبەش | What you learnedفێری چی بوویت |
|---|---|
| Programs and programming languagesبەشی بەرنامەکان و زمانەکانی بەرنامەسازی | what a program is, high- and low-level languages, compilers and interpreters چییە، و ، و |
| Solving problems step by stepبەشی چارەسەرکردنی کێشە هەنگاو بە هەنگاو | algorithms, pseudocode, flowcharts, trace tables, planning a project، ، ، ، پلاندانانی |
| Getting started with Pythonبەشی دەستپێکردن لەگەڵ Python | installing Python, the shell and scripts, print(), comments, reading error messagesدامەزراندنی Python، و ، print()، ، خوێندنەوەی پەیامی |
| Variables, data and expressionsبەشی گۆڕاوەکان، دراوە و دەربڕینەکان | variables and types, operators, input and output, f-strings و جۆرەکان، ، و ، |
| Making decisionsبەشی بڕیاردان | if, elif, else, and, or, not, nested ifif، elif، else، and، or، not، |
| Stringsبەشی زنجیرەنووسەکان | indexing, slicing, string methods, comparing text، ، ، بەراوردکردنی دەق |
| Repeating code: loopsبەشی دووبارەکردنەوەی کۆد: خولەکان | while, for and range(), break and continue, nested loops and patternswhile، for و range()، break و continue، و شێوەکان |
| Functionsبەشی کردارەکان | def and return, arguments, scope, modules, recursiondef و return، ، ، ، |
| Listsبەشی لیستەکان | creating, changing and copying lists, sorting, two-dimensional listsدروستکردن، گۆڕین و کۆپیکردنی ، ، |
| More data and safer programsبەشی دراوەی زیاتر و بەرنامەکانی سەلامەتتر | tuples, sets, dictionaries, try and except, reading and writing files, pickle، ، ، try و except، خوێندنەوە و نووسینی ، pickle |
| Data structures and algorithmsبەشی پێکهاتەکانی دراوە و ئەلگۆریزمەکان | stacks, queues and deques, bubble sort and insertion sort, binary search and hashing، و ، و ، و هاشکردن |
| Projects and next stepsبەشی پڕۆژەکان و هەنگاوەکانی داهاتوو | the binary-to-decimal converter, the Mixed Letters game, Python next to Javaگۆڕەری بۆ ، یاریی پیتە تێکەڵەکان، Python لە تەنیشت Javaـەوە |
What to learn nextدواتر فێری چی ببیت
Three CêIT courses build directly on what you learned here:
سێ کۆرسی CêIT ڕاستەوخۆ لەسەر ئەوەی لێرە فێری بوویت بنیات دەنێن:
- Artificial Intelligence — use Python to make programs that search, play games and reason. Start with What is AI?, then The Python you need for the AI labs.
- Web Programming — build websites that talk to a database, with PHP and MySQL. Start with The Internet and the Web, then Introduction to PHP.
- Mobile App Development — make apps for phones: how apps and app stores work, then Flutter with the Dart language. Start with Mobile apps and app stores and Native, web, hybrid and cross-platform apps.
- — بەکار بهێنە بۆ دروستکردنی کە دەگەڕێت، یاری دەکات و بیر دەکاتەوە. بە ژیریی دەستکرد چییە؟ دەست پێ بکە، پاشان ئەو Pythonـەی بۆ تاقیگەکانی ژیریی دەستکرد پێویستە.
- وێب — دروست بکە کە لەگەڵ قسە دەکات، بە PHP و MySQL. بە ئینتەرنێت و وێب دەست پێ بکە، پاشان سەرەتایەک بۆ PHP.
- پەرەپێدانی مۆبایل — بۆ مۆبایل دروست بکە: و فرۆشگاکانی چۆن کار دەکەن، پاشان Flutter بە زمانی Dart. بە ئەپەکانی مۆبایل و فرۆشگاکانی ئەپ و ئەپەکانی ڕەسەن، وێب، تێکەڵ و فرەسەکۆ دەست پێ بکە.
Whichever you choose, the habits from this course go with you: plan before you code, test every branch, read error messages calmly, and write code that people can read.
هەر کامێکیان هەڵبژێریت، خووەکانی ئەم کۆرسە لەگەڵت دێن: پێش کۆدنووسین پلان دابنێ، هەموو لقێک تاقی بکەرەوە، پەیامی بە ئارامی بخوێنەرەوە و کۆدێک بنووسە کە خەڵک بتوانێت بیخوێنێتەوە.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
The same 'print some text' program in Java and in Python: Java needs a class and a main method, Python one line.
هەمان بە و بە ، کە دەقێک پیشان دەدات: پێویستی بە و main هەیە، تەنها یەک دێڕ.
Watch all 7:50: printing (Java 0:00, Python 2:00), storing data (2:30, 3:40) and loops (4:30, 6:00). It also uses a Java HashMap, which this lesson does not teach.
هەموو ڤیدیۆکە (7:50) سەیر بکە: پیشاندانی دەق ( 0:00، 2:00)، هەڵگرتنی (2:30، 3:40) و (4:30، 6:00). هەروەها HashMap لە بەکاردەهێنێت، کە ئەم وانەیە فێری ناکات.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Related lecturesوانە پەیوەندیدارەکان
- Programming Languagesزمانەکانی بەرنامەسازی
- How Code Becomes a Running Program: Compilers and Interpretersچۆن کۆد دەبێت بە بەرنامەیەک کە کار دەکات: کۆمپایلەرەکان و ئینتەرپرێتەرەکان
- Variables and Data Typesگۆڕاوەکان و جۆرەکانی دراوە
- Variable Scope: Local and Globalمەودای گۆڕاوەکان: ناوخۆیی و گشتی
- Project: A Word-Scramble Gameپڕۆژیلە: یاریی پیتە تێکەڵەکان
- Native, Web, Hybrid and Cross-Platform Appsئەپی ڕەسەن، بەرنامەی وێب، ئەپی تێکەڵ و ئەپەکانی فرەسەکۆ
- Welcome to MATLAB (and Octave)بەخێربێیت بۆ MATLAB (و Octave)
- Stacks: Last In, First Outستاکەکان: دوایین هاتوو، یەکەم دەرچوو
- Queues and Deques: First In, First Outکیووەکان و کیووە دووسەرەکان: یەکەم هاتوو، یەکەم دەرچوو
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- The Java Tutorials: A closer look at the “Hello World!” application (written for JDK 8)
- Java SE 21 tools reference: the javac command
- Java SE 21 tools reference: the java command (source-file mode, -Xint, -Xmixed)
- The Java Language Specification, Java SE 21: Chapter 4, Types (primitive types)
- The Java Language Specification, Java SE 21: Chapter 14, Blocks and statements (do, for)
- Java SE 21 API: java.util.ArrayList
- JEP 330: Launch Single-File Source-Code Programs
- JEP 512: Compact Source Files and Instance Main Methods
- Android Developers: Android's Kotlin-first approach
- Python glossary: bytecode, interpreted
- What's new in Python 3.13 (experimental JIT compiler)
- Python programming FAQ: My program is too slow. How do I speed it up?
- Python design FAQ: indentation for grouping; compiling Python
- NumPy user guide: What is NumPy? (why vectorized code is fast)
- Microsoft Learn: A tour of C#
- Microsoft Learn: A tour of C# — file-based apps (C# 14 and .NET 10)
- Java SE 21 API: java.util.ArrayDeque and java.util.HashMap
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.