🧩 Java Program Structure
- A program is made up of one or more classes.
- A class contains one or more methods.
- A method contains program statements.
- A Java application always contains a method called main.
يتكون البرنامج من كلاس واحد أو أكثر. الكلاس يحتوي على دوال (methods). الدوال تحتوي على أوامر (statements). أي تطبيق جافا يحتوي على دالة اسمها main.
📛 Class Name
Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter.
كل برنامج جافا يحتوي كلاس واحد على الأقل. اسم الكلاس يبدأ بحرف كبير حسب convention. مثال: Welcome.
▶️ main Method
In order to run a class, the class must contain one method named main. The program begins execution from the main method.
لكي يتم تشغيل الكلاس، يجب أن يحتوي على دالة اسمها main. البرنامج يبدأ التنفيذ من أول سطر داخل main.
📁 Filename rule
A public class must be placed in a file that has a name of the form ClassName.java. So class Welcome is stored in file Welcome.java.
الكلاس المعرّف بـ public يجب أن يكون في ملف بنفس اسم الكلاس + امتداد .java. مثلاً Welcome.java.
📐 Naming Conventions
- Class names: Capitalize first letter of each word. Example:
ComputeArea. - Variable and method names: Use lowercase for first word, then capitalize subsequent words (camelCase). Example:
radius,computeArea. - Constants: All uppercase, words separated by underscore. Example:
PI,MAX_VALUE.
أسماء الكلاسات: تبدأ بحرف كبير لكل كلمة. المتغيرات والدوال: camelCase (أول كلمة صغيرة). الثوابت: كلها كبيرة مع underscores.
📝 Statement
A statement represents an action or a sequence of actions. Every statement in Java ends with a semicolon (;).
الأمر (statement) هو إجراء أو مجموعة إجراءات. كل أمر في جافا ينتهي بفاصلة منقوطة ;
🔑 Common Reserved Words (Keywords)
Reserved words have a specific meaning to the compiler and cannot be used for other purposes.
... and many more (over 50 total).
الكلمات المحجوزة (keywords) لها معنى محدد للمترجم ولا يمكن استخدامها كأسماء متغيرات أو كلاسات. هذه أشهرها.
💬 Comments
Comments are inline documentation. Compiler ignores them. Three forms:
التعليقات تستخدم لتوثيق الكود. المترجم يتجاهلها. ثلاثة أنواع: // سطر واحد، /* */ عدة أسطر، /** */ javadoc.
🏷️ Identifiers
An identifier is any name you give to program elements.
Rules:
- Must begin with a letter, underscore (_), or dollar sign ($).
- Cannot start with a digit.
- After first character, digits (0–9) can be used.
- Case-sensitive → Age and age are different.
- Cannot be a Java keyword.
- No spaces or special characters like @, #, %.
Valid identifiers: hello, _Name, valid_$1, $23
Invalid: 123Here, exam-, class, student ID, not@valid
المعرف (identifier) هو اسم تختاره لعناصر البرنامج. يبدأ بحرف أو _ أو $، لا يبدأ برقم، حساس لحالة الأحرف، لا يمكن أن يكون كلمة محجوزة.
📦 Variables
A variable must be declared by specifying its name and type.
Always choose meaningful and descriptive variable names.
المتغير يُصرّح بتحديد اسمه ونوعه. يمكن التصريح عن عدة متغيرات من نفس النوع في سطر واحد.
🔒 Constants (final)
A constant holds the same value during its entire existence. Declared using final keyword. By convention, constants are ALL_UPPER_CASE.
الثابت (constant) يحتفظ بنفس القيمة طوال عمره. يُصرّح باستخدام final. التسمية كلها uppercase مع underscores.
✍️ Assignment Statement
Changes the value of a variable using the = operator.
You can only assign a value of the same declared type.
جملة الإسناد تغير قيمة المتغير باستخدام =. يجب أن تكون القيمة من نفس النوع المصرّح به.
🖨️ Output Methods
System.out.println() prints and moves to next line. System.out.print() prints without advancing line.
Output: Three... Two... One... Zero... Liftoff!
Houston, we have a problem.
println تطبع وتنتقل لسطر جديد. print تطبع وتبقى في نفس السطر.
🔣 Escape Sequences
Start with backslash \ to represent special characters.
| Escape | Meaning |
|---|---|
| \n | newline |
| \t | tab |
| \" | double quote |
| \\ | backslash |
تسلسلات الهروب تبدأ بـ \ وتمثل محارف خاصة: \n سطر جديد، \t tab، إلخ.
➕ Concatenation vs Addition
+ can be used for string concatenation or numeric addition. Use parentheses for addition inside strings.
+ يستخدم للجمع العددي أو لضم النصوص. إذا أردت الجمع مع نص، استخدم أقواس.
📊 Two Groups
- Primitive data types (8 types)
- Reference data types (objects, arrays, etc.)
تنقسم أنواع البيانات في جافا إلى: أنواع بدائية (primitive) و 8 أنواع، وأنواع مرجعية (reference) مثل الكائنات.
🔢 8 Primitive Types
| Type | Size | Example |
|---|---|---|
| byte | 1 byte | 127 |
| short | 2 bytes | 32767 |
| int | 4 bytes | 2147483647 |
| long | 8 bytes | 9223372036854775807L |
| float | 4 bytes | 3.14f |
| double | 8 bytes | 3.14159 |
| char | 2 bytes | 'A' |
| boolean | 1 bit (JVM dependent) | true/false |
الأنواع البدائية الثمانية: byte, short, int, long (للأعداد الصحيحة)، float, double (للأعداد العشرية)، char (حرف)، boolean (صحيح/خطأ).
📌 Integer & Floating Details
Integer types: byte, short, int, long – hold whole numbers only.
Floating-point: float (7 decimal digits), double (15 decimal digits).
Floating-point can store integer values, but integer types cannot store floating-point values.
الأنواع الصحيحة تخزن أعداداً بدون كسور. الأنواع العشرية تخزن كسوراً. float دقة أقل، double دقة أعلى.
✅ boolean
Can have two values: true or false. Used in conditions.
النوع boolean له قيمتان: true أو false. يستخدم في الشروط.
🔤 char
Stores a single 16-bit Unicode character. Use single quotes.
char يخزن حرفاً واحداً بترميز Unicode. يوضع بين علامتي اقتباس مفردة.
🔮 Implicit type with var
Use var to let Java infer the type from the initial value.
var يخبر المترجم أن يستنتج النوع من القيمة المعطاة. متاح من Java 10.
🧮 Arithmetic
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 5 + 2 = 7 |
| - | Subtraction | 5 - 2 = 3 |
| * | Multiplication | 5 * 2 = 10 |
| / | Division | 5 / 2 = 2 (int) / 5.0/2 = 2.5 |
| % | Remainder (modulo) | 5 % 2 = 1 |
العمليات: + جمع، - طرح، * ضرب، / قسمة (تحذير: قسمة عددين صحيحين تعطي عدداً صحيحاً)، % باقي القسمة.
⚡ Combined Assignment Operators
| Operator | Example | Equivalent |
|---|---|---|
| += | x += 5 | x = x + 5 |
| -= | x -= 5 | x = x - 5 |
| *= | x *= 5 | x = x * 5 |
| /= | x /= 5 | x = x / 5 |
| %= | x %= 5 | x = x % 5 |
عوامل مثل += تختصر عملية الجمع مع الإسناد. مثال: x += 5 تعني x = x + 5.
📐 Math class
Math.sqrt(x)– square rootMath.pow(a,b)– a to the power bMath.abs(x)– absolute valueMath.max(a,b)– maximumMath.min(a,b)– minimumMath.random()– random double 0.0–1.0
كلاس Math يوفر دوال رياضية: جذر تربيعي، أس، قيمة مطلقة، أكبر، أصغر، رقم عشوائي.
🔍 Variable Tracing
Step by step: memory allocation → assign 20 → compute area → print.
يتم تخصيص مساحة للمتغيرات radius و area، ثم تعطى radius القيمة 20، ثم تحسب area، ثم تطبع.
📦 Packages
A package is a way to categorize classes and interfaces. Package name comes first in the file. Import statements tell the compiler where to find classes.
Built-in packages: java.lang (auto-imported), java.io, java.util, etc.
الحزم (packages) تنظم الكلاسات. import تخبر المترجم بمكان الكلاس. java.lang مستوردة تلقائياً.
⌨️ Scanner Class
To read input from keyboard, use Scanner from java.util.
كلاس Scanner يستخدم لقراءة المدخلات من لوحة المفاتيح. nextLine() لقراءة نص، nextInt() لعدد صحيح، nextDouble() لعدد عشري.
🌡️ Temperature Conversion
هذا البرنامج يطلب من المستخدم درجة حرارة بالفهرنهايت ويحولها إلى سيلسيوس باستخدام المعادلة.
⚖️ BMI Example
يحسب BMI بقسمة الوزن على مربع الطول.
📋 Input/Output Demo
هذا المثال يقرأ اسم، عمر، دخل ثم يعرضهم.
📝 Practice Problems with Solutions
Which of the following are valid Java identifiers? Explain why invalid ones are wrong.
- hello
- exam-
- class
- _Name
- 123Here
- valid_$1
- student ID
- not@valid
- $23
Valid: hello, _Name, valid_$1, $23
Invalid:
exam-– contains hyphen (-), not allowedclass– reserved keyword123Here– starts with digitstudent ID– contains spacenot@valid– contains @ symbol
What is the output of the following code?
Output:
Explanation: print stays on same line, println moves to next line after printing.
What is the output of this statement?
Output:
Explanation: \n newline, \t tab. 5 * 6 is calculated first (multiplication higher precedence), then concatenated. Parentheses in (5 * 6) are optional but clarify.
Evaluate the following expressions in Java:
- 5 / 2
- 5.0 / 2
- 5 % 2
- 17 % 5
5 / 2= 2 (integer division truncates)5.0 / 2= 2.5 (floating-point division)5 % 2= 1 (remainder)17 % 5= 2 (remainder)
What are the values of x, y, and z after the following code?
x += y→ x = 10 + 5 = 15y *= z→ y = 5 * 2 = 10z %= 3→ z = 2 % 3 = 2
Final: x = 15, y = 10, z = 2
Write Java expressions for:
- Square root of 25
- 2 raised to the power 8
- Absolute value of -10
- Maximum of 15 and 20
Math.sqrt(25)→ 5.0Math.pow(2, 8)→ 256.0Math.abs(-10)→ 10Math.max(15, 20)→ 20
Trace the ComputeArea program and show the values of radius and area after each step.
Step by step:
double radius;→ radius: uninitializeddouble area;→ area: uninitializedradius = 20;→ radius = 20.0area = radius * radius * 3.14159;→ area = 20 * 20 * 3.14159 = 1256.636- Print: "The area for the circle of radius 20.0 is 1256.636"
Write a program that asks the user for two integers and prints their sum.
Write a program that converts Celsius to Fahrenheit using formula: F = (C * 9/5) + 32.
Extend the BMI calculator to also display category: Underweight (<18.5), Normal (18.5–24.9), Overweight (25–29.9), Obese (≥30).
📌 Java Structure – All Key Points
- Java program: one or more classes, each with methods, one method named
main. - Class name: uppercase start, file name = ClassName.java.
- Naming: classes PascalCase, variables/methods camelCase, constants UPPER_SNAKE.
- Reserved words: 50+ keywords (common ones listed) cannot be used as identifiers.
- Comments: //, /* */, /** */.
- Identifiers: start with letter, _, $; case-sensitive.
- Variables: declared with type; constants with
final. - Output:
System.out.print()andprintln(). - Escape sequences: \n, \t, \", \\.
- Data types: 8 primitive (byte, short, int, long, float, double, char, boolean) and reference.
- var: implicit type inference (Java 10+).
- Arithmetic: + - * / %, combined assignments (+= etc.).
- Math class: sqrt, pow, abs, max, min, random.
- Packages: organize classes;
importstatement. - Scanner: read input: nextLine(), nextInt(), nextDouble().
- Problems: 10 practice problems with solutions included.
ملخص تركيب برنامج جافا:
- برنامج جافا: كلاسات تحتوي دوال، وأحدها main.
- اسم الكلاس يبدأ بحرف كبير، الملف بنفس الاسم.
- قواعد التسمية: كلاسات PascalCase، متغيرات/دوال camelCase، ثوابت UPPER_SNAKE.
- الكلمات المحجوزة (keywords) لا يمكن استخدامها كأسماء.
- التعليقات: // سطر، /* */ عدة أسطر، /** */ javadoc.
- المعرفات: تبدأ بحرف أو _ أو $، حساسة لحالة الأحرف.
- المتغيرات: تُصرّح بنوعها. الثوابت: final.
- الإخراج: print() و println().
- تسلسلات الهروب: \n سطر جديد، \t tab، إلخ.
- الأنواع البدائية: 8 أنواع (byte, short, int, long, float, double, char, boolean).
- var: استدلال النوع من القيمة (من Java 10).
- العمليات الحسابية: + - * / %، وعوامل الإسناد المركبة.
- كلاس Math: دوال رياضية.
- الحزم: تنظيم الكلاسات، import لاستيرادها.
- Scanner: لقراءة المدخلات nextLine(), nextInt(), nextDouble().
- المسائل: 10 مسائل مع الحلول.