انتقل إلى المحتوى
أمين أبوزيد
JNR-001

Java الحديثة

  • 8 درس
  • 87 دقيقة قراءة
  • 4 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • وتيرة الإصدارات
  • إصدارات LTS
  • السجلات (records)
  • الفئات المختومة (sealed classes)
  • تعبيرات switch
  • الكتل النصية (text blocks)
  • مطابقة الأنماط (pattern matching)

تعلّم أولًا

87 دقيقة قراءة · 4 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Explain Java's six-month release cadence, what LTS means, and how to choose a version for production.
  2. Replace boilerplate data classes with records, including compact constructors that validate.
  3. Model closed hierarchies with sealed interfaces and handle them exhaustively with pattern-matching switch.
  4. Use text blocks, instanceof patterns, and record patterns where they improve readability.

الدروس

  1. 01نموذج إصدارات Java وإصدارات LTS10 د قراءة
  2. 02السجلات (Records)11 د قراءة
  3. 03الفئات والواجهات المختومة (Sealed)11 د قراءة
  4. 04تعبيرات switch ومطابقة الأنماط11 د قراءة
  5. 05أنماط السجلات (Record patterns)11 د قراءة
  6. 06الكتل النصية ومكاسب صغيرة أخرى11 د قراءة
  7. 07ما الذي تغيّر منذ آخر LTS (يُحدَّث مع كل إصدار)11 د قراءة
  8. 08عرض عملي: تحديث فئة Java قديمة خطوة بخطوة11 د قراءة

المختبر والتمارين

Lab

JNR-001-LAB1 · Shop Core v1

Outcomes: 2–4 · Time: ~2.5 h Model Product, Money, and CartLine as records with validation in compact constructors. Model Discount as a sealed hierarchy (percentage, fixed amount, buy-X-get-Y) and price a cart line with an exhaustive switch. Acceptance: no setters anywhere; adding a new discount type causes a compile error until the switch handles it; no default branch.

Exercises (assessment: refactor a legacy POJO-heavy module and justify each change, plus seven exercises)

IDOutcomeTask
JNR-001-EX011Explain in three sentences the six-month cadence, what LTS means, and which version you would choose for a new production service and why.
JNR-001-EX022Rewrite a Customer class (name, email, getters, setters, equals, hashCode, toString) as a record. Reject a blank name.
JNR-001-EX032A record has a List<String> tags component. Show how a caller can break it and fix it.
JNR-001-EX043Declare a sealed interface PaymentMethod with Card, Cash, Voucher. Write a switch returning a fee for each.
JNR-001-EX053Add Transfer to PaymentMethod without touching the switch. What happens? Why is default harmful here?
JNR-001-EX064Rewrite a chain of if (o instanceof X) { X x = (X) o; ... } with pattern matching, and a concatenated JSON string with a text block.
JNR-001-EX074Use a record pattern to print Money(cents, currency) from an Object using instanceof.
أظهر الحلول
  • EX01 Releases every six months (March, September); LTS gets years of support; pick the current LTS, because it is patched for a long time (verify the version).
  • EX02 record Customer(String name, String email) { Customer { if (name == null || name.isBlank()) throw new IllegalArgumentException("name"); } }.
  • EX03 The caller keeps the original list and changes it. Fix: tags = List.copyOf(tags); in the compact constructor.
  • EX04 sealed interface PaymentMethod permits Card, Cash, Voucher {} (records); switch (m) { case Card c -> ...; case Cash c -> ...; case Voucher v -> ...; }.
  • EX05 The code fails to compile: the switch is not exhaustive. With default, it would compile and silently use the wrong fee.
  • EX06 if (o instanceof Product p) { ... p.name() ... }; a text block with three quotes and no escaped quotes.
  • EX07 if (o instanceof Money(long cents, String currency)) { System.out.println(cents + " " + currency); }.
  • Outcome 1 is also covered by quiz Q01–Q03; the lab covers outcomes 2–4.

الاختبار

Outcomes: O1 release model and LTS · O2 records · O3 sealed types and switch · O4 patterns, text blocks.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1How often does a new Java version appear?O1
  2. 2What does LTS mean?O1
  3. 3Preview features in production code are:O1
  4. 4A record automatically gives you:O2
  5. 5Where do you validate record values?O2
  6. 6A record has a List component. To keep it safe you:O2
  7. 7What does sealed ... permits do?O3
  8. 8A switch over a sealed type with a missing case:O3
  9. 9Why avoid default in that switch?O3
  10. 10if (o instanceof String s) does:O4

الجزء العملي

Seven exercises JNR-001-EX01 to EX07, the legacy-refactoring task, and the lab. Pass: at least 8 of 10 quiz answers, six exercises correct, and a lab that fails to compile when a discount type is unhandled.

أخطاء شائعة

Records with mutable components (lists not copied); default branches that defeat exhaustiveness; using preview features in production code.

التقييم

Refactor a legacy POJO-heavy module to modern Java and justify each change.

الصلة بالمسار الوظيفي

Modern codebases and interviews expect these features; sealed types reappear in event modeling (SRB-002).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.