0% found this document useful (0 votes)
11 views86 pages

Java Programming Language Basics

Java Programming

Uploaded by

ayon24704
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views86 pages

Java Programming Language Basics

Java Programming

Uploaded by

ayon24704
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Language Fundamentals

ပတ#ဝန#&က(င# *ပင#ဆင#မ-အe0က1င#&ကiu eလ5လ16ပ7&တ85eန1က#မ91 ဆက#လက#6ပ7& Java Programming ကiu ဘယ#လiu


eရ&သ1&ရတယ#ဆiuတ85 အပiuင#&ကiu eလ5လ1သ>1&0ကပ?မယ#။ Java န8A Programming ကiu eရ&သ1&တ85 eနရ1မ91
eရ&သ1&Biuင#တ85 Class eတ>၊ Method eတ> Variable eတ> 6ပ7&eတ15 Enum eတ> Generics eတ>Diပ?တယ#။ တဖန#
Programming Logic ကiu eရ&သ1&ဖiuအတ>က# လiuအပ#တ85 Operators eတ> 6ပ7&eတ15 Statement လ8 Diပ?တယ#။ ဒ?
eတ>ကiu ဒ7အခန#&မ91 eလ5လ1သ>1&Biuင#ပ?တယ#။

Java Programming မ91 ဘ1eတ> eရ&လiuရ


တယ#၊ 6ပ7&eတ15 မမ91&eအ1င# eရ&ဖiu ဘယ#လiu
eရ&ရမယ#ဆiuတ1ကiu ဒ7eနရ1မ91 သiDiသ>1&eစ
ခ(င#ပ?တယ#။ ပ?ဝင#မည#5 အe0က1င#&အရ1eတ>
ကeတ15 eအ1က#ပ?အတiuင#& *ဖစ#ပ?တယ#။

What is A Class
Anatomy of a Class
Visibility and Accessibility
Java Data Types
Using Arrays
Operators
Statements

What is Class
Class ဆiuတ1 Programming Unit တစ#ခu*ဖစ#ပ?တယ#။ လiuအပ#တ85 Function eတ>ကiu eရ&သ1&လiuတ85အခ?မ91လ8
Class ကiu eရ&6ပ7& အ85ဒ7 Class ထ8မ91 Function eတ>ကiueရ&သ1&ရပ?မယ#။ Java Programming မ91 Function
eတ>ကiu Method လiueခLeလ5Diပ?တယ#။

တဖန# Class eတ>ဟ1 Object တစ#ခuမ91ပ?တ85 State eတ>န8A Behavior eတ>ကiu သတ#မ9တ#eပ&Biuင#ပ?တယ#။ Object
ထ8မ91 ပ?သင#5တ85 State eတ>ကiu Variable eတ>န8A သတ#မ9တ#6ပ7& Behavior eတ>ကiueတ15 Method eတ>န8A eရ&သ1&
eဖL*ပရမ91 *ဖစ#ပ?တယ#။

ဒ?5e0က1င#5 Class တစ#ခuကiu eရ&ရတ85 ရည#ရ>ယ#ခ(က#ဟ1 ၂ ခuDiမ91 *ဖစ#ပ?တယ#။ ပထမရည#ရ>ယ#ခ(က#ကeတ15 Utility


Function eတ>ကiu eရ&သ1&လiuတ85အခ?မ91လ8 Class ကiueရ&သ1&6ပ7&မ9 Function eတ>ကiu Static Method eတ> အeန
န8A eရ&သ1&ရပ?မယ#။ eန1က#ရည#ရ>ယ#ခ(က#တစ#ခuကeတ15 Object eတ>ကiu တည#eဆ1က#တ85အခ?မ91 လiuအပ#တ85
State eတ>န8A Behavior eတ>ကiu သတ#မ9တ#eပ&ဖiu Class eတ>ကiu eရ&ရမ91*ဖစ#ပ?တယ#။
Java Developer Class

Anatomy of a Class
Java Programming ကiu စ6ပ7&eရ&သ1&eတ15မယ#ဆiuရင# Class ကeန စ6ပ7& eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။ ကOန#eတ1#
တiu ဒ7eနရ1မ91 Java Class တစ#ခuကiu ဘယ#လiu eရ&သ1&ရမယ#ဆiuတ1ကiu eလ5လ1သ>1&မ91 *ဖစ#ပ?တယ#။ အရင#ဆuP&
Class တစ#ခuကiu မမ91&eအ1င# ဘယ#လiueရ&သ1&ရမလ8 ဆiuတ1ကiu eလ5လ1သ>1&6ပ7&၊ Class ထ8မ91 ဘ1eတ> eရ&လiuရ
တယ#၊ 6ပ7&eတ15 ဘယ#လiu အသuP&*ပuရတယ#ဆiueတ1ကiu eလ5လ1သ>1&ပ?မယ#။

Minimum Requirements
Class တစ#ခuကiu မမ91&eအ1င#eရ&တယ#ဆiuတ1က Compile လuပ#လiuက#တ85အခ?မ91 Error မ*ဖစ#eအ1င# eရ&Biuင#တ1ကiu
ဆiuလiuတ1 *ဖစ#ပ?တယ#။

Class တစ#ခuကiu eရ&သ1&မယ#ဆiuရင# eဖL*ပပ? အတiuင#&


eရ&သ1&Biuင#မ91 *ဖစ#ပ?တယ#။ အန8ဆuP&eအ1က#ပ?
အခ(က#အလက#eတ>ကiu eရ&သ1&ထ1&ရင# Compileလuပ#တ85
အခ?မ91 Error တက#မ91 မဟuတ#ပ?ဘR&။

class keyword
class keyword ဟ1 Class တစ#ခu*ဖစ#e0က1င#&ကiu သတ#မ9တ#eပ&eနတ85 keyword *ဖစ#ပ?တယ#။ Class တစ#ခuကiu
eရ&သ1&ရ1မ91 မ*ဖစ#မeနပ?ဝင#ရမည#5 အရ1တစ#ခu*ဖစ#ပ?တယ#။ Java ဘ1သ1ရပ#ဟ1 Case Sensitive *ဖစ#တ85
အတ>က# Lower Case eတ>*ဖစ#တ85 class လiuသ1eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။ Class လiueရ&သ1&မယ#ဆiuရင#
keyword အeနန8A အသiအမ9တ#*ပuမ91 မဟuတ#ပ?ဘR&။ 6ပ7&eတ15 CLASS လiuလ8 eရ&သ1&လiuမရပ?ဘR&။

Identifier
HelloJava ကeတ15 Class Name *ဖစ#6ပ7& Identifier လiueခLပ?တယ#။ Class ကiu ဘယ#လiueခLeဝLမလ8 ဆiuတ1ကiu
သတ#မ9တ#eပ&တ1ပ?။ အ*ခခPအ1&*ဖင#5eတ15 Class Name eတ>ကiu Noun eတ>ကiu အသuP&*ပu6ပ7& eရ&သ1&eလ5Diပ?
တယ#။ Upper Case န8A စ6ပ7&eရ&သ1&eပ&ရပ?မယ#။ Noun eတ>ကiu ၂ ခuတ>86ပ7& eရ&သ1&မယ#ဆiuရင#eတ15 (Hello န8A
Java) ဒuတiယ Noun ရ8S အစစ1လuP&ကiu Upper Case န8A eရ&သ1&သင#5ပ?တယ#။ Camel Case eရ&သ1&ပuPလiu eခL
eလ5Diပ?တယ#။

Class Name ကiu Lower Case ကiu သuP&လiuမရဘR&လ1&။ Camel Case န8A မeရ&လiuမရဘR&လ1&ဆiueတ15 မeရ&ရင#လ8
ရပ?တယ#။ Compiler Error တက#မ91 မဟuတ#ပ?ဘR&။ ဒ?eပမ85 စနစ#က(တ85 eရ&သ1&ပuPeတ15 မဟuတ#ပ?ဘR&။ Team
Development လuပ#တ85 အခ?eတ>မ91 ဆiuရင# သတ#မ9တ#ထ1&တ85 စည#&ကမ#&eတ>ကiu လiuက#န16ပ7&eရ&သ1&တ1ဟ1
eက1င#&တ85 အက(င#5*ဖစ#ပ?တယ#။

Class Name Identifier eတ>ကiu eရ&သ1&တ85 eနရ1မ91 Java ရ8S Keyword eတ>ကiu အသuP&*ပuလiuမရပ?ဘR&။
Unicode Charactor eတ>၊ Currency Code eတ>၊ Number eတ>န8A Under Score ကiu အသuP&*ပu6ပ7& eရ&သ1&Biuင#ပ?
တယ#။ ဒ?eပမ85 Number eတ>န8A စ6ပ7& eရ&သ1&လiuမရပ?ဘR&။

Class Body
အတ>န#Aက>င#& အဖ>င#5န8A အပiတ#ကeတ15 Class Body *ဖစ#ပ?တယ#။ class တစ#ခuကiu eရ&သ1&တ85 အခ?မဟ1 Class
Body ကiu မ*ဖစ#မeနeရ&သ1&ရပ?မယ#။ Class Body အတ>င#&မ91eတ15 Class ထ8မ91 ပ?eစခ(င#တ85 အရ1eတ>ကiu
eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။

Learning Java 2 မင#&လ>င#


Java Developer Class

Class Declaration

အထက#eဖL*ပပ?အတiuင#& keyword, Class name Identifier န8A Class Body တiuသ1ပ?မယ#ဆiuရင# Java Compiler
ကeန မ9န#ကန#တ85 Class တစ#ခuအ*ဖစ# အသiအမ9တ#*ပuမ91 *ဖစ#ပ?တယ#။ ဒ7eဖL*ပပuPကeတ15 Class တစ#ခuအတ>က# အ
န8ဆuP&လiuအပ#ခ(က#eတ> *ဖစ#0ကပ?တယ#။ ဒ?eပမ85 Class တစ#ခuမ91 eရ&သ1&မယ#ဆiuရင# အ*ခ1&eရ&သ1&Biuင#တ1eတ>Diပ?
eသ&တယ#၊ ဆက#လက#6ပ7& eလ5လ1သ>1&0ကပ?မယ#။

public final class Student extends Person implements Runnable {}

Modifiers Extends Other Implements


Class Interface

Java Class တစ#ခuမ91 eရ&လiuရသမTကiu eရ&သ1&ထ1&တ85 နမRန1 ပuPစP*ဖစ#ပ?တယ#။ eUမ91 eလ5လ1လ1ခ85သလiuဆiuရင#


မ*ဖစ#မeနeရ&သ1&ရန#လiuအပ#eသ1 ပuPစPမ91 class Student {} သ1*ဖစ#ပ?တယ#။ က(န#တ1eတ>ကeတ15 လiuအပ#တ85
အခ?မ91 ထည#5eရ&သ1&ရeသ1 အရ1မ(1&*ဖစ#0ကပ?တယ#။

Modifiers
အထက#ပ?ပuPတ>င#ပ?ဝင#eသ1 public န8A final တiuဟ1 Modifier eတ>*ဖစ#0ကပ?တယ#။ Modifier ဆiuတ1ဟ1 Default
အရည#အeသ>&eတ>ကiu *ပu*ပင#လiuတ85အခ? (Modify) အသuP&*ပuBiuင#eသ1 အရ1မ(1&*ဖစ#ပ?တယ#။ Modifier eတ>ကiu
Access Modifier န8A Non Access Modifier ဆiu6ပ7& ၂ မ(iu&ခ>8*ခ1&Biuင#ပ?တယ#။ နမRန1ထ8တ>င#ပ?ဝင#eသ1 public ဆiu
တ1ဟ1 Access Modifier တစ#ခu*ဖစ#6ပ7& Student Class ကiu မည#သည#5 eနရ1မ9မဆiu အသuP&*ပuBiuင#e0က1င#&
သတ#မ9တ#eပ&eနပ?တယ#။

✴ Access Modifier န8A ပတ#သက#6ပ7& Modifier အခန#&B9င#5 OOP ရ8S Encapsulation အခန#&တiuမ91 အeသ&စiတ#
eဖL*ပသ>1&ပ?မယ#။

final ဆiuတ1ကလ8 Non Access Modifier တစ#ခu*ဖစ#ပ?တယ#။ final လiueရ&သ1&လiuက#တ1န8A Student Class ဟ1
final class *ဖစ#သ>1&6ပ7& Student Class ကiu အ*ခ1& Class eတ>ကလ16ပ7& Extends လuပ#လiuရeတ15မ91 မဟuတ#ပ?
ဘR&။

✴ Non Access Modifier eတ>ကiuလ8 Modifier အခန#&မ91 အeသ&စiတ#eဖL*ပသ>1&ပ?မယ#

Learning Java 3 မင#&လ>င#


Java Developer Class

Extends Other Class


Class တစ#ခuဟ1 လiuအပ#ရင# အ*ခ1& Class တစ#ခuကiu Extends *ပuလuပ#ပiuင#ခ>င#5Diပ?တယ#။ ဒ7လiu Extends လuပ#လiuက#*ခင#&အ1&*ဖင#
Class eတ>0က1&မ91 IS A Relationship ကiu *ဖစ#eပLeစ6ပ7& Super Class ထ8မ91Diတ85 private ကလ>8တ85 Method eတ>
Variable eတ>ကiu အသuP&*ပuလiuရသ>1&ပ?မယ#။

အထက#ပ?ပuPမ91 Student extends Person လiueရ&သ1&ထ1&တ85အတ>က# Student ဟ1 Person Class ကiu Extends လuပ#ထ1&
တယ#လiu eဖL*ပထ1&ပ?တယ#။ ဒ?5e0က1င#5 Student is A Person *ဖစ#သ>1&ပ?မယ#။

✴ OOP အခန#&မ91 အeသ&စiတ#eဖL*ပသ>1&ပ?မယ#။

Implements Interface
Class တစ#ခuဟ1 Interface eတ>ကiu Implement လuပ#Biuင#6ပ7& implements keyword eန1က#မ91 Implement လuပ#လiuတ85
Interface eတ>ကiu eက1#မ1 ခP6ပ7&eရ&သ1&eပ&ရမ91 *ဖစ#ပ?တယ#။ Interface Implementation eတ>ကiu အ6မ8တမ#& Class
Extension eတ>ရ8Seန1က#မ91 eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။ Class တစ#ခuဟ1 Class တစ#ခuထ8ကiuသ1 Exends လuပ#ပiuင#ခ>င#5Dieပမ85
Interface eတ>ကiueတ15 တစ#ခuထက#မက Implements လuပ#ပiuင#ခ>င#5Diပ?တယ#။

အထက#eဖL*ပပ?ပuPမ91 implements Runnable လiueရ&သ1&ထ1&တ1ဟ1 Student Class ဟ1 Runnable Interface ကiu


Implement လuပ#ထ1&တယ#ဆiuတ1ကiu eဖL*ပeနတ1 *ဖစ#ပ?တယ#။

✴ Types of Class B9င#5 OOP အခန#&မ91 အeသ&စiတ#eဖL*ပသ>1&ပ?မယ#။

Class Members
Class Body အတ>င#&မ91 eရ&သ1&လiuရတ85အရ1eတ>ကeတ15 Class Members eတ>*ဖစ#ပ?တယ#။ ဒ?*ဖင#5 Class
Body ထ8မ91 ဘ1eတ>eရ&လiuရလ8။

1. Variables
2. Methods
3. Constructors
4. Nested Classes and Interfaces
5. Nested Enums

Nested Member (No.4 , No.5)eတ>န8Aပတ#သက#6ပ7& Types of Classes မ91လ8 eဖL*ပသ>1&ပ?မယ#။ ဒ?5e0က1င#5 ဒ7


eနရ1မ91eတ15 Variables, Methods န8A Constructors eတ>အe0က1င#&ကiuပ8 အဓiကထ1&6ပ7& eဖL*ပသ>1&ပ?မယ#။

Variables
Variable ဟ1ဘ1လ8လiue*ပ1ရမယ#ဆiuရင# ကOန#eတ1#တiu အလယ#တန#&eလ1က#မ91 သင#ခ85ဘR&တ85 မသiကiန#& တစ#ခuန8A
တRပ?တယ#။ မသiတ85 ကiန#&တစ#ခuကiu x လiueပ&6ပ7& x eနရ1ကiu 10 လiueပ&လiuက#ရင# x ကiu 10 လiuထ1&6ပ7& တ>က#ခ850ကတ1
ကiu မ9တ#မiပ?ဦ&မယ#။ Java ရ8S variable ဟ1လ8 ဒ7သeဘ1ပ?ပ8။ ဒ?eပမ85 Java မ91က Variable ကiu အသuP&*ပuခ(င#ရင#
သuP&ပ?မယ#ဆiu6ပ7& eဖL*ပeပ&ရပ?တယ#။ အ85ဒ?ကiu Variable Declaration လiu eခLပ?တယ#။ Variable တစ#ခuကiu
Declare လuပ#တ85 eနရ1မ91 Variable Type ရယ# Variable Name ကiu eဖ1#*ပeပ&ဖiuလiuအပ#ပ?တယ#။

eန1က#6ပ7& Java Class eတ>ထ8မ91 Variable eတ>ကiu Xကiuက#တ85 eနရ1မ91 Declare လuပ#6ပ7& အသuP&*ပuBiuင#ပ?တယ#။
ကOန#eတ1#တiu Variable eတ>ကiu eလ5လ1တ85eနရ1မ91 eအ1က#ပ?အခ(က#eတ>ကiu သiထ1&ဖiuလiuအပ#ပ?တယ#။

Learning Java 4 မင#&လ>င#


Java Developer Class
✴ Variable ကiu ဘယ#လiu eရ&ရမလ8
✴ Variable အမ(iu&အစ1&eတ>ဟ1 ဘယ#လiuDiလ8
✴ Variable eတ>ကui ဘယ#လiuသuP&ရမလ8

Variable ကiu ဘယ#လiueရ&ရမလ8

Variable eတ>ကiu Class ထ8က Xကiuက#တ85eနရ1မ91 မဆiu eရ&သ1&Biuင#ပ?တယ#။ eရ&သ1&ထ1&တ85 eနရ1အeပLမRတည#


6ပ7& အမ(iu&အစ1&တRမ91 မဟuတ#ပ?ဘR&။ အ85ဒ7အe0က1င#&န8A ပတ#သက#6ပ7& Variable Types eတ>အe0က1င#&e*ပ1တ85အခ?
မ9ပ8 အeသ&စiတ# e*ပ1*ပပ?မယ#။

ဒ7eနရ1မ91eတ15 Variable တစ#ခuကiu ဘယ#လiueရ&ရင#မ9န#မလ8 ဆiuတ1ကiu ကOန#eတ1#တiu eလ5လ1သ>1&0ကရeအ1င#။

[Type] [Variable Name];

Variable eတ>ကiu Program ထ8မ91 အသuP&*ပueတ15မယ#ဆiuရင# အရင#ဆuP& Declare လuပ#ထ1&ဖiuလiuအပ#ပ?တယ#။


Variable eတ>ကiu Declare လuပ#တ85eနရ1မ91 အထက#ပ?အတiuင#& Type န8A Variable Name ကiu eရ&လiuက#ရင# ရပ?6ပ7။
တနည#&e*ပ1ရမယ#ဆiuရင# Java ဘ1သ1ရပ#မ91 Variable eတ>ကiu Declare လuပ#မယ#ဆiuရင# အ85ဒ7 Variable ရ8S Type
ကiu မသတ#မ9တ#ပ8 eရ&လiuမရပ?ဘR&။ ဒ?5e0က1င#5 Java Programming Language ကiu Strong Type Language လiu
လ8 eခLeလ5Diပ?တယ#။

int တiu char တiuလiu Primitive Data Type eတ>ကiuလ8 Type အ*ဖစ# အသuP&*ပuBiuင#သလiu String တiuလiu Java API က
*ပင#ဆင#ထ1&တ85 Class eတ>ကiuလ8 Type အ*ဖစ#အသuP&*ပuBiuင#ပ?တယ#။ ဒ?5အ*ပင# User က eရ&သ1&ထ1&တ85 Class
eတ>ကiuလ8 Type အ*ဖစ# အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။

int count; // primitive data type


String name; // Predefined Type
Account account; // User Defined Class

Variable အမ(iu&အစ1&eတ>ဟ1 ဘယ#လiuDiလ8

Variable eတ>ကiu ဘယ#လiueရ&ရမယ#ဆiuတ1ကiu eလ5လ16ပ7&တ85 eန1က#မ91 Variable အမ(iu&အစ1&eတ> ဘယ#လiuDiလ8


ဆiuတ1ကiu eလ5လ1သ>1&0ကရeအ1င#။ Variable eတ>ကiu eရ&သ1&လiuက#တ85eနရ1အeပLမ91 မRတည#6ပ7& အမ(iu&အစ1&လ8
ခ>8*ခ1&Biuင#ပ?တယ#။ အ85ဒ?အ*ပင# Non Access Modifier eတ>ကiu eရ&သ1&*ခင#&*ဖင#5လ8 အမ(i&အစ1&eတ>ကiu သတ#မ9တ#
လiuရပ?တယ#။

အရင#ဆuP&eရ&သ1&ထ1&တ85eနရ1အeပLမ91 မRတည#6ပ7& အမ(iu&အစ1&ခ>8*ခ1&ပuPကeန စ6ပ7& eလ5လ1သ>1&0ကရeအ1င#။ မiမi


eရ&သ1&ထ1&တ85 eနရ1အeပLမ91 မRတည#6ပ7& Variable eတ>ကiu အသuP&*ပuBiuင#တ85 Scope eတ>ဟ1 က>8*ပ1&0ကပ?တယ#။

Learning Java 5 မင#&လ>င#


Java Developer Class
eရ&သ1&ထ1&တ85 eနရ1အeပLမRတည#6ပ7& Variable eတ>ကiu Global Variable န8A Local Variable ဆiu6ပ7& ခ>8*ခ1&Biuင#ပ?
တယ#။

Global Variables
Class Body eအ1က#တည#5တည#5မ91 eရ&ထ1&တ85 Variable eတ>ဟ1 Global Variable eတ>*ဖစ#0ကပ?တယ#။ အ85ဒ7
Variable eတ>ကiu Class အတ>င#&Xကiuက#တ85eနရ1မ91 အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။ ဒ7eနရ1မ91 အ85ဒ7 Variable eတ>ကiu
အသuP&*ပuBiuင#တ85 အခ(iန# အတiuင#&အတ1ဆiuတ1ကiuလ8 သiဖiuလiuအပ#ပ?တယ#။ ဒ?ကiueတ15 eန1က#မ91 ဆက#6ပ7& Dင#&*ပသ>1&
ပ?မယ#။

class Student {

String name;

public void showName() {


[Link](name);
}
}

အထက#ပ?အတiuင#& Student Class ရ8S Class Body eအ1က#မ91 String name လiueရ&ထ1&တ1ဟ1 String Type
Variable *ဖစ#တ85 name ဆiuတ85 Variable တစ#ခuကiu eရ&သ1&လiuက#တ1ပ?ပ8။ Class Body eအ1က#မ91 eရ&သ1&ထ1&
တ1 *ဖစ#တ85 အတ>က# ဒ?ဟ1 Global Variable *ဖစ#ပ?တယ#။

ဒ?5e0က1င#5 name variable ကiu showName method ထ8ကeနလ8 အသuP&*ပueနBiuင#တ1 *ဖစ#ပ?တယ#။ Global
Variable eတ>ကiuဆiuရင# Class တစ#ခuလuP&ကeန အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။

Global Variable eတ>မ91 Value ကiu သတ#မ9တ#မထ1&ဘR&ဆiuရင# Compiler ကeန Default တန#ဖiu&ကiu သတ#မ9တ#
eပ&မ91 *ဖစ#ပ?တယ#။ Number eတ>ရ8S Default တန#ဖiu&ဟ1 0 *ဖစ#6ပ7&၊ boolean eတ>ရ8S Default တန#ဖiu&ဟ1 false
*ဖစ#ပ?တယ#။ Object eတ>ရ8S Default တန#ဖiu&ကeတ15 null *ဖစ#ပ?တယ#။

Global Variable eတ>ကiuထပ#6ပ7& ဘယ#eနရ1မ91 သuP&ခ(င#တ1လ8 ဆiuတ85 ရည#ရ>ယ#ခ(က#န8A ထပ#6ပ7& ခ>8*ခ1&Biuင#ပ?တယ#။


What is Class ဆiuတ1ကiuDင#&*ပရင#&န8A Class တစ#ခuကiu eရ&သ1&ရ*ခင#& ရည#ရ>ယ#ခ(က# ၂ ခuDiတယ#လiue*ပ1ခ85ပ?တယ#။
အ85ဒ?eတ>ကeတ15 Class ကeန တဆင#5 Object ကiuတည#eဆ1က#ဖiuရည#ရ>ယ#ခ(က#န8A Utility Function eတ>ကiu
eရ&သ1&ဖiuရည#ရ>ယ#ခ(က#ပ?။

အ85ဒ7 ရည#ရ>ယ#ခ(က#eတ>အeပLမRတည#6ပ7& Variable eတ>ကiuလ8 Object eတ>မ91သuP&ခ(င#တ85 Varable eတ>န8A Class ထ8


မ91 ထ1&6ပ7& Function eတ>ထ8မ91 အသuP&*ပuခ(င#တ85 Variable eတ>ဆiu6ပ7& ခ>8*ခ1&Biuင#ပ?တယ#။

Instance Variable
Instance Variable eတ>ဆiuတ1ကeတ15 Object eတ>မ91 အသuP&*ပuခ(င#တ85 Variable eတ>*ဖစ#ပ?တယ#။ Class တစ#ခu
ကiu eရ&သ1&ရတ85 အဓiက ရည#ရ>ယ#ခ(က#ဟ1 Object eဆ1က#ဖiuဆiuတ1 *ဖစ#တ85 အတ>က# Default အတiuင#&ဆiuရင#
Global Variable eတ>ဟ1 Instance Variable eတ> *ဖစ#0ကပ?တယ#။

Learning Java 6 မင#&လ>င#


Java Developer Class
အထက#ပ? နမRန1ထ8မ91ဆiuရင# String name; လiu Class Body eအ1က#မ91 eရ&သ1&ထ1&တ85 အတ>က# name ဟ1
String type Variable *ဖစ#တယ#။ 6ပ7&eတ15 Global Variable လ8*ဖစ#6ပ7& Instance Variable တစ#ခu*ဖစ#တယ#လiu
သiBiuင#မ91 *ဖစ#ပ?တယ#။

Global Variable *ဖစ#6ပ7& value ကiu ဘ1မ9 မeရ&ထ1&တ85 အတ>က# Default တန#ဖiu&ဟ1 null *ဖစ#eနမယ#ဆiuတ1ကiu
သieနBiuင#ပ?တယ#။ 6ပ7&eတ15 Instance Variable တစ#ခu*ဖစ#တယ#ဆiuတ1ကiuလ8 သiBiuင#ပ?တယ#။ eUမ91လ8e*ပ1ခ856ပ7&
*ဖစ#ပ?တယ#။ Instance Variable eတ>ဟ1 Class ထ8မ91 ထ1&6ပ7&သuP&တ85 Variable eတ>မဟuတ#ပ8 အ85ဒ7 Class ကeန
Object ကiu တည#eဆ1က#တ85အခ?မ91 အ85ဒ7 Object eတ>မ91 ပiuင#ဆiuင#သင#5တ85 State eတ>ကiu သiမ#&eပ&ထ1&ဖiuအတ>က#
သuP&*ပuပ?တယ#။

ဒ?5e0က1င#5 Instance Variable eတ>ဟ1 Object တစ#ခuခ(င#&ဆ7န8A သက#ဆiuင#တ85 Variable eတ> *ဖစ#0ကပ?တယ#။
Class တစ#ခuကeန Object ဘယ#B9စ#ခueဆ1က#Biuင#လ8ဆiuရင# အXကiမ#Xကiမ#တည#eဆ1က#Biuင#ပ?တယ#။ 6ပ7&eတ15 အ85ဒ7
Object တစ#ခuခ(င#&စ7ဟ1လ8 သ7&*ခ1&စ7ရပ#တည#eနတ1*ဖစ#ပ?တယ#။

class StudentTest {

public static void main(String [] args) {


Student s1 = new Student();
[Link] = “Aung Aung”;

Student s2 = new Student();


[Link] = “Thidar”;
}

Student s1 = new Student() လiueရ&ထ1&တ1ဟ1 အရင#ဆuP& Stack Memory eပLမ91 s1 ဆiuတ85 အမည#န8A
Student Type Variable တစ#ခuကiu သ>1&eဆ1က#ပ?တယ#။ 6ပ7&မ9 new Student() လiueရ&တ85 ကuဒ#ကiu Evaluate လuပ#
6ပ7& Heap Memory eပLမ91 Student Object ကiu သ>1&eဆ1က#ပ?တယ#။ ဒ7လiu Object eဆ1က#တ85အခ?မ91 Student
Class ထ8မ91Diတ85 instance variable eတ>ကiu Copy ကR&သ>1&6ပ7& ယRသ>1&ပ?တယ#။ ဒ?5e0က1င#5 Student Object ထ8
မ91 name ဆiuတ85 Instance Variable လ8 ပ?သ>1&ပ?တယ#။

6ပ7&မ9 s1 eနရ1ကiu တည#eဆ1က#ထ1&တ85 Object ရ8S Memory Address ကiu အစ1&ထiu&ပ?တယ#။ ဒ7လiuပ8 s2 Variable
ကiuလ8 တည#eဆ1က#သ>1&တ1 *ဖစ#ပ?တယ#။ ဒ?5e0က1င#5 s1 ရ8S name န8A s2 ရ8S name eတ>ဟ1 သ7&*ခ1&စ7 *ဖစ#eနမ91
*ဖစ#ပ?တယ#။ [Link] = “Aung Aung” လiu eရ&လiuက#တယ#ဆiuရင# s1 ရ8S name သ1 Aung Aung *ဖစ#သ>1&မ91 *ဖစ#
6ပ7& s2 ရ8S name န8A သက#ဆiuင#*ခင#& Diမ91 မဟuတ#ပ?ဘR&။ အလ1&တR [Link] = “Thidar” လiueရ&လiuက#ရင#လ8 s2 ရ8S
name သ1 တန#ဖiu&e*ပ1င#&သ>1&တ1 *ဖစ#6ပ7& s1 ရ8S name န8A ပတ#သက#မD - iမ91 မဟuတ#ပ?ဘR&။

Instance Variable eတ>ဟ1 Object eတ>အတ>က# eရ&ထ1&တ85 Variable eတ> *ဖစ#တ85 အတ>က# Instance
Variable eတ>ကiu Object Variable eတ>လiuလ8 eခLeလ5Diပ?တယ#။ ဒ?5e0က1င#5 Instance Variable eတ>ကiu Access
လuပ#ခ(င#တ85 အခ?မ91 Owner *ဖစ#တ85 Object က တဆင#5 [Link] လiu eရ&သ1&ရတ1 *ဖစ#ပ?
တယ#။

Learning Java 7 မင#&လ>င#


Java Developer Class

Static Variable
Java ဘ1သ1ရပ#မ91 Class eတ>ကiu eရ&သ1&တ1ဟ1 Object eဆ1က#ဖiu Xက7&ပ8 မဟuတ#ပ?ဘR&။ Java မ91 Class မပ?
ပ8 ကuဒ#eတ>ကiu eရ&သ1&လiu မရBiuင#ပ?ဘR&။ ဒ?5e0က1င#5 Utility Function eတ>ကiu eရ&သ1&မယ#ဆiuရင#eတ1င#မ9 Class
ထ8မ91ပ8 eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။ အ85ဒ7လiu Class ထ8မ91ပ8ထ1&6ပ7& အသuP&*ပuခ(င#တ85 Method eတ>ကiueတ15 Static
Method လiueခLပ?တယ#။ တဖန# အ85ဒ7 Method eတ>အ0က1&မ91 ဘuPအeနန8A ထ1&6ပ7& သiမ#ထ1&ခ(င#တ85 တန#ဖiu&eတ>
အတ>က# Variable eတ>လiuအပ#လ1ပ?တယ#။ Object မ91 သuP&ဖiu မဟuတ#ပ8 Class ထ8မ91ပ8 ထ1&6ပ7& အသuP&*ပuခ(င#တ85
Variable eတ>ကiueတ15 Static Variable eတ>လiu eခL0ကပ?တယ#။

က8 ဒ7eနရ1ကစ6ပ7& Static Variable eတ>ကiu ဘယ#လiueရ&ရမယ#၊ 6ပ7&eတ15 Static Variable eတ>ရ8S ထR&*ခ1&ခ(က#eတ>
န8A Static Variable eတ>ကiu ဘယ#လiu အသuP&*ပuရမယ# ဆiuတ1ကiu eလ5လ1သ>1&0ကရeအ1င#။

Static Variable eတ>ကiuဘယ#လiueရ&0ကမလ8

Global Variable eတ>ကiu ဒ7အတiuင#& eရ&ထ1&ရင# Instance Variable eတ>*ဖစ#ပ?တယ#။ တနည#& e*ပ1ရမယ#ဆ7uရင#
Global Variable eတ>ဟ1 Default အတiuင#&ဆiuလiuကeတ15 Object eတ>မ91 သuP&မည#5 Instance Variable eတ> *ဖစ#
eနမ91ပ?ပ8။ Static Variable အ*ဖစ# အသuP&*ပuလiuတ85အခ?မ91 static ဆiuတ85 Non Access Modifier ကiu Variable
Type ရ8S eUမ91 eရ&သ1&6ပ7& Static Variable *ဖစ#e0က1င#& eဖL*ပBiuင#မ91 *ဖစ#ပ?တယ#။

class Student {

static int count;


String name;

void show() {
[Link](“Count : “ + count);
[Link](“Name : “ + name);
}

အထက#ပ? Student Class ထ8မ91 Global Variable ၂ ခuDiပ?တယ#။ count န8A name ပ?။ count ဟ1 int type *ဖစ#
6ပ7& static လiueရ&ထ1&တ85 အတ>က# static variable *ဖစ#ပ?တယ#။ 6ပ7&eတ15 name ကeတ15 Type အ*ပင# ဘ1မ9 မ
eရ&ထ1&တ85 အတ>က# Instance Variable *ဖစ#ပ?တယ#။

Static Variable eတ>ရ8S ထR&*ခ1&ခ(က#eတ>


Instance Variable eတ>ဟ1 Object eတ> အတ>က# *ဖစ#တ85 အတ>က# Class ကeန Object eဆ1က#တ85 အခ?မ91
eက1#ပ7ကR&သ>1&6ပ7& Object ထ8မ91 State eတ>ကiu သiမ#&ထ1&ဖiu အတ>က# အသuP&*ပuပ?တယ#။ ဒ?eပမ85 Static Variable
eတ>ကeတ15 Utility Function eတ>အ0က1&မ91 State eတ>ကiu Share လuပ#6ပ7& အသuP&*ပuဖiu အတ>က# *ဖစ#ပ?တယ#။

ဒ?5e0က1င#5 Object eဆ1က#တ85 အခ?မ91 Static Variable eတ>ကiu Copy ကR&6ပ7& ယRမသ>1&ပ?ဘR&။ Class ထ8မ91ပ8 ခ(န#
ထ1&ခ85ပ?တယ#။ Utility Function eတ>အ0က1&မ91 အသuP&*ပuဖiu ထ1&တ85 Variable eတ>*ဖစ#တ85 အတ>က# Static
Variable eတ>ဟ1 Class ထ8မ91ပ8 Dieနမ91 *ဖစ#ပ?တယ#။ 6ပ7&eတ15 Class eတ>ဟ1 JVM eပLမ91 တစ#ခuထ8သ1Diတ85

Learning Java 8 မင#&လ>င#


Java Developer Class

class Student {

static int count;


String name;

void show() {
[Link](“Count : “ + count);
[Link](“Name : “ + name);
}

void countUp() {
count ++;
}

public static void main(String [] args) {


Student s1 = new Student();
[Link]();
[Link] = “Aung Aung”;
[Link]();

Student s2 = new Student();


[Link]();
[Link] = “Thidar”;
[Link]();
}

အတ>က# Static Variable eတ>ဟ1လ8 JVM မ91 တစ#ခuထ8သ1Diတ85 Variable eတ>အeနန8A ရပ#တည#မ91 *ဖစ#ပ?တယ#။
ဒ7eနရ1မ91 Static Variable eတ>ရ8S ထR&*ခ1&ခ(က#ကiu သieအ1င# eအ1က#ပ? Code eတ>န8A စမ#&0ကည#50ကရeအ1င#။
အထက#ပ? ကuဒ#ထ8မ91 countUp ဆiuတ85 Method ထ8မ91 count ++ ဆiu6ပ7& static variable ကiu တစ#တiu&ထ1&ပ?တယ#။

6ပ7&eတ15 main method ထ8ကiu 0ကည#50ကရeအ1င#။ အရင#ဆuP&. s1 ဆiuတ85 Object တစ#ခuကiu eဆ1က#တယ#။ s1 ရ8S
countUp method ကiu invoke လuပ#ပ?တယ#။ ဒ?ဆiuရင# ဘ1*ဖစ#သ>1&လ8 ဆiuတ1ကiu 0ကည#50ကရeအ1င#။ count ဆiuတ85
variable ကiu တစ#တiu&ပ?မယ#။ ဒ?5e0က1င#5 count ရ8S တန#ဖiu&ဟ1 1 *ဖစ#သ>1&မယ#။

6ပ7&eတ15 ဆက#6ပ7& [Link]() ကiu eခLပ?တယ#။ ဒ7eနရ1မ91 s1 ရ8S count န8A name ကiu Console eပLမ91 *ပeပ&ပ?
မယ#။ သတiထ1&ဖiuလiuတ1က count ဟ1 Static Variable *ဖစ#6ပ7&eတ15 name ဟ1 Instance Variable *ဖစ#eနတ1
ကiuပ?ပ8။ ဆက#6ပ7& s1 ဆiuတ85 န1မည#န8A Object တစ#ခuကiu eဆ1က#*ပန#ပ?တယ#။ 6ပ7&eတ15 countUp() method ကiu eခL
*ပန#တယ#။ 6ပ7&မ9 s2 ရ8S name ကiu Thidar လiu အစ1&ထiu&6ပ7& s2 ရ8S show() Method ကiu eခL*ပန#တယ#။

S1 ရ8S show() Method ကiu eခLတuန#&က အe*ဖဘယ#လiu*ပ6ပ7& s2 ရ8S show() Method eခLeတ15 အe*ဖဘယ#လiu
ထ>က#မယ#ဆiuတ1ကiu သတiထ1&မieစခ(င#ပ?တယ#။

Learning Java 9 မင#&လ>င#


Java Developer Class
Count : 1
Name : Aung Aung
Count : 2
Name : Thidar

အထက#ပ? အတiuင#& အe*ဖထ>က#ပ?မယ#။ [Link]() ကiu eခLခ85တuန#&က count ရ8S တန#ဖiu&ဟ1 1 *ဖစ#ပ?တယ#။ show()
method ကiu မeခLခင# countUp() Method ကiu eခLခ85တယ#။ ဒ?5e0က1င#5 count ဟ1 1 *ဖစ#သ>1&ပ?တယ#။

ဒuတiယ အXကiမ# [Link]() ကiu eခL*ပန#တယ#။ count ကiu တစ#*ပန#တiu&ပ?တယ#။ တကယ#လiuသ1 count ဟ1
Object eတ>eဆ1က#တiuင#& Copy ကR&သ>1&ရင# အ85ဒ7eနရ1မ91လ8 count ကiu s2 က ပiuင#မ91 *ဖစ#တ85 အတ>က# တန#ဖiu&ဟ1
၁ ပ8*ဖစ#မ91ပ?။ ဒ?eပမ85 count ဟ1 Static Variable *ဖစ#တယ#။ ဒ?5e0က1င#5 s2 ဟ1 JVM တစစ#ခuလuP&မ91 တစ#ခuထ8သ1
Diပ?တယ#။ s1 က သiတ85 count eက1 s2 ကသiတ85 count ပ? တစ#eနရ1ထ8မ91 Diတ85 count ကiu *မင#eနတ1 *ဖစ#
တယ#။ ဒ?5e0က1င#5 s1 ရ8S count ကiu တစ#တiu&လiuက#eတ15 count ရ8S တန#ဖiu&ဟ1 1 *ဖစ#လ16ပ7&၊ eန1က#တစ#Xကiမ# s2
ကeန count ကiu တစ#*ပန#တiu&eတ15 တန#ဖiu&ဟ1 2 *ဖစ#သ>1&တ1 *ဖစ#ပ?တယ#။

ဒ7eနရ1မ91 Static Variable eတ>ဟ1 JVM eပLမ91 တစ#ခuထ8သ1 Diမ91 *ဖစ#တ85 အတ>က# ဘယ# Object ကပ8 သuP&သuP&
တစ#ခuထ8ကiu သuP&eနတယ#ဆiuတ1ကiu သieစခ(င#ပ?တယ#။

Static Variable eတ#ကiu ဘယ)လiueနရ-မ/iu1eတ#မ2- အသu516ပu8ကမလ9


ဒ?ကeတ15 Static Variable eတ>ရ8S ထR&*ခ1&ခ(က#ကiu 0ကည#5ရင# သiBiuင#ပ?လiမ#5မယ#။ Static Variable eတ>ကiu Utility
Function eတ>အ0က1&မ91 State eတ>ကiu ဘuPအeနန8A အသuP&*ပuခ(င#တ85 အခ?မ(iu&မ91 အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။
eန1က#တစ#ခuက မiမiရ8S Application တစ#ခuလuP&မ91 Share လuပ#6ပ7& သuP&ခ(င#တ85 Data မ(iu&eတ>မ91လ8 Static Variable
eတ>ကiu အသuP&*ပuBiuင#ပ?တယ#။

ဒ?5e0က1င# Desktop Application eတ>မ91 Login User ရ8S Information eတ>ကiu Application တစ#ခuလuP&မ91 ဘuPအeန
န8A Share လuပ#6ပ7& အသuP&*ပuလiuတ85 အခ?မ(iu&မ91 Static Variable eတ>ကiu အသuP&*ပu0ကတ1ကiu eတ>Zရပ?မယ#။ 6ပ7&eတ15
Application တစ#ခuလuP&မ91 Object တစ#ခuထ8သ1 Dieစလiuတ85 Singleton Design Patterns မ(iu&မ91လ8 Static
Variable eတ>ကiu အသuP&*ပuတ1ကiu eတ>Zရပ?တယ#။

OOP ကiu သင#0က1&6ပ7&တ85 အခ?မ91 Design Patterns eတ>အe0က1င#&ကiu Class Design eတ>န8A လက#eတ>Z
*ပဿန1eတ>ကiu ဘယ#လiu e*ဖDင#&တယ#ဆiuတ1ကiu eဖL*ပသ>1&ပ?မယ#။ အ85ဒ7 အခန#&eရ1က#ရင# Singleton Pattern
အe0က1င#&ကiu အe0က1င#&ကiu ဆက#6ပ7& eဖL*ပသ>1&ပ?မယ#။

Learning Java 10 မင#&လ>င#


Java Developer Class

Local Variables
Code Block တစ#ခuခuအတ>င#&မ91 စ16ပ7& Declare လuပ#ထ1&တ85 Variable eတ>ဟ1 Local Variable eတ> *ဖစ#0ကပ?
တယ#။ Code Block လiue*ပ1တ85eနရ1မ91 အတ>န#Aက>င#5 { *ဖစ#5စ6ပ7& အတ>န#Aက>င#& အပiတ#န8A } ဆuP&တ85 Code eတ>ကiu
ဆiuလiuတ1ပ?။ Instance Block eတ>၊ Static Block eတ>၊ Method Body eတ>၊ if / else statement Block eတ>၊
For န8A While Block eတ>ဟ1 Code Block eတ>*ဖစ#0ကပ?တယ#။

တဖန# Method Arguments eတ>န8A for loop ထ8မ91 eရ&ထ1&တ85 Variable eတ>ကလ8 Local Variable eတ> *ဖစ#0က
ပ?တယ#။ Local Variable eတ>ဟ1 သRတiuကiu စ6ပ7&eရ&ထ1&တ85 eနရ1ကeန စ6ပ7& သRတiuကiu eရ&ထ1&တ85 Block ဆuP&တ85
အထi အသuP&*ပuလiuရပ?တယ#။ Local Variable eတ>ဟ1 Global Variable eတ>န8A မတRပ8 Default တန#ဖiu&ကiu
Compiler က ထည#5မeပ&Biuင#ပ?ဘR&။ ဒ?5e0က1င#5 Local Variable eတ>ကiu အသuP&*ပueတ15မယ#ဆiuရင# တန#ဖiu& တစ#ခuခu
ကiu Assign လuပ#6ပ7&မ91 အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။ တကယ#လiu တန#ဖiu&ကiu အစ1&မထiu&ပ8 အ85ဒ7 Local Variable တစ#
ခuကiu Access လuပ#ရင# Compile Time မ91 Error တက#မ91 *ဖစ#ပ?တယ#။

Local Variables in Blocks

class LocalVariablesInBlocks {

{
// Instance Block
int data = 10;
}

static {
// static block
int data = 11;
}
}

Class Body အတ>င#&မ91 { န8Aစ6ပ7& } န8A ဆuP&ထ1&တ1ဟ1 Instance Block eတ>*ဖစ#ပ?တယ#။ Instance Block eတ>
ဟ1 Class ကeန Object ကiuထည#eဆ1က#တ85အခ?မ91 Constructor ကiu အလuပ#မလuပ#ခင# တစ#Xကiမ# အလuပ#လuပ#eစ
ပ?တယ#။ အထက#ပ?အတiuင#& Instance Block ထ8မ91 int data = 10 လiueရ&ထ1&တ85 အတ>က# data ဟ1 Local
Variable တစ#ခu*ဖစ#6ပ7& } eနရ1အထi အ8ဒ7 data ကiu အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။

တဖန# static { } လiueရ&ထ1&တ1ကeတ15 static block *ဖစ#ပ?တယ#။ JVM eပLကiu အ85ဒ7 Class ကiu Load လuပ#6ပ7&
တ1န8A static block ထ8မ91Diတ85 ကuဒ#eတ>ကiu အလuပ#လuပ#eစမ91 *ဖစ#ပ?တယ#။

Local Variable in Methods


Method တစ#ခuအတ>င#&မ91 eရ&သ1&ထ1&တ85 Variable eတ>ဟ1လ8 Local Variable eတ> *ဖစ#0ကပ?တယ#။ အ85ဒ7
Variable eတ>ဟ1လ8 စ6ပ7& Declare လuပ#တ85 eနရ1ကeနစ6ပ7& Method Body ဆuP&တ85 အထiသ1 အသuP&*ပuBiuင#မ91
*ဖစ#ပ?တယ#။ Method အထ8မ91 Local Variable eတ>ကiu ၂ eနရ1မ91 eတ>ZBiuင#ပ?တယ#။

Learning Java 11 မင#&လ>င#


Java Developer Class
ပထမဆuP&eတ>ZBiuင#တ1ကeတ15 Method Body.အတ>င#&မ91 Declare လuပ#ထ1&တ85 Variable eတ>ပ?။ eန1က#တစ#ခu
ကeတ15 Method Argument eတ>မ91 eရ&သ1&ထ1&တ85 Variable eတ>ပ8 *ဖစ#0ကပ?တယ#။

class LocalVariablesInMethod {

void someMethod() {
// declare in method
int data = 10;
}

void takeArgument(int data) {


// can use data in method body
}
}

အထက#ပ? နမRန1ထ8မ91 someMethod() ထ8မ91 int data = 10 ဆiu6ပ7&eရ&ထ1&ပ?တယ#။ Type ကiu တ>8eရ&6ပ7ဆiuတ1န8A
ဒ?ဟ1 Variable တစ#ခuကiu လ16ပ7& Declare လuပ#တယ#ဆiuတ1ကiu သeဘ1ပ?က#ဖiuလiuပ?တယ#။ Method Body ထ8မ91
စ6ပ7& Declare လuပ#ထ1&တ85 အတ>က# ဒ?ဟ1 Local Variable *ဖစ#ပ?တယ#။ အ85ဒ7 Variable ကiu Declare လuပ#တ85
eနရ1ရ8S eန1က#ကeနစ6ပ7& Method ဆuP&တ85 အထi အသuP&*ပuBiuင#ပ?တယ#။

eန1က# Method တစ#ခuကeတ15 takeArgument(int data) လiueရ&ထ1&ပ?တယ#။ Method Argument အeနန8A int
data ကiu eရ&သ1&ထ1&ပ?တယ#။ ဒ7eနရ1မ91လ8 Type န8A အတReရ&ထ1&တ1 *ဖစ#တ85 အတ>က# Local Variable *ဖစ#ပ?
တယ#။ အ85ဒ7 Variable ကiu takeArgument method အတ>င#&မ91ပ8 အသuP&*ပuလiu ရမ91 *ဖစ#ပ?တယ#။ ဒ7eနရ1မ91
someMethod ထ8က int data န8A takeArgument ထ8က int data eတ>ဟ1 Type န8A Name ဟ1 တReနeပမ85
အသuP&*ပuBiuင#တ85 eနရ1 (Scope) ဟ1 မတRပ?ဘR&။ ဒ?e0က1င#5 သ7&*ခ1& Variable eတ> *ဖစ#0ကပ?တယ#။

Local Variable in Statements

for(int i=0; i < 10; i++) {


String str = “Count is “;
[Link](str + i);
}

Decision Statement eတ>န8A Looping Statement eတ>ထ8မ91 eရ&သ1&ထ1&တ85 Variable eတ>ဟ1လ8 Local
Variable eတ> *ဖစ#0ကပ?တယ#။ အထက#ပ? နမRန1ထ8မ91 Local Variable ၂ ခu ပ?ဝင#ပ?တယ#။ int i = 0 လiu eရ&ထ1&
တ85 အတ>က# ဒ?ဟ1လ8 Local Variable *ဖစ#ပ?တယ#။ 6ပ7&eတ15 String str လiu eရ&ထ1&တ1လ8 Local Variable *ဖစ#
ပ?တယ#။

Local Variable eတ>ဟ1 သRတiuကiu စeရ&ထ1&တ85 eနရ1ကeနစ6ပ7& သRတiuDieနတ85 Block ဆuP&တ85 အထi အသuP&*ပuBiuင#
မ91*ဖစ#ပ?တယ#။ ဒ?5e0က1င#5 for(int i = 0; i < 10; i ++) ဆiu6ပ7& i ကiu *ပန#6ပ7& အသuP&*ပueနBiuင#တ1 *ဖစ#ပ?တယ#။

Learning Java 12 မင#&လ>င#


Java Developer Class

Variable Hidings
ကOန#eတ1#တiu Variable eတ>ကiu eရ&သ1&တ85 eနရ1မ91 တစ#ခuသတiထ1&ဖiuလiuတ1က Scope တစ#ခuအတ>င#&မ91
န1မည#တR Variable eတ>ကiu eရ&သ1&လiu မရBiuင#ပ?ဘR&။ Scope မတRရင#eတ15 န1မည#တR Variable eတ>ကiueရ&သ1&
Biuင#ပ?တယ#။

Local Variable eတ>အခ(င#&ခ(င#& Scope မတRတ1က *ပဿန1မDieပမ85 Global Scope ထ8က Variable န8A Local
Scope ထ8က Variable eတ>မ91 န1မည#တRခ85ရင# Local Scope ထ8မ91eတ15 Local Scope ထ8က Variable ကiuပ8
အသuP&*ပuသ>1&မ91 *ဖစ#ပ?တယ#။ ဒ7လiu Local Scope ထ8က Variable က Global Scope ထ8က Variable ကiu သ>1&6ပ7&
ဖuန#&က>ယ#သလiu *ဖစ#သ>1&တ1ကiu Variable Hiding လiueခLပ?တယ#။

Global Variable eတ>မ91 Instance Variable န8A Static Variable ဆiu6ပ7& Diပ?တယ#။ အ85ဒ7အတ>က# Variable
Hiding *ဖစ#Biuင#တ85 ပuPစPeတ>ကiu 0ကည#50ကည#5ရeအ1င#။

public class VariableHiding {

String name;
static int count;

public void setValues(String name, int count) {


name = name;
count = count;
}
}

အထက#ပ?နမRန1ကiu 0ကည#5ရင# Global Scope ထ8မ91 Instance Variable name ဆiuတ1Di6ပ7& Static Variable
count ဆiuတ1Diပ?တယ#။ 6ပ7&eတ15 setValues method ထ8မ91လ8 Argument အeနန8A name န8A count ကiu ယRထ1&
ပ?တယ#။ ဒ7လiu eရ&လiuက#တ1န8A Global Scope ထ8က Variable eတ>န8A Local Scope ထ8က Variable eတ>ဟ1
န1မည#တRသ>1&ပ?6ပ7။

ဒ?5e0က1င#5 အ85ဒ7 Scope ထ8မ91 name လiu Access လuပ#ရင# Local Scope ထ8က Variable eတ>ကiuပ8 အသuP&*ပueတ15
မ91 *ဖစ#ပ?တယ#။ အ85ဒ7လiuပ8 count လiueရ&လiuက#ရင#လ8 Local Scope ထ8က Variable ကiuပ8 အသuP&*ပueတ15မ91ပ?။
ဒ?5e0က1င#5 ဒ7eနရ1မ91 name = name ဆiuရင# Local Variable name ကiu Local Variable name န8A အစ1&eနeတ15
မ91 *ဖစ#ပ?တယ#။

ဒ?eပမ85 ဒ7 Method က Argument အeနန8A ယRထ1&တ85 တန#ဖiu&eတ>န8A Global Scope ထ8က Variable eတ>ကiu
သတ#မ9တ#eပ&eနတ1 *ဖစ#ပ?တယ#။ အ85ဒ7လiueနရ1မ(iu&မ91 Global Scope ထ8က Variable ကiu အသuP&*ပuလiuပ?က
[Link] လiu eရ&eပ&ရမ91 *ဖစ#ပ?တယ#။

Global Variable name ဟ1 Instance Variable *ဖစ#တ85အတ>က# သ\ရ8S Owner *ဖစ#တ85 this ကiu eUမ91 eရ&eပ&ရ
မ91 *ဖစ#6ပ7& [Link] = name လiueရ&eပ&ရမ91 *ဖစ#ပ?တယ#။ တဖန# count ဟ1 Static Variable *ဖစ#6ပ7& Owner
ဟ1 Class *ဖစ#တ85 အတ>က# [Link] = count လiu eရ&eပ&ရမ91 *ဖစ#ပ?တယ#။

Variable Hiding မ*ဖစ#တ85 eနရ1eတ>မ91 ဆiuရင#eတ15 Class တစ#ခuထ8က Variable eတ>ကiu Owner ထည#56ပ7& မeရ&
လ8 ရeနမ91 *ဖစ#6ပ7&၊ Variable Hiding *ဖစ#မ9သ1 Owner ကiu ထည#56ပ7& eရ&eပ&ရမ91 *ဖစ#ပ?တယ#။

Learning Java 13 မင#&လ>င#


Java Developer Class

Methods
Java ဘ1သ1ရပ#မ91 Utility Function eတ>၊ Object eတ>ရ8S Behavior eတ>ကiu eဖL*ပဖiuအတ>က# Method eတ>ကiu
အသuP&*ပuပ?တယ#။ Variable eတ>တuန#&ကလiuပ8 Method eတ>မ91လ8 Class eတ>မ91 သuP&မ91လ1& Object eတ>မ91 သuP&
မ91လ1&ဆiu6ပ7& Static Method န8A Instance Method eတ>ဆiu6ပ7& ခ>8*ခ1&ထ1&ပ?တယ#။

အရင#ဆuP& Method တစ#ခuကiu မ9န#eအ1င# ဘယ#လiueရ&ရမယ#ဆiuတ85 အe0က1င#&ကeန စတင# eလ5လ1သ>1&0က


ရeအ1င#။

public class MethodDemo {


// method
void sayHello() {} 4. Method Body
}

1. Return Type

2. Method Name

3. Argument List

Method eတ>ကiu eရ&သ1&တ85eနရ1မ91 Class Body အထ8မ91eရ&သ1&ရပ?တယ#။ Java ဘ1သ1ရပ#မ91 Class eတ>
ရ8S အ*ပင#မ91 Method eတ>ကiu eရ&သ1&လiu မရပ?ဘR&။ ဒ?5အ*ပင# အ*ခ1&eသ1 Code Block eတ>ထ8မ91လ8 Method
eတ>ကiu eရ&သ1&လiu မရBiuင#ပ?ဘR&။

eရ&သ1&မယ#ဆiuရင# [1. Return Type] [2. Method Name] [3. Argument List] [4. Method Body] ဆiu6ပ7&
eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။ အန8ဆuP& အထက#ပ?အခ(က#eတ>ပ?တယ#ဆiuရင# Method အeနန8A Compiler ကeန
အသiအမ9တ#*ပuမ91 *ဖစ#ပ?တယ#။ ဒ7eနရ1မ91 Method Name ကeတ15 Method ရ8S န1မည#*ဖစ#6ပ7& Method Body
ကeတ15 လuပ#eဆ1င#ခ(က#eတ>ကiu eရ&သ1&ရမ91 *ဖစ#တ85 အတ>က# အeထ>အထR& မeဖL*ပeတ15ပ8 Return Type န8A
Argument List အe0က1င#&ကiuပ8 eဖ1#*ပသ>1&ပ?မယ#။

Return Type

Method eတ>ကiu eရ&သ1&တ85 အခ?မ91 အ85ဒ7 Method ကiu Invoke လuပ#တ85အခ?မ91 ဘ1ကiu *ပန#eပ&ရမလ8 ဆiuတ85
Return Type ကiu သတ#မ9တ#eပ&ဖiuလiuအပ#ပ?တယ#။ တကယ#လiu Method တစ#ခuကiu Invoke လuပ#6ပ7&တ85 အခ?မ91 int
type ကiu *ပန#6ပ7& လiuခ(င#တယ#ဆiuရင# Return Type eနရ1မ91 int လiueရ&ရ6ပ7& String ကiu လiuခ(င#တယ#ဆiuရင# String
လiueရ&ရပ?မယ#။

6ပ7&eတ15 Method ထ8က eန1က#ဆuP& Statement ရ8S eန1က#မ91 return ဆiu6ပ7& သတ#မ9တ#ထ1&တ85 Type ရ8S တန#ဖiu&ကiu
Return လuပ#eပ&ရမ91 *ဖစ#ပ?တယ#။ အ85ဒ7 Statement ကiu Return Statement လiu eခLပ?တယ#။ Method ထ8က
Return Statement ရ8S eန1က#မ91 ဘ1ကuဒ#မ9 eရ&သ1&လiu မရBiuင#ပ?ဘR&။

Learning Java 14 မင#&လ>င#


Java Developer Class
တကယ#လiu မiမieရ&သ1&ထ1&တ85 Method ဟ1 Return *ပန#စရ1မလiuဘR&ဆiuရင# Return Type eနရ1ကiu void လiu
eရ&သ1&eပ&ရပ?မယ#။ ဒ?ဆiuရင# အ85ဒ7 Method ထ8မ91 ဘ1တန#ဖiu&မ9 Return လuပ#စရ1မလiueတ15ပ?ဘR&။

ဆက#လက#6ပ7& ဘယ#လiu Method eတ>က Return လuပ#ဖiuလiu6ပ7& ဘယ#လiu Method eတ>က Return လuပ#စရ1မလiuဘR&
လ8 ဆiuတ1ကiu ဆက#လက#6ပ7& စ]#&စ1&သ>1&0ကရeအ1င#။ Utility Function ပ8e*ပ1e*ပ1 Object ရ8S Behavior လiuပ8
e*ပ1e*ပ1 Method eတ>ဆiuတ1 လuပ#eဆ1င#မ-eတ>ကiu eဆ1င#ရ>က#eပ&Biuင#တ85 အစiတ#အပiuင#&eတ> *ဖစ#ပ?တယ#။
ကOန#eတ1#တiu Student Object ကiu အသuP&*ပuBiuင#ဖiu Student Class ကiu eရ&သ1&eနတယ# ဆiu0ကပ?စiu။ အ85ဒ7 Class
ထ8မ91 Instance Variable အeနန8A String Type name ဆiuတ85 Variable Diပ?တယ#။

ဒ7eနရ1မ91 Student Object ကeန name ကiu *ပန#eပ&Biuင#တ85 Behavior န8A Student Object ကiu မiတ#ဆက#စ1ကiu
Console eပLမ91 Print လuပ#Biuင#တ85 Behavior eတ>ကiu ထည#5eပ&ဖiuလiuလ1တ85 အခ?ကiu စ]#&စ1&0ကည#5ပ?။ ကOန#eတ1#တiu
Method eတ>ကiu eရ&ဖiu လiuအပ#လ1ပ?6ပ7။

public class Student {

private String name;

public String getName() {


return name;
}

public void printGreeting() {


[Link](“Hello, I am “ + name);
}
}

အထက#ပ? အတiuင#& Student Object ရ8S name ကiu *ပန#eပ&စရ1လiuတ85 getName() Method မ91eတ15 Return
Type eနရ1မ91 name ရ8S Type *ဖစ#တ85 String ကiu eရ&သ1&ထ1&ရပ?တယ#။ eန1က#6ပ7& Console eပLမ91 Greeting
ကiu Print လuပ#^uPသ1လiuတ85 printGreeting() Method မ91eတ15 Return *ပန#စရ1မလiuတ85 အတ>က# void လiu eရ&သ1&
eပ&ဖiuလiuအပ#ပ?တယ#။ တစ#*ခ1& Language eတ>မ91လiu Return *ပန#စရ1မလiuတ85 အတ>က# ဘ1မ9 မeရ&ပ8ထ1&လiu မရ
ပ?ဘR&။ မ*ပန#ဘR&ဆiuရင#လ8 void ဆiu6ပ7& eရ&eပ&ရမ91 *ဖစ#ပ?တယ#။

ဒ?5e0က1င#5 Method eတ>ကiu eရ&သ1&တ85 အခ?မ91 အ85ဒ7 Method ကiu သuP&6ပ7&တ85 အခ?မ91 တစ#ခuခuကiu *ပန#ရခ(င#တ1လ1&
ဆiuတ1ကiu အရင#ဆuP& စ]#&စ1&ရပ?မယ#။ *ပန#လiuခ(င#တယ#ဆiuရင#လ8 ဘယ# Type မ(iu&ကiu လiuခ(င#တ1လ8ဆiuတ1ကiu ဆuP&*ဖတ#
ရပ?မယ#။ 6ပ7&မ9 လiuခ(င#တ85 Type ကiu Return Type အeနန8A သတ#မ9တ#eပ&ရပ?မယ#။ Return *ပန#မလiuခ(င#eတ15ဘR&ဆiu
ရင#eတ15 void ကiu Return Type အeနန8A eရ&သ1&eပ&ရမ91ပ?။

Return Statement ကiu eရ&သ1&တ85eနရ1မ91လ8 သတ#မ9တ#ထ1&တ85 Return Type န8A ထပ#တRဒ?မ9မဟuတ# Sub Type
eတ>ဆiuရင# Return Value အeနန8A အသuP&*ပuBiuင#ပ?တယ#။ ဆiu0ကပ?စiu Student Class ဟ1 Person Class ရ8S Sub
Class ဆiuရင# Person Type ကiu Return *ပန#လiuတ85 Method ထ8မ91 Student Type ကiu Return Value အeနန8A
အသuP&*ပuBiuင#ပ?တယ#။ အ85ဒ7လiu Sub Type ကiu Return *ပန#Biuင#တ1က Covariant Return လiueခL6ပ7& Java SE 5
ကeန စတင# အသuP&*ပuလ1Biuင#တ1 *ဖစ#ပ?တယ#။

ဒ?eပမ85 လuP&ဝ Type မတRတ85 Value ကiueတ15 Return *ပန#လiuမရပ?ဘR&။

Learning Java 15 မင#&လ>င#


Java Developer Class

Argument List

Method တစ#ခuရ8S Argument ဆiuတ1ဟ1 အ85ဒ7 Method ထ8ကeန အသuP&*ပuဖiu အတ>က#လiuအပ#တ85 eဒတ1eတ>ကiu
ရယRBiuင#ဖiu အတ>က# eရ&သ1&ရတ85 Variable eတ> *ဖစ#0ကပ?တယ#။ ဆiu0ကပ?စiu၊ ကOန#eတ1#တiu int ၂ ခuကiu eပ?င#&6ပ7&
ရလဒ#ကiu *ပန#eပ&Biuင#တ85 Method တစ#ခuကiu eရ&eနတ85 အခ?မ91 int variable ၂ ခuကiu Method ထ8ကeန မသiBiuင#ရင#
eရ&လiu ရမ91 မဟuတ#ပ?ဘR&။ အ85ဒ7 int B9စ#ခuကiu Method Argument အeနန8A eရ&သ1&6ပ7& Method ထ8ကeန အသuP&*ပu
Biuင#မ91 *ဖစ#ပ?တယ#။

ဆက#လက#6ပ7&eတ15 Method Argument eတ>ကiu ဘယ#လiu eရ&ရတယ#ဆiuတ1ကiu eလ5လ1သ>1&0ကရeအ1င#။


method name ရ8S eန1က#မ91 () လiueရ&ထ1&တ85 eနရ1Diပ?တယ#။ အ85ဒ7 “(“ န8A “)” 0က1&မ91 Method အတ>င#&မ91
အသuP&*ပuလiuတ85 Argument eတ>ကiu Variable အeနန8A Declare လuပ#eပ&ရပ?မယ#။ Method Argument eတ>ကiuလ8
မလiuအပ#ပ?က မeရ&ပ8eနBiuင#သလiu၊ လiuအပ#ပ?က တစ#ခuထက#မက Comma ခP6ပ7& eရ&သ1&eပ&ရမ91 *ဖစ#ပ?တယ#။

public class ArgumentDemo {

void noArgument() { // codes }

double makeDouble(double d) {
return d * 2;
}

double add(double a, double b) {


return a + b;
}
}

အထက#ပ?နမRန1ထ8မ91 အတiuင#& Argument eတ>ကiu လiuအပ#သလiu eရ&သ1& အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။

Method Signature and Method Overloading

Method တစ#ခuမ91 အန8ဆuP&eရ&သ1&ဖiu လiuအပ#တ85 အစiတ#အပiuင#&eတ>ကiu eလ5လ16ပ7&တ85 eန1က#မ91 ဆက#လက#6ပ7&


Java Compiler န8A Java Runtime မ91 Method တစ#ခuကiu ဘယ#လiu သieနလ8 ဆiuတ1ကiu eလ5လ1သ>1&0ကပ?မယ#။
Variable မ91တuန#&က Scope တစ#ခuအတ>င#&မ91 Name တRတ85 Variable eတ>ကiu eရ&သ1&လiu မရBiuင#ဘR&ဆiuတ1ကiu
eလ5လ1ခ850ကရမ91ပ?။ ဘ1လiuလ8 ဆiueတ15 Java Compiler န8A Java Runtime မ91 Variable ရ8S Name ကiu 0ကည#56ပ7&
Identifined လuပ#eနတ85 အတ>က# *ဖစ#ပ?တယ#။

ဒ?*ဖင#5ရင# Method eတ>ကiueက1 ဘယ#လiu Identified လuပ#eနတ1လ8 ဆiuတ1ကiu ဆက#6ပ7& eလ5လ1သ>1&0ကရeအ1င#။


Class တစ#ခuထ8မ91 န1မည#တRတ85 Method eတ> eရ&လiuရပ?တယ#။ ဒ?5e0က1င#5 Java Compiler န8A Runtime က
Method တစ#ခuကiu ကiu န1မည# တစ#ခuထ8န8A သတ#မ9တ#eနတ1 မဟuတ#ပ?ဘR& ဆiuတ1ကiu သiBiuင#ပ?တယ#။

Learning Java 16 မင#&လ>င#


Java Developer Class

Compiler န8A Runtime ကeန Method ကiu Identified လuပ#eနတ85 အစiတ# အပiuင#&eတ>ကiu Method Signature လiu
eခLပ?တယ#။ Method Signature မ91 Method Name န8A Argument List တiu ပ?ဝင#0ကပ?တယ#။ Java ဘ1သ1ရပ#
မ91က Method Name တReပမ85 Method Signature မတRရင# Method တစ#ခuစ7လiu သတ#မ9တ#ပ?တယ#။

public class OverloadDemo {

public void takeNote(Memo memo) {


// codes to write note in memo
}

public void takeNote(Tablet tablet) {


// code to write note in tablet
}
}

အထက#ပ? နမRန1ထ8မ91 Method B9စ#ခuလuP& န1မည#တReပမ85 Argument eတ>မတRပ?ဘR& ဒ?5e0က1င#5 Java Compiler
က Method တစ#ခuစ7လiu *မင#ပ?တယ#။ ဒ?ကeတ15 Method Argument Type မတRတ85အတ>က# Method Signature
မတRဘR&လiu သတ#မ9တ#သ>1&တ1ပ?။

public class OverloadDemo {

public void takeNote(Memo memo) {


// codes to write note in memo
}

public void takeNote(Memo memo, Pen pen) {


// code to write note in tablet
}
}

အထက#ပ?အတiuင#& Argument List အeရအတ>က# မတRရင#လ8 Method Signature မတRဘR&လiu သတ#မ9တ#ပ?တယ#။


ဤက85သiu Method Name တR6ပ7& Argument List မတRတ85 Method eတ>ကiu eရ&သ1&*ခင#&ကiu Method
Overloading လiueခLဆiuပ?တယ#။

JRE ထ8မ91ပ?တ85 Java Standard API eတ>မ91လ8 Method Overloading ကiu အမ(1&အ*ပ1& အသuP&*ပueရ&သ1&ထ1&
တ1ကiu eတ>Zရပ?တယ#။

Learning Java 17 မင#&လ>င#


Java Developer Class

Type of Methods

Method eတ>ကiu အသuP&*ပuလiuတ85 ရည#ရ>ယ#ခ(က#အeပLမRတည#6ပ7& အမ(iu&အစ1&ခ>8*ခ1&Biuင#ပ?တယ#။ Global Variable


eတ>မ91တuန#&ကလiuပ8 Object eတ>ရ8S Behavior အeနန8A အသuP&*ပuလiuတ85 Method eတ>ကiueတ15 Instance Method
လiu eခL6ပ7&၊ Class ထ8မ91ပ8ထ1&6ပ7& Utility Function eတ>အeနန8A အသuP&*ပuလiuတ85 Method eတ>ကiueတ15 Static
Method eတ>လiueခLပ?တယ#။

Instance Method
Instance Method eတ>ဟ1 Object eတ>ရ8S Behavior အeနန8A အသuP&*ပuလiuတ85 အခ?မ91 eရ&သ1&ရတ85 Method
eတ> *ဖစ#ပ?တယ#။ Class တစ#ခuကiu eရ&သ1&ရတ85 အဓiက ရည#ရ>ယ#ခ(က#ဟ1 Object eဆ1က#ဖiu *ဖစ#တ85 အတ>က#
Variable eတ>တuန#&ကလiuပ8 Class တစ#ခuအတ>င#&မ91 Default အတiuင#&eရ&သ1&ထ1&တ85 Method eတ>ဟ1
Instance Method eတ> *ဖစ#0ကပ?တယ#။

Instance Method eတ>ဟ1 Object eတ>န8A သက#ဆiuင#တ85 Method eတ> *ဖစ#တ85 အတ>က# အသuP&*ပuလiuတ85 အခ?မ91
Object eတ>ကတဆင#5 အသuP&*ပuရမ91 *ဖစ#ပ?တယ#။ ထiuအ*ပင# Object eတ>ရ8S ကiuယ#ပiuင# State eတ>*ဖစ#တ85 Instance
Variable eတ>ကiuလ8 Instance Method ထ8ကeန Access လuပ#6ပ7& အသuP&*ပuBiuင#ပ?တယ#။ တဖန# Class တစ#ခu
အတ>င#&မ91Diတ85 Instance Method အခ(င#&ခ(င#&လ8 အသuP&*ပuBiuင#ပ?တယ#။

public class InstanceMethodDemo {

private String format = “Hello %s%n”;

public void showMessage(String name) {


[Link](format, name);
}

public void showMessages(String [] names) {


for(String name : names) {
showMessage(name);
}
}

public static void main(String [] args) {


InstanceMethodDemo demo = new InstanceMethodDemo();
[Link](“Java”);

String [] array = {“Java”, “Programming”};


[Link](array);
}

Learning Java 18 မင#&လ>င#


Java Developer Class
အထက#ပ? InstanceMethodDemo Class ထ8မ91 eရ&သ1&ထ1&တ85 showMessage() Method န8A
showMessages() Method eတ>ဟ1 static ဆiuတ85 Non Access Modifier မပ?တ85 အတ>က# Instance Method
eတ> *ဖစ#0ကပ?တယ#။ တဖန# main method မ91eတ15 static ဆiuတ85 Non Access Modifier ပ?တ85 အတ>က# Static
Method *ဖစ#ပ?တယ#။ Static Method န8A ပတ#သက#6ပ7& eန1က# အခန#&မ91 အeသ&စiတ# eဖL*ပသ>1&ပ?မယ#။

ဒ7eနရ1မ91eတ15 Instance Method eတ>ကiu ဘယ#လiu eရ&ရမယ#ဆiuတ1န8A Instance eတ>ကiu ဘယ#လiu eခLသuP&ရမယ#
(Invoke) ဆiuတ1ကiuပ8 eလ5လ1သ>1&0ကပ?မယ#။ Instance Method eတ>ဟ1 Object eတ>န8A ဆiuင#တ85 Method eတ>
*ဖစ#တ85 အတ>က# အ*ခ1& Class ထ8ကeခLတ85အခ?၊ ဒ?မ9မဟuတ# Main Method လiu Static Method ထ8ကeန eခLခ(င#
တ85 အခ?eတ>မ91 Object eဆ1က#6ပ7& Object ကeနပ8 eခLလiuရမ91 *ဖစ#ပ?တယ#။

ဒ?5e0က1င#5 အထက#ပ? နမRန1ထ8မ91 Instance Method eတ>ကiu Main Method ထ8ကeန Invoke လuပ#ခ(င#တ85
အတ>က# demo ဆiuတ85 Object ကiu အရင#တည#eဆ1က#6ပ7&က1မ9 [Link](“Java”) ဆiu6ပ7& Invoke
လuပ#eနတ1*ဖစ#ပ?တယ#။ [Link](array) လiu eရ&ထ1&တ1လ8 Instance Method *ဖစ#တ85 အတ>က#
Object ကeန Invoke လuပ#eနရတ1*ဖစ#ပ?တယ#။

ဒ?eပမ85 Class တစ#ခuထ8မ91Diတ85 Instance Method အခ(င#&ခ(င#& Invoke လuပ#လiuတ85အခ?မ91eတ15 Object တစ#ခuထ8
မ91 သuP&မည#5 Method အခ(င#&ခ(င#& *ဖစ#တ85အတ>က# Object eဆ1က#စရ1မလiuအပ#ပ8 အသuP&*ပuလiuရeနတ1*ဖစ#တယ#။
showMessages() Method ထ8မ91 showMessage(name) လiueခLတ85အခ?မ91 Object eဆ1က#စရ1မလiuတ1ကiu
eတ>Zရပ?မယ#။

ဆက#လက#6ပ7& Instance Method ထ8ကeနဘ1eတ>ကiu Access လuပ#လiuရလ8ဆiuတ1ကiu eလ5လ1သ>1&0ကရeအ1င#။

public class AccessDemo {


private static String FORMAT = “Hello %s”;
private String name;

public void setName(String name) {


[Link] = name;
}

public static String changeUpper(String str) {


return [Link]();
}

public void greet() {


String message = [Link](FORMAT, name);
String upperMessage = changeUpper(message);
[Link](upperMessage);
}
}

အထက#ပ?နမRန1ထ8မ91 0ကည#5ရင#သiBiuင#သလiu greet() method ဟ1 Instance Method တစ#ခu*ဖစ#ပ?တယ#။ အ85ဒ7


Method ထ8မ91 FORMAT ဆiuတ85 Static Variable ကiuလ8 အသuP&*ပuBiuင#ပ?တယ#။ တဖန# name ဆiuတ85 Instance
Variable ကiuလ8 အသuP&*ပuBiuင#သလiu changeUpper() ဆiuတ85 Static Method ကiuလ8 အသuP&*ပuBiuင#ပ?တယ#။

ဒ?5အ*ပင# eUမ91eဖL*ပခ85သလiu Instance Method ထ8ကeန အ*ခ1&eသ1 Instance Method eတ>ကiuလ8 အသuP&*ပuBiuင#
ပ?တယ#။

Learning Java 19 မင#&လ>င#


Java Developer Class

Static Method
Class ထ8မ91 ထ1&6ပ7& Utility Function eတ>အeနန8A အသuP&*ပuခ(င#တ85 Method eတ>ကiu Static Method အeနန8A
eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။ Method eတ>ကiu Default အတiuင#&eရ&သ1&ထ1&မယ#ဆiuရင# Instance Method eတ>
*ဖစ#ကuန#ပ?တယ#။ Static Method အ*ဖစ# အသuP&*ပuလiuပ?က static ဆiuတ85 Non Access Modifier ကiu Return Type
ရ8S eUမ91 eရ&သ1&eပ&ရမ91 *ဖစ#ပ?တယ#။

ဆက#လက#6ပ7& Static Method eတ>ကiu ဘယ#လiueရ&မလ8၊ Static Method eတ>ကiu ဘယ#လiu Invoke လuပ#မလ8 ဆiu
တ1န8A Static Method eတ>က အသuP&*ပuBiuင#တ1eတ>က ဘ1eတ>လ8 ဆiuတ1ကiu eလ5လ1သ>1&0ကပ?မယ#။

public class StaticMethodDemo {


private static String FORMAT = “Hello %s%n”;

public static void show(String message) {


[Link](FORMAT, message);
}

public static void show(String [] messages) {


for(String m : messages) {
show(m);
}
}
}

အထက#ပ? ကuဒ#နမRန1ထ8မ91 eရ&သ1&ထ1&တ85 show() Method B9စ#ခuလuP&မ91 static ဆiuတ85 Modifier ကiu eရ&သ1&
ထ1&တ85 အတ>က# Method B9စ#ခuလuP& Static Method eတ> *ဖစ#0ကပ?တယ#။ အeပLက show() Method ထ8မ91eတ15
FORMAT ဆiuတ85 Static Variable ကiu အသuP&*ပuထ1&ပ?တယ#။ Static Method eတ>ဟ1 Class တစ#ခuထ8မ91 Diတ85
Static Member eတ>ကiu အသuP&*ပuBiuင#ပ?တယ#။

ဒuတiယ show() Method ထ8မ91eတ15 for loop ထ8မ91 Static Method eန1က#တစ#ခuကiu eခLသuP&eနပ?တယ#။ Class
တစ#ခuထ8မ91Diတ85 Member အခ(င#&ခ(င#&ဆiuပ?က Owner ကiu eရ&သ1&စရ1မလiuပ8 တiuက#^iuက# eရ&သ1& အသuP&*ပuBiuင#ပ?
တယ#။ ဆက#လက#6ပ7& အ*ခ1& Class တစ#ခuကeန Static ကiu အသuP&*ပuပuPကiu eလ5လ10ကရeအ1င#။

public class StaticMethodTest {

public static void main(String [] args) {


String [] array = {“Static Method”, “Java”};
[Link](array);
}
}

StaticMethodTest Class ထ8ကeန StaticMethodDemo Class ရ8S Static Method ကiu အသuP&*ပuမယ#ဆiuရင#
eတ15 Owner *ဖစ#တ85 Class ကeန တဆင#5 eခLယRအသuP&*ပuရမ91 *ဖစ#ပ?တယ#။

Learning Java 20 မင#&လ>င#


Java Developer Class
တစ#ခuသတiထ1&ဖiuလiuအပ#တ1က Static Method eတ>ဟ1 Class တစ#ခuထ8မ91Diတ85 အ*ခ1& Static Method eတ>န8A
Static Variable eတ>ကiu အသuP&*ပuBiuင#ပ?တယ#။ ဒ?eပမ85 အ*ခ1& Instance Method eတ> Instance Variable eတ>
ကiueတ15 အသuP&*ပuလiu မရပ?ဘR&။

Constructor

Constructor ဆiuတ1 Class ကeန Object ကiu တည#eဆ1က#တ85အခ?မ91 အသuP&*ပuတ85 Special Method တစ#မ(iu&
*ဖစ#ပ?တယ#။ Constructor eတ>ကiu Object တစ#ခuကiu တည#eဆ1က#တ85 eနရ1မ91 တစ#Xကiမ#သ1 အသuP&*ပu6ပ7& Object
*ဖစ#သ>1&6ပ7&ရင#eတ15 Constructor eတ>ကiu အသuP&မ*ပueတ15ပ?ဘR&။ Constructor eတ>ကiu Instance Variable eတ>
ကiu Initialize လuပ#တ85အခ?မ91 အသuP&*ပuဖiu *ပင#ဆင#ထ1&တ1 *ဖစ#ပ?တယ#။

Constructor ရ8S န1မည#ဟ1 Class န1မည#န8A ထပ#တRတRည7ရပ?မယ#။ 6ပ7&eတ15 Constructor မ91 Return Type eရ&
လiuမရပ?ဘR&။ Class တစ#ခuအတ>က# Constructor ကiu eရ&သ1&တ85 အခ?မ91 Argument List မတRတ85 Construcotr
eတ>ကiu Overload လuပ#6ပ7& eရ&သ1&Biuင#ပ?တယ#။

Default Constructor
eအ1က#eဖL*ပပ?ကuဒ#ထ8မ91လiu new Student() ဆiu6ပ7&eရ&သ1&လiuက#တ1ဟ1 Student Class ရ8S Constructor ကiu
လ9မ#&eခL6ပ7& Object ကiu တည#eဆ1က#လiuက#တ1ပ8 *ဖစ#ပ?တယ#။

public class Student {


private String name;

public static void main(String [] args) {


Student student = new Student();
}
}

ဒ?eပမ85 eသeသ*ခ1*ခ10ကည#5မယ#ဆiuရင# Student Class ထ8မ91 Student() ဆiuတ85 Constructor ကiu eရ&မထ1&ပ?
ဘR&။ ဒ?eပမ85 new Student() ဆiu6ပ7& eရ&လiuရeနပ?တယ#။ ဒ?ဟ1 Student Class ကiu Compile လuပ#တ85အခ?မ91
Java Compiler ကeန Constructor မပ?တ85 Class eတ>ဆiuရင# Default Constructor တစ#ခuကiu ထည#5eပ&eနလiuပ8
*ဖစ#ပ?တယ#။

public Student() {
super();
}

Default Constructor eတ>ဟ1 public *ဖစ#6ပ7& Argument မပ?ပ?ဘR&။ 6ပ7&eတ15 အတ>င#&ပiuင#&မ91 super() ဆiuတ85 ကuဒ#
တစ#e0က1င#&ကiu eရ&သ1&ထ1&မ91 *ဖစ#ပ?တယ#။ တကယ#လiuသ1 မiမiတiueရ&သ1&ထ1&တ85 Class အတ>င#&မ91
Constructor တစ#ခuခuကiu eရ&သ1&ထ1&6ပ7ဆiuရင#eတ15 Compiler ကeန Default Constructor ကiu ထည#5eပ&eတ15
မ91 မဟuတ#ပ?ဘR&။

Learning Java 21 မင#&လ>င#


Java Developer Class

Constructor ကiuဘ-လiueရ1ရတ-လ9
Constructor eတ>ဟ1 Class ကeန Object ကiu တည#eဆ1က#တ85အခ?မ91 မ*ဖစ#မeနeခLဆiuရမည#5 Special
Method eတ>*ဖစ#6ပ7&eတ15 Instance Varable eတ>န8A Instance Method eတ>ကiu အသuP&*ပuBiuင#ပ?တယ#။ ဒ?5e0က1င#5
Object eတ>ရ8S Initial State eတ>ကiu သတ#မ9တ#လiuတ85အခ?မ91 ကiuယ#တiuင# Constructor ကiu eရ&သ1&ဖiuစ]#&စ1&ရပ?
eတ15မယ#။

public class Student {


private String name;

public Student() {
[Link] = “Aung Aung”;
}
}

eဆ1က#သမT Object တiuင#&ရ8S name ဟ1 Aung Aung *ဖစ#သ>1&မယ#လiu e*ပ1စရ1Dieပမ85၊ အထက#ပ? နမRန1ထ8မ91လiu
Student ရ8S name ကiu Initialize လuပ#လiuတ85အခ?မ(iu&မ91 Conatructor ကiu အသuP&*ပuBiuင#ပ?တယ#။ Object မeဆ1က#မ97
Instance Variable eတ>ကiu Initialize လuပ#ဖiuအတ>က# Instance Block eတ>ကiuလ8 အသuP&*ပuBiuင#ပ?တယ#။

public class Student {


private String name;

{
[Link] = “Aung Aung”;
}
}

Instance Block eတ>ဟ1လ8 Object eတ>ကiu စတင#တည#eဆ1က#စ]# Constructor eတ>ကiu အလuပ#မလuပ#ခင#


အလuပ#လuပ#Biuင#ပ?တယ#။ အ85ဒ7အတ>က# Instance Variable eတ>ကiu Initialize လuပ#တ1eတ>ကiu eရ&သ1&Biuင#ပ?တယ#။
ဒ?*ဖင#5 Constructor န8A Instance eတ>ကiu ဘယ#လiu eရ>&ခ(ယ# အသuP&*ပuရမလ8။

public class Student {


private String name;

public Student(String name) {


[Link] = name;
}
}

Constructor eတ>က Initialize လuပ#လiuတ85 တန#ဖiu&eတ>ကiu Constructor Argument eတ>ကလက#ခPရယRBiuင#eပမ85


Instance Block eတ>ကeတ15 Argument eတ>ကiu လက#ခPလiu မရBiuင#ပ?ဘR&။

Learning Java 22 မင#&လ>င#


Java Developer Class

Constructor Overloading
Class တစ#ခuအတ>င#&မ91 Argument List မတRတ85 Constructor eတ>ကiu eရ&သ1&*ခင#&ကiu Constructor
Overloading လiueခLပ?တယ#။ Constructor ဆiuတ1က Class ကeန Object ကiu တည#eဆ1က#တ85 အခ?မ91
အသuP&*ပu6ပ7& Instance Variable eတ>ကiu Initialize လuပ#လiuတ85 အခ?မ91 အသuP&*ပueလ5Diပ?တယ#။

Object eတ>ကiu တန#ဖiu&အမ(iu&မ(iu&eပ&6ပ7& Object eဆ1က#လiuတ85 အခ?မ91 Constructor Overloading ကiu အသuP&*ပu
Biuင#ပ?တယ#။ Student Object မ91 name န8A age ကiu instance variale အeနန8A ပiuင#ဆiuင#တယ# ဆiu0ကပ?စiu။ Student
Object ကiu ဘ1 Argument မ9 မeပ&ပ8တည#eဆ1က#လiuတ85 အခ?လ8Diမယ#။ 6ပ7&eတ15 name Xက7&ပ8 eပ&6ပ7&eဆ1က#
ခ(င#တ85အခ?လ8 Diမယ#၊ age Xက7&ပ8 eပ&6ပ7&eဆ1က#ခ(င#တ85 အခ?လ8Diမယ#။ name eက1 age ပ?eပ&6ပ7& eဆ1က#ခ(င#တ85
အခ?လ8Diပ?တယ#။ ဒ7လiu အခ?မ(iu&မ91 Constructor Overloading ကiu အသuP&*ပu6ပ7& Constructor eတ>ကiu *ပင#ထ1&Biuင#
ပ?တယ#။

public class Student {


private String name;
private int age;

public Student() {}

public Student(String name) {


[Link] = name;
}

public Student(int age) {


[Link] = age;
}

public Student(String name, int age) {


[Link] = name;
[Link] = age;
}
}

တကယ#လiu ဘ1 Argument မ9 မeပ&ပ8 Object eဆ1က#ခ(င#ရင# Student s = new Student() လiu eရ&သ1&Biuင#ပ?
တယ#။ 6ပ7&eတ15 Name ကiu eပ&6ပ7& Object ကiu တည#eဆ1က#လiuလ(င# Student s = new Student(“Aung Aung”)
လiu eရ&သ1&Biuင#ပ?တယ#။

Constructor eတ>ကiu *ပင#ဆင#ထ1&သeလ1က# Object eတ>ကiu ပuPစPအမ(iu&မ(iu&န8A တည#eဆ1က#Biuင#မ91 *ဖစ#ပ?တယ#။

Learning Java 23 မင#&လ>င#


Java Developer Class

Visibility and Accessibility


Java ဘ1သ1ရပ#မ91 Class eတ>အခ(င#&ခ(င#& eတ>Z*မင#Biuင#မ- (Visibility) န8A အ*ခ1& Class တစ#ခuကeန အ*ခ1& Class
တစ#ခuရ8S Member eတ>ကiu သuP&Biuင#သလ1& မသuP&Biuင#ဘR&လ1& (Accessibility) ကiu ခ>8*ခ1&သတ#မ9တ#Biuင#eအ1င# *ပင#ဆင#
ထ1&ပ?တယ#။

Class eတ>အခ(င#&ခ(င#&ရ8S Visibility န8A Accessibility ကiu Package eတ>န8A Access Modifier တiuကiu အသuP&*ပu6ပ7&
သတ#မ9တ# eရ&သ1&Biuင#ပ?တယ#။

Packages
Package ဆiuတ1က Java Programming ဘ1သ1ရပ#မ91 အသuP&*ပueနတ85 Name Space န8A Visibility ကiu
သတ#မ9တ#eပ&Biuင#တ85 နည#&လမ#&တစ#မ(iu&*ဖစ#ပ?တယ#။ အလ>ယ#e*ပ1ရမယ#ဆiuရင# Packaege ဆiuတ1ဟ1 Computer
ထ8က Folder တစ#ခuန8A ဆင#တRပ?တယ#။ Folder တစ#ခuထ8မ91 န1မည#တR File B9စ#ခuကiu တည#eဆ1က#လiu မရBiuင#သလiu
Java မ91လ8 တစ#eနရ1ထ8မ91 Class B9စ#ခuကiu တည#eဆ1က#လiu မရBiuင#ပ?ဘR&။

တကယ#လiu Package သ1မDiခ85ရင# Java Class eတ>ကiu န1မည#eပ&တ85 eနရ1မ91 အeတ1#eလ&ခက#ခ8သ>1&မ91 *ဖစ#ပ?
တယ#။ ဒ7လiueနရ1မ(iu&မ91 Package eတ>ကiu အသuP&*ပu6ပ7& Class eတ>ကiu ခ>8*ခ1&သတ#မ9တ#eပ&Biuင#ပ?တယ#။ Folder မ
တRရင#eတ15 န1မည#တR File eတ>ကiu eရ&သ1လiuရသလiuပ8 Package မတRရင#eတ15 န1မည#တR Java Class eတ>ကiu
eရ&သ1&လiuရလ1Biuင#ပ?တယ#။

ဒ?5အ*ပင# Java Package eတ>ဟ1 Class eတ> အခ(င#&ခ(င#&ရ8S Visibility ကiuလ8 သတ#မ9တ#eပ&Biuင#ပ?တယ#။

Writing a Class in a Package

Class တစ#ခuကiu Package ထ8မ91 eရ&သ1&လiuပ?က Package Statement ကiu


eရ&သ1& သတ#မ9တ#Biuင#ပ?တယ#။
Package Statement eတ>ကiu eရသ1&တ85 eနရ1မ91 Source File eတ>ရ8S ထiပ#ဆuP&
မ91 eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။ Package Statement ရ8S eUမ91 Source Code
မဟuတ#တ85 Comment eတ>ကiu eရ&သ1မယ#ဆiuရင#eတ15 eရ&သ1&Biuင#ပ?တယ#။
ဒ?eပမ85 Source Code eတ>ရ8S ထiပ#ဆuP&မ91eတ15 Package Statement ကiu
eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။ တဖန# Source File တစ#ခuမ91လ8 Package
Statement တစ#ခuကiu eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။

Package Statement eတ>ကiu eရ&သ1&တ85 eနရ1မ91 Reverse Domain Name


ကiu အသuP&*ပueလ5Di6ပ7& ရည#ရ>ယ#ခ(က#တRတ85 Class eတ>ကiu Package အလiuက#
ထ1&Dieလ5Diပ?တယ#။ Folder eတ>ထ8မဟ1 Sub Folder eတ>ကiu ထ1&DiBiuင#သလiu
Package eတ>eအ1က#မ91လ8 Sub Package eတ>ကiu ထ1&DiBiuင#ပ?တယ#။

[Link] လiueရ&သ1&ထ1&ရင# com ဆiuတ85 package eအ1က#မ91 jdc


package Di6ပ7& ၎င#&ရ8S eအ1က#မ91 entity package Diတယ#လiu မ9တ#ယRBiuင#ပ?
တယ#။ Package တစ#ခuန8A တစ#ခuအ0က1&မ91 [.] ကiu ခP6ပ7& eရ&သ1&ရမ91 *ဖစ#ပ?
တယ#။

Learning Java 24 မင#&လ>င#


Java Developer Class

// JDC Online Registration System


package [Link];

public class Student {

အထက#ပ?အတiuင#& eရ&သ1&ထ1&ပ?က [Link] Package eအ1က#မ91 Student Class ကiu တည#eဆ1က#


eပ&သ>1&မ91 *ဖစ#ပ?တယ#။

Visibility of Classes
Program eတ>ကiu eရ&သ1&တ85 eနရ1မ91 Class eတ>ကiu တစ#ခuထက#မက အသuP&*ပu6ပ7& eရ&သ1&eလ5Diပ?တယ#။
Object တစ#ခuရ8S Member အeနန8A အ*ခ1& Object eတ>ကiu အသuP&*ပueနBiuင#ပ?တယ#။ တနည#&ဆiuရမယ# ဆiuရင# Class
တစ#ခuကiu တစ#*ခ1& Class eတ>ထ8မ91 Variable eတ>ရ8S Type အeနန8AအသuP&*ပueနBiuင#ပ?တယ#။ ဒ7လiu အသuP&*ပuတ85
eနရ1မ91 အသuP&*ပueနတ85 Class ကiu လက#Di အသuP&*ပueနတ85 Class ကeန *မင#eနမ9သ1 အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။

အထက#ပ? Diagram ထ8မ91 [Link] Package eအ1က#မ91 entity န8A service Package တiuDiပ?တယ#။ တဖန#
entity Package ထ8မ91 Student, Course န8A Registration Class တiuDi0က6ပ7& service Package ထ8မ91
RegistrationService Class တiuDi0ကပ?တယ#။

Package eတ>ဟ1 Class eတ>ရ8S Visibility ကiu သတ#မ9တ#eပ&Biuင#တ1 *ဖစ#တ85 အတ>က# Package မတReတ15တ1န8A
Class eတ>ရ8S Visibility ကလ8 က>8*ပ1&သ>1&မ91 *ဖစ#ပ?တယ#။ Package တစ#ခuအတ>င#&မ91Diမယ#ဆiuရင# Class eတ>
ဟ1 အခ(င#&ခ(င#&*မင#နရမ91 *ဖစ#တ85အတ>က# Registration ထ8ကeန Student န8A Course တiuကiu *မင#eနရမ91 *ဖစ#ပ?
တယ#။
Learning Java 25 မင#&လ>င#
Java Developer Class

package [Link];

public class Registration {


private Student student;
private Course course;
}

Registration, Student န8A Course eတ>ဟ1 Package တစ#ခuအတ>င#&မ91Diတ85 အတ>က# Student န8A Course တiuကiu
Registration ထ8မ91 ဒ7အတiuင#&ပ8 eရ&သ1&Biuင#မ91 *ဖစ#ပ?တယ#။

package [Link];

public class RegistrationService {

public void create(Registration r) {


// codes
}
}

RegistrationService ဟ1 Registration Class ကiu Method Argument အeနန8A အသuP&*ပuလiuပ?တယ#။ ဒ?eပမ85


RegistrationService ဟ1 [Link] Package eအ1က#မ91Di6ပ7& Registration ဟ1 [Link]
Package eအ1က#မ91Diတ85 အတ>က# *မင#မeနBiuင#ပ?ဘR&။ တစ#*ခ1& Package eအ1က#က Class eတ>ကiu အသuP&*ပuလiu
ပ?က Class eတ>ကiu Qualified Name န8A eရ&သ1& အသuP&*ပuBiuင#ပ?တယ#။

package [Link];

public class RegistrationService {

public void create([Link] r) {


// codes
}
}

Qualified Name ကiu eရ&သ1&တ85 eနရ1မ91 Package Name န8A Class Name တiuကiu တ>86ပ7& eရ&သ1&ရမ91 *ဖစ#ပ?
တယ#။ Qualified Name ကiueရ&တ85eနရ1မ91 Type ကiu eရ&တ85 eနရ1တiuင#& Package Name eတ>ပ? eရ&eနရမ91
*ဖစ#တ85 အတ>က# ကuဒ#eတ>မ(1&လ1eလeလ Package Name eတ>မ(1&လ16ပ7& ကuဒ#eတ>ကiu ဖတ#ရတ1 လ>ယ#ကRမ91
မဟuတ#eတ15ပ?ဘR&။

တစ#*ခ1& Package ထ8မ91 Diတ85 Class eတ>ကiu Packate တစ#ခuထ8မ91 Diသက85သiu အသuP&*ပuလiuပ?က Import
Statement eတ>ကiu eရ&သ1& အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။

Learning Java 26 မင#&လ>င#


Java Developer Class

Import Statements
Import Statement eတ>ကiu Package Statement ရ8S eအ1က# Class Declaration ရ8S အeပLမ91 eရ&သ1&ရမ91
*ဖစ#ပ?တယ#။ Import Statement eတ>ကiu Class ရ8S eအ1က#မ91တiu Class ထ8မ91တiu eရ&လiu မရပ?ဘR&။ Import လuပ#
လiuတ85 Class ကiu Qualified န8A eရ&သ1&ရမ91 *ဖစ#6ပ7&၊ Import လuပ#6ပ7&တ1န8A Package Name eရ&စရ1မလiueတ15ပ8
Class Name န8A Type eတ>ကiu အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။ Import Statement eတ>ကiu eရ&သ1&တ85 eနရ1မ91
import [Qualified Name] လiu eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။

package [Link];

import [Link];

public class RegistrationService {

public void create(Registration r) {


// codes
}
}

အထက#ပ?အတiuင#& Registration Class ကiu Import လuပ#ထ1&ပ?က Package Name ကiu မeရ&ပ8 Registration
Class ကiu Package တစ#ခuထ8မ91Diတ85 အတiuင#& eရ&သ1&လiuရမ91 *ဖစ#တယ#။ အ*ခ1&eနရ1မ91 Type အ*ဖစ#သuP&မယ#ဆiu
ရင#လ8 eရ&စရ1လiueတ15ပ?ဘR&။တဖန# Package တစ#ခuထ8က Class eတ>ကiu Import လuပ#ခ(င#တယ#ဆiuရင# Wild
Card (*) ကiu အသuP&*ပu6ပ7&လ8 Import လuပ#Biuင#မ91 *ဖစ#ပ?တယ#။

package [Link];

import [Link].*;

public class StudentService {

public void create(Student student) {


// codes
}

public List<Registration> findRegistration(Student s) {


// codes and return statements
}
}

အထက#ပ?အတiuင#& [Link].* လiu Wild Card ကiu အသuP&*ပu6ပ7& Import လuပ#ထ1&မယ#ဆiuရင# [Link]
Package eအ1က#က Class eတ>အ1&လuP&ကiu Import လuပ#Biuင#မ91 *ဖစ#ပ?တယ#။ ဒ?eပမ85 တစ#ခuသတiထ1&ဖiu လiuတ1က
Wild Card Import ဟ1 Current Package eအ1က#မ91Diတ85 Class eတ>ကiuသ1 Import လuပ#Biuင#တ1*ဖစ#6ပ7& Sub
Package eအ1က#က Class eတ>ကiueတ15 Import လuပ#Biuင#မ91 မဟuတ#ပ?ဘR&။

Learning Java 27 မင#&လ>င#


Java Developer Class

Import လuပ)စရ-မလiueသ- Class မ/-1


ကOန#eတ1#တiu eUမ91 Type အeနန8A String တiu [Link] တiu ကiu eရ&သ1&ခ850ကပ?တယ#။ ဒ7eနရ1မ91 eမ&
စရ1Diလ1ပ?တယ#။ အ85ဒ7 String တiu System တiuကeက1 တစ#*ခ1& Package ထ8မ91 မDiလiuလ1&လiu။ ဟuတ#ပ?တယ#
String တiu System တiuဟ1 [Link] Package eအ1က#က Class eတ>*ဖစ#ပ?တယ#။

Java Language ကiu eရ&သ1&တ85eနရ1မ91 အသuP&မ(1&တ85 Class eတ> Interface eတ> Enum eတ>ကiu [Link]
Package eအ1က#မ91 eရ&သ1&ထ1&0ကပ?တယ#။ 6ပ7&eတ15 [Link] Package eအ1က#က Class eတ>ကiu Import
လuပ#စရ1မလiuပ8 eရ&သ1&Biuင#eအ1င# aခင#&ခ(က# အeနန8A ထ1&eပ&ထ1&ပ?တယ#။ ဒ?5e0က1င#5 String တiu System တiuကiu
eရ&သ1&တ85 eနရ1မ91 Import လuပ#စရ1မလiuပ8 eရ&သ1&Biuင#တ1 *ဖစ#ပ?တယ#။

Primitive Type ရ8S Wrapper Class eတ>၊ String န8A StringBuffer, StringBuilder Class eတ>၊ အသuP&မ(1&တ85
Exception Class eတ>ကiu [Link] Package eအ1က#မ91ထ1&Diတ85 အတ>က# အ85ဒ7 Class eတ>ကiu အသuP&*ပuမယ#
ဆiuရင# Import လuပ#စရ1မလiuအပ#ပ?ဘR&။

Static Import
Java SE 5 အeရ1က#မ91 Static Variable eတ>န8A Static Method eတ>ကiuလ8 Static Import Statement ကiu
အသuP&*ပu6ပ7& Import လuပ#လiuရလ1ပ?တယ#။ import static [QualifiedName of Class].[member] ဆiu6ပ7& eရ&သ1&
ရမ91 *ဖစ#ပ?တယ#။ Static Import Statement eတ>မ91လ8 Wild Card ကiu အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။

import static [Link];


import static [Link].MAX_VALUE;
import static [Link].*;

public class StaticImportDemo {

public static void main(String [] args) {

int maxInt = MAX_VALUE;


int psInt = parseInt("100");
double maxDouble = MAX_VALUE;

[Link](MAX_VALUE);
}
}

Static Member eတ>ကiu Import လuပ#ထ1&*ခင#&အ1&*ဖင#5 တစ#*ခ1& Class ထ8မ91Diတ85 Static Variable eတ>န8A Static
Method eတ>ကiu မiမi Class ထ8က Member eတ>လiu Owner *ဖစ#တ85 Class Name ကiu eရ&စရ1မလiuပ8 eရ&သ1&Biuင#
မ91 *ဖစ#ပ?တယ#။ အထက#ပ? နမRန1ထ8မ91လiu Integer.MAX_VALUE န8A Wild Card န8A Import လuပ#ထ1&တ85
Double.MAX_VALUE တiuDiခ85မယ#ဆiuရင# အတiအက( Import လuပ#ထ1&တ85 Integer.MAX_VALUE ကiu အသuP&*ပu
သ>1&မ91 ပ?တယ#။ double maxDouble = MAX_VALUE လiu double variable မ91 အစ1&ထiu&မယ#ဆiuရင#eတ15
Wild Card ထ8က Double.MAX_VALUE ကiu အသuP&*ပuမ91 *ဖစ#ပ?တယ#။

Learning Java 28 မင#&လ>င#


Java Developer Class

Access Modifiers
Class eတ>န8A Class Members eတ>ရ8S Access လuပ#Biuင#ခ>င#5ကiu သတ#မ9တ#eပ&eနတ1ကeတ15 Access Modifier
eတ>ပ8 *ဖစ#0ကပ?တယ#။ Class eတ>န8A Member eတ>မ91 Access Modifier eတ>ကiu မeရ&သ1&ထ1&ရင# အ85ဒ7 Class
တည#Dieနတ85 Package ထ8မ91သ1 အသuP&*ပuလiuရမ91 *ဖစ#ပ?တယ#။

တကယ#လiu တစ#*ခ1& Package eတ>ကပ? အသuP&*ပueစခ(င#တ85အခ? ဒ?မ9မဟuတ# Package တစ#ခuထ8မ91 Diတ85 Class
eတ> အခ(င#&ခ(င#& Access လuပ#ပiuင#ခ>င#5ကiu သတ#မ9တ#လiuတ85 အခ?မ(iu&မ91 Access Modifier eတ>ကiu သuP&6ပ7& သတ#မ9တ#
eရ&သ1&Biuင#မ91 *ဖစ#ပ?တယ#။

Types of Access Modifier

Modifiers Descriptions
default Access Modifier အ1& eရ&သ1&ထ1&*ခင#&မDiပ?က Package အတ>င#&Di Class
မ(1&မ9 ဆက#သ>ယ#Biuင#မည#

public မည#သည#5eနရ1မ9 မဆiu ဆက#သ>ယ# အသuP&*ပuBiuင#မည#

protected ထiu Class အ1& Inheritance လuပ#ထ1&eသ1 Class မ(1&မ9 ဆက#သ>ယ#Biuင#မည#

private ထiu Class အတ>င#&မ9 တ*ခ1& ဆက#သ>ယ#Biuင#မည#မဟuတ#eပ

Class eတ>န8A Class Member eတ>*ဖစ#0ကတ85 Variable eတ>၊ Method eတ>၊ Constructor eတ>န8A အ*ခ1& Nested
Member eတ>မ91 Access Modifier ကiu eရ&သ1&6ပ7& အသuP&*ပuပiuင#ခ>င#5ကiu သတ#မ9တ#eပ&Biuင#ပ?တယ#။ ဘ1e0က1င#5
Access လuပ#ပiuင#ခ>င#5ကiu လiuအပ#ရတ1လ8 ဘယ#လiueရ&သ1&သင#5သလ8န8A ပတ#သက#6ပ7&eတ15 OOP ရ8S Encapsulation
အe0က1င#&e*ပ1တ85 အခ?မ91လ8 ထပ#မPeဖL*ပသ>1&မ91 *ဖစ#ပ?တယ#။

Default Access

Class eတ>န8A Class Members eတ>မ91 Access Modifier eတ>ကiu eရ&သ1&ထ1&*ခင#& မDiပ?က Default Access
ကiu ရDiမ91 *ဖစ#ပ?တယ#။ Package တစ#ခuအတ>င#&မ91 Diတ85 အ*ခ1& Class eတ>ကeန ဆက#သ>ယ# အသuP&*ပuBiuင#မ91 *ဖစ#
ပ?တယ#။ ဒ?eပမ85 အ*ခ1& Package eတ>ရ8S Class eတ>ကeနeတ15 *မင#ရမ91 မဟuတ#တ85 အတ>က# အသuP&*ပuခ>င#5 Diမ91
မဟuတ#ပ?ဘR&။

package [Link];

class StringConverter {

String getUnicode(String str) {


// codes and return statement
}
}

အထက#ပ?အတiuင#& StringConverter Class မ91 Access Modifier ကiu eရ&မထ1&ပ?က Default Access *ဖစ#eန
6ပ7& [Link] Package အတ>င#&မ91Diတ85 အ*ခ1& Class eတ>ကသ1 အသuP&*ပuBiuင#မ91 Diပ?တယ#။ ဒ7လiu

Learning Java 29 မင#&လ>င#


Java Developer Class
Default Access eတ>ကiu Package တစ#ခuထ8က အ*ခ1& Class eတ>ကသ1 အသuP&*ပueစလiuတ85 အခ?မ91 eရ&သ1&
eလ5 Diပ?တယ#။

Public Access

Class eတ>န8A Class Members eတ>မ91 Public Access Modifier ကiu အသuP&*ပu6ပ7&eရ&သ1&ထ1&ရင#eတ15 အ85ဒ?
Class eတ> Member eတ>ဟ1 Public Access ကiu ရDiမ91 *ဖစ#ပ?တယ#။ Public Access ဆiuတ1ကeတ15 မည#သ
ည#5 Class က မဆiu Access လuပ#ပiuင#ခ>င#5ရDiတ1ကiu ဆiuလiuတ1ပ?။

package [Link];

public class StringConverter {

public String getUnicode(String str) {


// codes and return statement
}
}

အထက#ပ?အတiuင#& StringConverter Class န8A getUnicode() Method မ91 public Access Modifier ကiu public
Access Modifier ကiu eရ&သ1&ထ1&တ85 အတ>က# အ*ခ1& Package eတ>ကeနလ8 Access လuပ#6ပ7& အသuP&*ပuBiuင#မ91
*ဖစ#ပ?တယ#။

တကယ#လiu getUnicode() Method ကiu public လuပ#ထ1&6ပ7& StringConverter Class ကiu Default Access
အတiuင#&ထ1&မယ#ဆiuရင#eတ15 Class ကiu မ*မင#ရတ85 အတ>က# Method ကiu အသuP&*ပuBiuင#မ91 မဟuတ#ပ?ဘR&။ Class
ထ8က Member eတ>ကiu အသuP&*ပuBiuင#ဖiuအတ>က# Class ကiuလ8 အသuP&*ပuBiuင#မ9ရမ91 *ဖစ#ပ?တယ#။

package [Link];

import [Link];

public class MessageRepository {

private StringConverter converter;

public void create(Message obj) {


String zawgyi = [Link]();
String unicode = [Link](zawgyi);
[Link](unicode);

// Other Codes
}
}

အထက#ပ?အတiuင#& StringConverter န8A getUnicode method တiuကiu တစ#*ခ1& Package ထ8မ91Diတ85


MessageRepository ထ8ကeန အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။
Learning Java 30 မင#&လ>င#
Java Developer Class

Protected Access

Class တစ#ခuရ8S Member eတ>ကiu Package တစ#ခuထ8မ91 Diတ85 Class eတ>န8A Sub Classs eတ>ကသ1 အသuP&*ပueစ
ခ(င#တယ#ဆiuရင# Proteced Access Modifier ကiu အသuP&*ပuBiuင#ပ?တယ#။

package [Link];

public class Shape {

protected double width;


protected double height;

public Shape(double w, double h) {


width = w;
height = h;
}

protected double getArea() {


return width * height;
}
}

အထက#ပ?အတiuင#& Shape Class ထ8မဟ1 width န8A height ကiu protected Access Modifier န8A eရ&သ1&ထ1&တ85
အတ>က# တစ#*ခ1& Package ထ8မ91Diတ85 Sub Class eတ>ကပ? width န8A height ကiu အသuP&*ပuလ1Biuင#မ91 *ဖစ#ပ?
တယ#။

package [Link];

public class ShapeTest {

public static void main(String[] args) {


Shape shape = new Shape(100, 50);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]());
}
}

အထက#ပ?နမRန1ထ8မ91လiu ShapeTest န8A Shape တiuဟ1 [Link] ဆiuတ85 Package ထ8မ91 အတRDiတ85 အတ>က#
shape object ကeန Protected Member eတ> *ဖစ#0ကတ85 [Link] န8A [Link] တiuကiu အသuP&*ပuလiuရ
eနတ1 *ဖစ#ပ?တယ#။

Protected Method eတ>ဆiuရင#လ8 အလ1&တRပ?ပ8။ Package တစ#ခuထ8မ91ဆiuရင# Access လuပ#လiuရeနမ91ပ?။ ဒ?eပမ85


Package မတReတ15ဘR&ဆiuတ1န8A ဒ7အတiuင#&ဆiuရင# Protected Member eတ>ကiu Access လuပ#လiuရမ91 မဟuတ#eတ15
ပ?ဘR&။ Protected Member eတ>ကiu Access လuပ#လiuလ(င# Shape Class ကiu Extends လuပ#ထ1&မ9သ1ရDiမ91 *ဖစ#
ပ?တယ#။
Learning Java 31 မင#&လ>င#
Java Developer Class

package [Link];

import [Link];

public class OtherShape extends Shape {

public OtherShape(double width, double height) {


super(width, height);
}

public static void main(String[] args) {


OtherShape other = new OtherShape(100, 50);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]());
}
}

OtherShape Class ဟ1 Shape Class န8A Package မတRပ?ဘR&။ ဒ?eပမ85 OtherShape ဟ1 Shape Class ကiu
Extends လuပ#ထ1&ပ?တယ#။ ဒ?5e0က1င#5 OtherShape Object *ဖစ#တ85 other ကeန height ကiueက1 with ကiuပ?
Access လuပ#Biuင#6ပ7& getArea() Method ကiuလ8 အသuP&*ပuBiuင#တ1 *ဖစ#ပ?တယ#။

Protected Modifier eတ>ဟ1 Package အတ>င#&မ91လ8 အသuP&*ပuBiuင#သလiu Package အ*ပင#မ91Diတ85 Sub Class
eတ>ကeနတဆင#5လ8 အသuP&*ပuBiuင#eအ1င# eဆ1င#ရ>က#eပ&Biuင#ပ?တယ#။ ဒ?5e0က1င#5 Protected Access Modifier eတ>
ကiu Package ++ Access Modifier လiuလ8 eခLဆiuBiuင#တ1 *ဖစ#ပ?တယ#။

Private Access

လက#Di Class တစ#ခuထ8ကသ1 အသuP&*ပuBiuင#တ85 Member eတ>ကiu eရ&သ1&လiuတ85 အခ?မ(iu&မ91 Private Access
Modifier ကiu အသuP&*ပueရ&သ1&Biuင#ပ?တယ#။ Class Member eတ>မ91 private လiueရ&လiuက#တ1န8A အ85ဒ7 Member
eတ>ဟ1 လက#Di Class ထ8ကသ1 အသuP&*ပuBiuင#eတ15မ91 *ဖစ#ပ?တယ#။

package [Link];

public class Shape {

private double width;


private double height;

public Shape(double w, double h) {


width = w;
height = h;
}
}

Learning Java 32 မင#&လ>င#


Java Developer Class

Java Data Types


Java Data Type ဆiuသည#မ91 Variable အeနန8A အသuP&*ပuBiuင#တ85 Data Type အမ(iu&အစ1&မ(1& *ဖစ#0ကပ?တယ#။
Java Data Type မ(1&ကiu အe*ခခPအ1&*ဖင#5 B9စ#မ(iu&ခ>8*ခ1&Biuင#ပ?တယ#။ အ85ဒ?eတ>ကeတ15 Primitive Data Type န8A
Reference Data Type တiu *ဖစ#0ကပ?တယ#။

အသuP&*ပueနတ85 Data Type အeပLမRတည#6ပ7&eတ15 Varaible eတ>ရ8S အလuပ#လuပ#ပuPက တRမ91 မဟuတ#ပ?ဘR&။


ဆက#လက#6ပ7& Java Data Type အမ(i&အစ1&eတ>န8A အလuပ#လuပ#ပuPeတ>ကiu eလ5လ1သ>1&0ကရeအ1င#။

Primitive Data Types


Primitive Data Type eတ>ကeတ15 Java Data Type eတ>ထ8မ91 အe*ခခPအက(ဆuP& Data Type eတ>*ဖစ#0ကပ?
တယ#။ Program eတ>ဆiuတ1က OS ကeန တဆင#5 CPU ကiu ဟiuဟ1 လuပ#ပ?၊ ဒ7ဟ1 လuပ#ပ?ဆiuင#& eစeနတ1ပ8*ဖစ#ပ?
တယ#။ အ85ဒ7အတ>က# OS eတ>ကeန Programming Language eတ>မ91 အသuP&*ပuဖiuဆiu6ပ7& Data Type eတ>ကiu *ပင#
eပ&ထ1&ပ?တယ#။

Java မ91e*ပ1တ85 Primitive Data Type eတ>ဆiuတ1က OS ကeန *ပင#eပ&ထ1&တ85 Data Type eတ> *ဖစ#ပ?တယ#။
ဒ7လiu ကနဦ& Data Type eတ>ကiu Primitive Data Type eတ>လiu eခLတ1 *ဖစ#ပ?တယ#။

Primitive Data အမ/iu1အစ-1မ/-1

အe*ခခPအ1&*ဖင#5 Primitive Data အမ(iu&အစ1& ၃ မ(iu&Diပ?တယ#။ True န8A False ကiuပ8 eဖL*ပBiuင#တ85 Boolean ဓ
အမ(iu&အစ1&၊ စ1လuP&eတ>ကiu ကiuယ#စ1&*ပuတ85 Character အမ(iu&အစ1&eတ> န8A Number eတ>ပ8 *ဖစ#ပ?တယ#။ Number
eတ>ကiuမ9ထပ#6ပ7& အ*ပည#5ကiန#& Integer eတ>န8A ဒဿမကiန#& Floating Number eတ>ဆiu6ပ7& ခ>8*ခ1&Biuင#ပ?တယ#။

Integer eတ>အထ8မ91မ9 အသuP&*ပueနတ85 Memory အလiuက# byte, short, int, long ဆiu6ပ7&ခ>8*ခ1&6ပ7&၊ ဒသမကiန#&eတ>
ကiuလ8 float န8A double ဆiu6ပ7& ခ>8*ခ1&ပ?တယ#။

ဒ?5e0က1င#5 Java မ91 အသuP&*ပueနတ85 Primitive Data Type အ1&လuP&eပ?င# ၈ မ(iu&Diတယ#လiu e*ပ1Biuင#မ91 *ဖစ#ပ?တယ#။

Learning Java 33 မင#&လ>င#


Java Developer Class

Type Description Type Description

byte 8bit, + or - Integer float 32bit, + or - fp value

short 16bit, + or - Integer double 64bit, + or - fp value


int 32bit, + or - Integer char 16bit, Unicode Character

long 64bit, + or - Integer boolean true or false

Number eတ>ထ8မ91 Integer အeနန8A byte, short, int, long တiuကiu အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။ က>1*ခ1&တ1
ကeတ15 Memory eပLမ91 အသuP&*ပuBiuင#တ85 Size ပ?ပ8။ အထက#ပ?ဇယ1&မ91 eဖL*ပထ1&တ85အတiuင#& byte eတ>ဆiuရင# 8
bit + or - Integer eတ>ကiu အသuP&*ပuBiuင#မ91 *ဖစ#6ပ7& long type ဆiuရင# + or - 64 bit Integer eတ>ကiu အသuP&*ပuBiuင#မ91
*ဖစ#ပ?တယ#။

Floating Number eတ>ဆiuရင#eတ15 float န8A double type eတ>ကiu အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။ float ကeတ15 32
bit + or - Floating Number eတ>ကiu အသuP&*ပuBiuင#မ91 *ဖစ#6ပ7& double ဆiuရင#eတ15 64 bit + or - Floating
Number eတ>ကiu အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။

Character eတ>ကiu ကiuယ#စ1&*ပuတ85 char type eတ>မ91eတ15 16 bit Unicode Character eတ>ကiu အသuP&*ပuBiuင#ပ?
တယ#။ တဖန# True န8A False သ1ပiuင#တ85 boolean type eတ>မ91eတ15 true န8A false ကiuသ1 အသuP&*ပuBiuင#ပ?မယ#။

Literals
Java Source File eတ>ထ8မ91 Source Code eတ>၊ Comment eတ>န8A Variable eတ>ရ8S Value eတ>ကiu eရ&သ1&
Biuင#ပ?တယ#။ Variable eတ>ကiu Initialize လuပ#တ85အခ? ဒ?မ9မဟuတ# Variable တစ#ခuရ8S တန#ဖiu&လiu အစ1&ထiu&တ85 အခ?
မ(iu&မ91 Source Code eတ>ထ8မ91 Value eတ>ကiu တiuက#^iuက#eရ&သ1&eလ5Diပ?တယ#။ Source Code eတ>ထ8မ91
တiuက#^iuက#eရ&သ1&ထ1&တ85 Value eတ>ကiu Literals eတ>လiueခLပ?တယ#။ Primitive Type eတ>မ91 ကiuယ#ပiuင#
Literals eတ>ကiu ပiuင#ဆiuင#0ကပ?တယ#။

package [Link];

public class LiteralDemo {

public static void main(String [] args) {


boolean b = true;
char c = ‘a’;
byte d = 10;
short e = 100;
int f = 1000;
long l = 10000;
float m = 10.01;
double n = 100.01918;
}
}

Learning Java 34 မင#&လ>င#


Java Developer Class

Boolean Literals

Boolean Type Variable eတ>မ91 အသuP&*ပuBiuင#တ85 literals eတ>ကeတ15 true န8A false သ1Diပ?တယ#။

package [Link];

public class LiteralDemo {

public static void main(String [] args) {


boolean yes = true;
boolean no = false;
}
}

Character Literals

Character eတ>ဆiuရင#eတ15 Single Quotation[‘] န8A အစ1ဆuP&ကiu ပiတ#6ပ7& eရ&သ1&eပ&ရပ?မယ#။ အထက#eဖL*ပပ?


အတiuင#& ‘a’ ဆiu6ပ7& Character တစ#လuP&ကiu eရ&သ1&eပ&ရမ91 *ဖစ#ပ?တယ#။ Character eတ>ကiu eရသ1&တ85 eနရ1မ91
Unicode Code Point ကiu အသuP&*ပu6ပ7&လ8 eရ&သ1&Biuင#ပ?တယ#။ ‘\u1018’ ဆiu6ပ7& eရ&သ1&Biuင#ပ?တယ#။

package [Link];

public class LiteralDemo {

public static void main(String [] args) {


char a = ‘a’;
char ba = ‘\u1018’;
}
}

Numbers Literals

Integer eတ>*ဖစ#0ကတ85 အ*ပည#5ကiန#&eတ>မ91 ဆiuရင# Decimal, Hexadecimal, Octal န8A Binary ပuPစPန8A တန#ဖiu&eတ>
ကiu eရ&သ1&လiuရBiuင#ပ?တယ#။ Decimal ဆiuတ1က ဆယ#ဆတiu&ပuPစPန8A eရ&သ1&နည#&ပ?။ ကOန#eတ1#တiu အ*ပင#မ91
အသuP&*ပueနတ85 ပuPစP*ဖစ#6ပ7& အသuP&မ(1&တ85 ပuPစP*ဖစ#ပ?တယ#။

Binary ပuPစPကeတ15 0 eတ> 1 eတ>န8A Integer ကiu eဖL*ပတ85 ပuPစP*ဖစ#ပ?တယ#။ 1 ဟ1 1 *ဖစ#6ပ7& 10 ဆiuတ1က 2 *ဖစ#ပ?
မယ#။ တဖန# 11 ဆiuတ1ကeတ15 3 ကiu eဖL*ပတ1 *ဖစ#ပ?မယ#။ Literal eတ>ကiu eရ&သ1&တ85 eနရ1မ91 eUမဟ1
Prefix အeနန8A 0B ဒ?မ9မဟuတ# 0b ကiu eရ&သ1&eပ&ရပ?မယ#။

Learning Java 35 မင#&လ>င#


Java Developer Class

package [Link];

public class LiteralDemo {

public static void main(String [] args) {

int binary = 0b11; // Binary form


int octal = 011; // Octal form
int decimal = 10; // Decimal Form
int hexadecimal = 0x1A; // Hexadecimal form
float f = 10.10;
double d = 1091.0191;
}
}

Octal ဆiuတ1ကeတ15 8 ဆတiu&ပuPစPန8A eဖL*ပတ1*ဖစ#ပ?တယ#။ 0 ကeန 7 အထiDi6ပ7& 10 ဆiuတ1 8 *ဖစ#6ပ7& 11 ဆiuရင#


9 *ဖစ#ပ?မယ#။ Octal ပuPစPန8A eရ&မယ#ဆiuရင#eတ15 eUမ91 Prefix အeနန8A 0 ကiu eရ&eပ&ရမ91 *ဖစ#ပ?တယ#။

Hexadecimal ဆiuတ1ကeတ15 16 ဆတiu&ကiန#&စနစ#န8A eဖL*ပတ1*ဖစ#ပ?တယ#။ Hexadecimal ပuPစPမ91 ကiန#&တစ#လuP&


ဟ1. ၁၆ လuP&အထieဖL*ပeပ&Biuင#ပ?တယ#။ ကiန#&တစ#eနရ1အတ>က# 0 မ9 9 အထiရယ# A မ9 F အထieဖL*ပeပ&Biuင#ပ?
တယ#။ 0 မ9 9 eက(1#သ>1&6ပ7ဆiuတ1န8A A ကeန F အထi eဖL*ပeပ&ပ?တယ#။ 10 ဆiuရင# A လiueဖL*ပ6ပ7& 11 ဆiuရင# B လiu
eဖL*ပပ?မယ#။ Java မ91 Hexadecimal ပuPစPန8Aeရ&မယ#ဆiuရင# eUမ91 Prefix အeနန8A 0x ဒ?မ9 မဟuတ# 0X ကiu eရ&eပ&
ရပ?မယ#။

Floating Number eတ>အeနန8A float န8A double တiuပ8Diပ?တယ#။ အ85ဒ?eတ>ကiueတ15 ဒသမကiန#&eရ&သလiuပ8 eရ&သ1&
Biuင#ပ?တယ#။

Suffix of Numbers

Integer Number eတ>အတ>က# byte, short, int, long ဆiu6ပ7&Diပ?တယ#။ 6ပ7&eတ15 Floating Number eတ>အတ>က#
float န8A double Diပ?တယ#။ Literal န8A ဒ7အတiuင#&eရ&မယ#ဆiuရင# အ85ဒ7 Type eတ>ကiu ခ>86ပ7& eဖLမ*ပBiuင#ပ8 Default Type
ကiu အသuP&*ပuသ>1&ပ?မယ#။

Integer Literal eတ>န8A eရ&ထ1&မယ#ဆiuရင# Default အတiuင#&ဆiuပ?က int ကiu အသuP&*ပuသ>1&ပ?မယ#။ 6ပ7&eတ15
Floating Number eတ>ဆiuရင#လ8 default အတiuင#&ဆiuပ?က double type ကiu အသuP&*ပuသ>1&မ91 *ဖစ#ပ?တယ#။
Variable တစ#ခuမ91 Assign လuပ#တ85 eနရ1မ91 မသiသ1eပမ85 Method eတ>ကiu Invoke လuပ#တ85 အခ?မ91 Parameter
eတ>eနရ1မ91 Literals eတ>ကiueရ&သ1&တ85 eနရ1မ91 သတi*ပuသင#5ပ?တယ#။

eအ1က#ပ? ကuဒ#နမRန1ထ8မ91လiu take() method ကiu Argument Type မတReအ1င# Method ၄ မ(iu&eရ&သ1&ထ1&ပ?
တယ#။ Argument Type eတ>ကeတ15 အသ7&သ7& byte, short, int, long တiu*ဖစ#0ကပ?တယ#။ Method Name တR
eပမ85 Argument Type မတRတ85 အတ>က# သRတiueတ>ဟ1 Method တစ#ခuစ7လiu Java Compiler ကeက1 Java
Runtime ကပ? မ9တ#ယReန0ကပ?တယ#။

Learning Java 36 မင#&လ>င#


Java Developer Class

public class TypeDemo {

public static void main(String[] args) {


take(10);
}

static void take(byte b) {


[Link]("Byte Value");
}

static void take(short s) {


[Link]("Short Value");
}

static void take(int i) {


[Link]("Int Value");
}

static void take(long l) {


[Link]("Long Value");
}
}

တကယ#လiu Main Method ထ8ကလiu take(10) လiueရ&လiuက#ရင# ဘယ# Method ကiu အလuပ#လuပ#သ>1&မ91လ8။ ဒ7eနရ1
မ91eတ15 Integer Literal eတ>ရ8S Default Type ဟ1 int *ဖစ#တ85 အတ>က# int type ကiu Argument အeနန8A ယRတ85
Method ကiu အလuပ#လuပ#eစမ91 *ဖစ#ပ?တယ#။

တကယ#လiu long type အeနန8A အသuP&*ပuခ(င#တယ#ဆiuရင#eတ15 Literals ရ8S eန1က#မ91 l ဒ?မ9မဟuတ# L ကiu Suffix
အeနန8A eရ&သ1&eပ&ရပ?မယ#။ take(10L) လiueရ&လiuက#ရင# long Type *ဖစ#တယ#ဆiuတ1ကiu သတ#မ9တ#eပ&Biuင#တ85
အတ>က# take(long l) Method ကiu အလuပ#လuပ#eစမ91 *ဖစ#ပ?တယ#။

အလ1&တRပ8 suffix ကiu အသuP&*ပuBiuင#တ1က float န8A double *ဖစ#ပ?တယ#။ float type အတ>က# suffix အeနန8A f ဒ?မ9
မဟuတ# F ကiu အသuP&*ပuBiuင#6ပ7& double type အတ>က# d န8A D ကiu အသuP&*ပuBiuင#ပ?တယ#။

ဒ?*ဖင#5ရင# byte, short, int မ91eတ15 suffix မDi0ကပ?ဘR&။ ဒ?*ဖင#5 byte ဒ?မ9မဟuတ# short ကiu သuP&တ85 Method ကiu
Invoke လuပ#ခ(င#ရင# ဘယ#လiuလuပ#မလ8။ int ကiu အသuP&လiuတ85 Type ကiu e*ပ1င#&ဖiuလiuအပ#ပ?တယ#။ ဒ?ကiu Type
Casting လiueခLပ?တယ#။

Primitive Casting

Size eသ&တ85 Type ကeန Size Xက7&တ85 Type ကiu e*ပ1င#&ခ(င#ရင# Java Compiler က အလiuအeလ(1က#e*ပ1င#&eပ&
Biuင#တယ#။ အ85ဒ?ကiu Implesit Casting လiueခLပ?တယ#။ ဥပမ1အ1&*ဖင#5 byte variable တစ#ခuကiu int variable ဆ7ကiu
အစ1&ထiu&တယ# ဆiu0ကပ?စiu။ အ85ဒ?ဆiuရင# ဘ1မ9အeထ>အထR&e*ပ1င#&စရ1မလiuပ8 eရ&သ1&Biuင#ပ?တယ#။ ဒ?eပမ85 int
variable ကeန byte Variable ကiu Assign လuပ#မယ#ဆiuရင#eတ15 (byte) ဆiu6ပ7& cast လuပ#eပ&ဖiuလiuအပ#ပ?တယ#။

Learning Java 37 မင#&လ>င#


Java Developer Class

public class Casting {

public static void main(String[] args) {

byte b = 10;
int i = b;

b = (byte) i;

float f = 10.1;
double d = f;

f = (float)d;
}
}

အလ1&တRပ8 float variable ကiu double variable eနရ1မ91 Assign လuပ#မယ#ဆiuရင# double က float ထက#ပiu6ပ7&
Xက7&တ85 အတ>က# အeထ>အထR&e*ပ1င#&6ပ7&eရ&စရ1မလiuအပ#ပ?ဘR&။ ဒ?eပမ85 float variable eနရ1ကiu double variable
ကiu Assign လuပ#မယ#ဆiuရင#eတ15 (float) ဆiu6ပ7& Type Cast လuပ#eပ&မ9ရပ?မယ#။

public class TypeDemo {

public static void main(String[] args) {


take((byte)10);
}

static void take(byte b) {


[Link]("Byte Value");
}

static void take(int i) {


[Link]("Int Value");
}
}

အထက#ပ? နမRန1ထ8မ91 take(10) လiueရ&ရင# take(int i) ကiu eခLသ>1&ပ?မယ#။ ဒ?5e0က1င#5 take(byte b) method ကiu
Invoke လuပ#eစခ(င#ရင#eတ15 take((byte)10) ဆiu6ပ7& Type Cast လuပ#6ပ7&မ9 take() Method ကiu Invoke လuပ#eပ&ဖiu
လiuအပ#ပ?တယ#။

Learning Java 38 မင#&လ>င#


Java Developer Class

Underscore In Number Literals


ကOန#eတ1#တiu ကiန#&ဂဏန#&eတ>ကiu အDည#Xက7& eရ&သ>1&မယ#ဆiuရင# အမတန#ကiu ဖတ#ရခက#ပ?တယ#။ ခu၊ ဆယ# ရ1
eထ1င# eသ1င#& အစDiသ*ဖင#5 မန8 ဖတ#ယRရတ1ကiu 0ကuPဘR&0ကမ91ပ?။ ဒ7eနရ1မ91 သuP&လuP&တစ#eနရ1 Comma (,) ကiu ခP
6ပ7& eရ&ထ1&ရင# ဖတ#ရတ1လ>ယ#ကReစပ?တယ#။ 100,000,000 ဆiuတ1မ(iu&eပ?5။

Java SE 7 ကeနစ6ပ7& Java မ91လ8 ကiန#&eတ>0က1&မ91 ဖတ#ရလ>ယ#eအ1င# *ပင#ဆင#လ1ခ85တ1Diပ?တယ#။ Comma


ကeတ15 Java မ91 အသuP&*ပueနတ85 စ1လuP&*ဖစ#တ85 အတ>က# အသuP&*ပuလiuမရပ?ဘR&။ အ85ဒ7အစ1& Under Score (_) ကiu
အသuP&*ပuBiuင#ပ?တယ#။ ဒ?eပမ85 Under Score ကiu သuP&လiuမရတ85 eနရ1eတ>Diပ?တယ#။

✓ eUဆuP&၊ eန1က#ဆuP&eတ>မ91 eရ&လiu မရပ?ဘR&


✓ Prefix eတ>ရ8S eန1က#eတ>၊ Suffix eတ>ရ8S eUeတ>မ91 eရ&လiuမရပ?ဘR&
✓ ဒသမ စ1လu&P ရ8S eUeန1က#eတ>မ91 eရ&လiuမရပ?ဘR&

eရ&လiuမရတ85 ပuPစPeတ>*ဖစ#ပ?တယ#။

public static void main(String[] args) {

int data1 = _10_000_000; // အeUဆuP&မ91 eရ&မရပ?


int data2 = 10_110_110_; // အeန1က#ဆuP&မ91 eရ&မရပ?
int data3 = 0b_11011; // Prefix eန1က#မ91 eရ&မရပ?
long data4 = 10000_L; // Suffix eUမ91 eရ&မရပ?
float data5 = 10191._101; // ဒသမကiန&# eန1က#မ91 eရ&မရပ?
}

eရ&လiuရတ85 ပuPစP*ဖစ#ပ?တယ#။

public static void main(String[] args) {

int data1 = 10_000_000;


int data2 = 10_110_110;
int data3 = 0b11_011;
long data4 = 10_000L;
float data5 = 10_191.101;
}

Learning Java 39 မင#&လ>င#


Java Developer Class

Reference Data Types


Primitive Data Type eတ>အe0က1င#&ကiu eလ5လ16ပ7&တ85 eန1က#မ91 ဆက#လက#6ပ7& eလ5လ1သ>1&0ကမ91ကeတ15
Reference Data Type *ဖစ#ပ?တယ#။ Object eတ>မ9န#သမTဟ1 Reference Data Type eတ> *ဖစ#0ကပ?တယ#။
Java မ91Diတ85 Type အeနန8A အသuP&*ပuလiuရတ85 Class eတ>၊ Interface eတ>၊ Enum eတ> အ1&လuP&ဟ1 Reference
Data Type eတ>*ဖစ#0ကတယ#။

ကOန#eတ1#တiu နမRန1ထ8မ91 အသuP&*ပueန0ကတ85 String တiuလiu Java SE API ထ8မ91ပ?တ85 Class eတ>eက1၊ ကOန#eတ1#
တiu ကiuယ#တiuင#eရ&သ1&ထ1&တ85 Class eတ>အ1&လuP&ဟ1လ8 Reference Data Type eတ>*ဖစ#0ကပ?တယ#။ ဒ?5e0က1င#5
Java မ91 Primitive Data Type က ၈ မ(iu&သ1Dieပမ85 Reference Data Type ကeတ15 Class eတ>Diသeလ1က#Di
eနမ91 *ဖစ#ပ?တယ#။

က8ဆက#လက#6ပ7& ဘ1လiu Reference Data Type လiueခLတ1လ8 ဆiuတ1ကiu eလ5လ1သ>1&0ကရeအ1င#။

public class ReferenceDemo {


public static void main(String[] args) {
int a = 77;
Person p = new Person();
[Link] = “Aung Aung”;
}
}

class Person {
Stirng name;
}

ကOန#eတ1#တiu eUမ91လ8 eရ&ဘR&ပ?တယ#။ Person p = new Person() ဆiuတ1 new Person() ဆiu6ပ7& Object
eဆ1က#6ပ7& Person type p Variable eနရ1မ91 လ1အစ1&ထiu&တယ#လiuပ8 သiလ10ကမ91 *ဖစ#ပ?တယ#။ တကယ#eတ15
Person Type p variable ကiu Stack Memory eပLမ91 သ>1&eဆ1က#ပ?တယ#။

(Stack Memory ဆiuတ1က JVM eပLမ91 အသuP&*ပuတ85 Memory Aera တစ#မ(iu&*ဖစ#ပ?တယ#။ JMV eပLမ91 Stack န8A
Heap ဆiu6ပ7& Memory Area B9စ#ခuကiu အသuP&*ပu0ကပ?တယ#။ Stack Memory ဆiuတ1က CPU န8A အန7&ဆuP&eနရ1မ91
Di6ပ7& CPU ကeန တiuက#^iuက# ဆက#သ>ယ# အသuP&*ပuBiuင#တ85 Memory Area *ဖစ#6ပ7& Life Time ကလ8 အeတ1#eလ&ကiu
တiueတ1င#&ပ?တယ#။ ကOန#eတ1#တiu eရ&ထ1&တ85 ကuဒ#eတ>ကiu အလuပ#လuပ#eနစ]# အ85ဒ7 Code eတ>ဟ1 Stack eပLမ91
Dieနပ?မယ#။ 6ပ7&သ>1&ရင#eတ15 eပ(1က#ကuန#မ91 *ဖစ#ပ?တယ#။ Heap ဆiuတ1ကeတ15 Stack ထက#ပiu6ပ7& က(ယ#*ပန#A6ပ7&
CPU ကeန တiuက#^iuက# ဆက#သ>ယ#6ပ7& အသuP&*ပueလ5မDiပ?ဘR&။ အ0က1Xက7&လ8 eဒတ1eတ>ကiu သiမ#&ထ1&6ပ7& အသuP&*ပu
Biuင#ပ?တယ#)

eန1က#ကပ?တ85 new Person() ဆiuတ1ကeတ15 Heap Memory eပLမ91 Person Object ကiu သ>1&eဆ1က#တ1ပ8
*ဖစ#ပ?တယ#။ Object eဆ1က#တ85eနရ1မ91 Instance Variable eတ>Diရင# Class ထ8ကeန Copy ကR&သ>1&6ပ7&
Object ထ8မ91 သiမ#&ထ1&ပ?မယ#။ ဒ?e0က1င#5 name Variable ဟ1လ8 Object ထ8ကiuပ?သ>1&ပ?မယ#။ 6ပ7&eတ15မ9
eဆ1က#ခ85တ85 Memory Address ကiu Person p eနရ1မ91 လ16ပ7& အစ1&ထiu&ပ?တယ#။

[Link] ဆiu6ပ7& Variable p ရ8S name ကiu အသuP&*ပu6ပ7ဆiuရင# p eနရ1မ91 Diတ85 Memory Address ကeန Object ကiu
Reference လuပ#6ပ7& name eနရ1ကiu အသuP&*ပuမ91 *ဖစ#ပ?တယ#။ ဒ7လiu Variable eတ>ကeန Object Diတ85eနရ1ကiu
Reference လuပ#6ပ7& သuP&eနရတ85 အတ>က# Object eတ>ကiu Reference Type Variable eတ>လiu eခLဆiuရ*ခင#& *ဖစ#ပ?
တယ#။
Learning Java 40 မင#&လ>င#
Java Developer Class
တကယ#eတ15 Primitive eတ>တuန#&က Variable eနရ1ကiu Assign လuပ#တ1န8A ပတ#သက#6ပ7& ဒ7eနရ1မ91 e*ပ1ခ(င#တ85
အတ>က# eUမ91 မe*ပ1ခ85ပ?ဘR&။ Primitive Type Variable eတ>ကeတ15 Reference Type Variable eတ>န8A မတR
ပ?ဘR&။

int a = 77; လiueရ&ထ1&ရင# Stack Memory eပLမ91 a အတ>က# 32 Bit စ1 eနရ1ယRပ?တယ#။ 6ပ7&ရင# 77 ကiu Value
အeနန8A အစ1&ထiu&မ91 *ဖစ#ပ?တယ#။ မ9တ#မယ#ဆiuရင# Primitive Type Variable eတ>က Variable eတ>eနရ1မ91 တ
ကယ#5 တန#ဖiu&ကiu လ1အစ1&ထiu&6ပ7& Reference Data Type eတ>ကeတ15 eဆ1က#ထ1&တ85 Object ကiu Reference
လuပ#Biuင#တ85 Memory Address ကiu အစ1&ထiu&တ1ပ8 *ဖစ#ပ?တယ#။

public class ReferenceDemo {


public static void main(String[] args) {
int a = 77;
Person p = new Person();
[Link] = "Aung Aung";

int b = a;
Person q = p;

a = 100;
[Link] = "Thidar";
}
}

Variable eတ>ကiu အ*ခ1& Variable eတ>မ91 အစ1&ထiu&0ကည#5ရeအ1င#။ int b = a; လiueရ&လiuက#တ1န8A a ရ8S တန#ဖiu&*ဖစ#
တ85 77 ဟ1 b ရ8S တန#ဖiu&*ဖစ#သ>1&ပ?မယ#။ ဒ?5e0က1င#5 eအ1က#မ91 a = 100 လiu a ရ8S တန#ဖiu&ကiu 100 လiu အစ1&ထiu&ရင#
လ8 b ရ8S တန#ဖiu&ဟ1 77 ပ8 *ဖစ#eနမ91ပ?။

ဒ?eပမ85 Reference Type eတ>ကeတ15 ဒ7လiuမဟuတ#ပ?ဘR&။ Person q = p; လiueရ&လiuက#တ1ဟ1 q ရ8S တန#ဖiu&ကiu p


ကeန Reference လuပ#eနတ85 Object ရ8S Memory Address ကiu လ16ပ7& အစ1&ထiu&တ1 *ဖစ#ပ?တယ#။ ဒ?5e0က1င#5
Reference Variable p eက1 q ပ? တစ#ခuထ8eသ1 Person Object ကiu လ9မ#&6ပ7& Reference လuပ#eန0ကမ91 *ဖစ#ပ?
တယ#။ ဒ?5e0က1င#5 [Link] = “Thidar” လiue*ပ1င#&လiuက#တ1န8A [Link] ဟ1လ8 “Thidar” *ဖစ#သ>1&မ91 *ဖစ#ပ?တယ#။
Reference Variable eတ>ကiu အသuP&*ပuတ85 eနရ1မ91 တစ#*ခ1& Variable eတ>ကeန သuP&eနတ85 Object ကiu
Reference လuပ#eနBiuင#တယ#ဆiuတ1ကiu သတiထ1&ဖiuလiuအပ#ပ?တယ#။

ဆက#လက#6ပ7& Method Argument eတ>မ91 သuP&တ85 Variable eတ>ဆiuတ1 ဘယ#လiuဟ1မ(iu&လ8 ဆiuတ1ကiu eလ5လ1
သ>1&0ကရeအ1င#။ Primitive Type ကeန စ6ပ7& eလ5လ1သ>1&0ကရeအ1င#။

public class ReferenceDemo {


public static void main(String[] args) {

int a = 77;
change(a);
[Link](a);
}

static void change(int a) {


a = 100;
} မင#&လ>င#
Learning Java 41
}
Java Developer Class
အထက#မ91 eဖL*ပထ1&သလiu change() method က int type Variable တစ#ခuကiu Argument အeနန8A ယRပ?တယ#။
6ပ7&eတ15မ9 Method ထ8မ91 a = 100 ဆiu6ပ7& a ရ8S တန#ဖiu&ကiu e*ပ1င#&လiuက#ပ?တယ#။ Main Method ထ8မ91eတ15
change(a) ဆiu6ပ7& Invoke လuပ#ပ?တယ#။ [Link](a) လiueရ&လiuက#ရင# a ရ8S တန#ဖiu&ဟ1 ဘ1*ဖစ#သ>1&မ
လ8။ Method ထ8မ91 a = 100 လiu e*ပ1င#&ထ1&တ85 အတ>က# a ရ8S တန#ဖiu&ဟ1 100 လiue*ပ1င#&သ>1&တယ#လiu ထင#ပ?
မယ#။ ဒ?eပမ85 အe*ဖဟ1 77 ပ8*ဖစ#eနမ91 *ဖစ#ပ?တယ#။

ဘ1လiuလ8 ဆiueတ15 change(a) လiu eရ&လiuက#တ1ဟ1 Variable a ကiu change() Method ကiu eပ&လiuက#တ1မဟuတ#
ပ?ဘR&။ Variable a ရ8S value *ဖစ#တ85 77 ကiu change() Method ရ8S Argument *ဖစ#တ85 a eနရ1ကiu Copy ကR&6ပ7&
eပ&လiuက#တ1 *ဖစ#ပ?တယ#။ Value ကiuပ8 eပ&လiuက#တ1*ဖစ#ပ?တယ#။ ဒ?5e0က1င#5 change() method ထ8က a ဟ1
Main Method ထ8က Variable a န8A သက#ဆiuင#မ-မDiပ?ဘR&။ change() Method ထ8က a ရ8S တန#ဖiu&ကiu e*ပ1င#&လiuက#
တ1လ8 change() method ထ8က a သ1 e*ပ1င#&လ8သ>1&တ1 *ဖစ#6ပ7&၊ Main Method ထ8က a ရ8S တန#ဖiu&ဟ1 နဂiu
အတiuင#& 77 ပ8*ဖစ#eနတ1 *ဖစ#ပ?တယ#။

public class ReferenceDemo {


public static void main(String[] args) {
Person p = new Person();
[Link] = "Aung Aung";
change(p);
[Link]([Link]);
}

static void change(Person p) {


[Link] = "Thidar";
}
}

Reference Variable eတ>မ91eက1 ဘယ#လiuလ8 ဆiuတ1ကiu ဆက#6ပ7&eလ5လ1သ>1&0ကရeအ1င#။ eစ1eစ1က အတiuင#&


ပ?ပ8။ Person p ကiu change(p) ဆiu6ပ7& change() Method ကiu Invoke လuပ#eနပ?တယ#။ ဒ7eနရ1မ91လ8 သတi*ပuဖiu
လiuတ1က change(Person p) လiueရ&ထ1&တ1န8A Main Method ထ8မ91 Person p လiueရ&ထ1&တ1က န1မည#သ1
တRတယ#၊ Scope ခ(င#&မတRတ85 အတ>က# Local Variable တစ#ခuစ7လiu သiထ1&eစခ(င#ပ?တယ#။

Main Method ထ8မ91 change(p) လiueရ&လiuက#တ1ဟ1 Main Method ထ8မ91Diတ85 p variable ရ8S Value ကiu
change() method ရ8S local variable p မ91 Copy ကR&eပ&လiuက#တ1ပ?ပ8။ Method eတ>ကiu Invoke လuပ#တ85 eနရ1
မ91 Variable eတ>ကiu Parameter အeနန8A eပ&လiuက#တယ#ဆiuတ1 Variable ကiu eပ&လiuက#တ1မဟuတ#ပ8 Variable ရ8S
Value ကiuပ8 eပ&လiuက#တ1 *ဖစ#ပ?တယ#။

Reference Type Variable eတ>ရ8S Value ဟ1 Reference လuပ#eနတ85 Object eတ>ရ8S Memory Address သ1
*ဖစ#တ85 အတ>က# change() Method ထ8မ91 Dieနတ85 p ဟ1လ8 Main Method ထ8က p က Reference လuပ#eနတ85
Object ကiuပ8 Reference လuပ#eနပ?တယ#။ change() method ထ8မ91 [Link] ကiu e*ပ1င#&လiuက#တ85 အတ>က# Main
Method ထ8မ91Diတ85 [Link] ဟ1လ8 e*ပ1င#&သ>1&မ91 *ဖစ#ပ?တယ#။ ဒ?5e0က1င#5 [Link]([Link]) လiu
eရ&လiuက#တ85 အခ?မ91 အe*ဖဟ1 “Thidar” လiu eဖL*ပသ>1&မ91 *ဖစ#ပ?တယ#။

Wrapper Classes of Primitives


eUခန#&မ91 Primitive Data Type eတ>အe0က1င#&ကiueလ5လ1ခ85တ85 eနရ1မ91 Primitive Data Type eတ>မ91
Method eတ>မပ?ဘR&ဆiuတ1ကiu သတi*ပuမiပ?မယ#။ တကယ#လiu Type တစ#ခuကeနတစ#ခuကiu e*ပ1င#&ခ(င#တ85အခ?၊ ဒ?မ9

Learning Java 42 မင#&လ>င#


Java Developer Class
မဟuတ# String ကeန တစ#*ခ1& Type တစ#ခuခuကiu e*ပ1င#&ခ(င#တ85အခ?၊ Primitive Type ကeန Stiring ကiue*ပ1င#&ခ(င#
တ85 အခ?မ(iu&eတ>မ91 အခက#အခ8 eတ>Zရတတ#ပ?တယ#။ အ85ဒ7လiuလuပ#eဆ1င#မ-eတ>ကiu အလ>ယ#တကReဆ1င#ရ>က#Biuင#
eအ1င# Java မ91 Primitive Type eတ>အတ>က# Wrapper Class eတ>ကiu *ပင#eပ&ထ1&ပ?တယ#။
အထက#ပ?အတiuင#& Primitive Data Type အလiuက# Wrapper Class eတ>ကiu *ပင#eပ&ထ1&6ပ7& Primitive Type မ9
ဒ?မ9မဟuတ# String တစ#ခuကeန Object အ*ဖစ#e*ပ1င#&Biuင#eအ1င# Constructor eတ>ကiu *ပင#eပ&ထ1&ပ?တယ#။

Integer a = new Integer(10);


Integer b = new Integer(“20”);
Float c = new Float(14.10F);
Float d = new Float(“10.8181”);

အထက#ပ?အတiuင#& သက#ဆiuရင#ရ1 Primitive အလiuက# Wrapper Object eတ>ကiu တည#eဆ1က#Biuင#ပ?တယ#။


Auto Boxing - Unboxing
Java Version 5 အeရ1က#မ91 Auto Boxing - Unboxing ဆiuတ85 Feature တစ#ခuကiu အသစ#*ဖည#5စ>က#လ1ပ?တယ#။
ဒ?ကeတ15 Primitive Type eတ>န8A Wrapper Object eတ>0က1&မ91 Object eဆ1က#စရ1မလiuပ8 Assign လuပ#လ1
Biuင#သလiu၊ Object eတ>မ91လ8 Method ကiu eခLစရ1မလiuပ8 Primitive အ*ဖစ#e*ပ1င#&လ1Biuင#ပ?တယ#။

int a = 10;
Integer b = new Integer(a);

int c = [Link]();

Java SE 5 မတiuင#ခင#ကဆiuရင# primitive type a ကiu Integer အ*ဖစ#e*ပ1င#&ခ(င#ရင# new Integer(a) ဆiu6ပ7&
Constructor ကiuအသuP&*ပu6ပ7& Object အ*ဖစ#e*ပ1င#&မ9ရပ?တယ#။ ဒ7လiuမ(iu& Primitive Type ကeန သ\ရ8S Wrapper
Object အ*ဖစ#e*ပ1င#&တ1ကiu Boxing လiueခLပ?တယ#။ Object ဆiuတ85 eသတj 1ထ8ထည#5တယ#ဆiuတ85 သeဘ1eပ?5ဗ(1။

တဖန# Wrapper Object ကeန primitive type ကiu *ပန#လiuခ(င#ရင#လ8 [Link]() ဆiu6ပ7& Method ကiu အသuP&*ပu6ပ7&
Type ကiu *ပန#e*ပ1င#&eပ&ဖiuလiuပ?တယ#။ အ85ဒ?ကiueတ15 Unboxing လiueခLပ?တယ#။ Object ဆiuတ85 eသတj 1ထ8ကeန
ထuတ#6ပ7& Primitive ကiu *ပန#e*ပ1င#&eပ&တယ#ဆiuတ85 သeဘ1eပ?5။

ဒ?eပမ85 Java SE 5 eရ1က#တ85 အခ?မ91 ဒ?eတ>ဟ1 မလiuအပ#eတ15ပ?ဘR&။ Boxing eတ> Unboxing eတ>ကiu JVM က
အလiuအeလ(1က#လuပ#eဆ1င#eပ&လ1Biuင#ပ?တယ#။

int a = 10;
Integer b = a;

int c = b;

အထက#ပ?အတiuင#& Wrapper Object b eနရ1ကiu Primitive Variable တစ#ခu*ဖစ#တ85 a ကiu တiuက#^iuက# အစ1&ထiu&Biuင#
သလiu၊ primitive type variable တစ#ခu*ဖစ#တ85 c eနရ1ကiuလ8 int ရ8S Wrapper Object တစ#ခu*ဖစ#တ85 b န8A အစ1&ထiu&
လ1Biuင#ပ?တယ#။

Learning Java 43 မင#&လ>င#


Java Developer Class

Wrapper Class eတ#န9< ဘ-လuပ)လiuရလ9


Primitive Wrapper eတ>န8A ဘ1eတ>လuပ#လiuရလ8 ဆiuတ1ဟ1 ကOန#eတ1#တiu သiထ1&သင#5တ85 အခ(က# *ဖစ#ပ?တယ#။
Primitive Type Value eတ>ကiu *ပန#6ပ7& ထuတ#ယRလiuရတယ#။ ဒ?eပမ85 Auto Boxing Unboxing e0က1င#5 ဒ?ဟ1 သiပ#
6ပ7& အeရ&မပ?eတ15ပ?ဘR&။ Variable B9စ#ခuကiu Compare လuပ#လiuရမယ#။ တန#ဖiu&တRမတR စစ#လiuရမယ#။ 6ပ7&ရင# အ*ခ1&
Type eတ>ကiu e*ပ1င#&လiuရမယ#။

Wrapper Class တiuင#&မ91 compare() န8A equals() Method eတ>ပ?လ1ပ?တယ#။ ဒ?eပမ85 ဒ?ဟ1လ8 Primitive Type
eတ>န8A Operators eတ>ကiu တ>86ပ7&eရ&မယ#ဆiuရင# *ဖစ#eနတ1*ဖစ#တ85 အတ>က# အရမ#&ကiu လiuအပ#eနတ1eတ15 မဟuတ#
ပ?ဘR&။ ဒ?eပမ85 တစ#*ခ1& Primitive Type eတ> အ*ဖစ#e*ပ1င#&လiuရတ85 အခ(က#ကeတ15 သuP&ခ(င#စရ1 အခ(က#တစ#ခu*ဖစ#
ပ?တယ#။

Compare with other Variable

Number အမ(iu&အစ1& Wrapper Type eတ>မ91 compare() ဆiuတ85 Static Method တစ#ခuန8A compareTo() ဆiuတ85
Instance Method eတ>ပ?ဝင#6ပ7& Variable B9စ#ခuကiu B-င#&ယ9]#ရ1မ91 အသuP&*ပuBiuင#ပ?တယ#။

public static void main(String[] args) {


int a = 10;
int b = 13;

int result1 = [Link](a, b);


[Link](result1);

Integer c = a;
Integer d = b;

int result2 = [Link](d);


[Link](result2);
}

[Link](int a, int b) Method ကeတ15 Static Method *ဖစ#6ပ7& int variable B9စ#ခuကiu B-င#&ယ9]#ရ1မ91
အသuP&*ပuBiuင#eအ1င# *ပင#ဆင#ထ1&တ85 Method တစ#ခuDiပ?တယ#။ အ*ခ1&eသ1 Number Type Class eတ>မ91လ8
compare method ကiu *ပင#eပ&ထ1&ပ?တယ#။ [Link]() Method ကiu အသuP&*ပuမယ#ဆiuရင# double
variable B9စ#ခuကiu Compare လuပ#Biuင#မ91 *ဖစ#ပ?တယ#။ compare() Method ရ8S Return Type ကeတ15 int *ဖစ#6ပ7&
variable B9စ#ခuတန#ဖiu&တReနရင# 0 ကiu Return *ပန#မ91 *ဖစ#6ပ7& ပထမ Variable ကXက7&eနရင# 1 ကiu Return *ပန#6ပ7&၊
ပထမ Variable က ငယ#eနရင# -1 ကiu Return *ပန#မ91 *ဖစ#ပ?တယ#။

အထက#ပ? နမRန1ထ8မ91 [Link](a, b) လiueရ&ထ1&6ပ7& a ဟ1 b ထက#ငယ#တ85 အတ>က# result1 ဟ1 -1


*ဖစ#ပ?မယ#။ တဖန# Wrapper Object eတ>*ဖစ#0ကတ85 c န8A d eတ>မ91 compareTo() ဆiuတ85 Method ကiu အသuP&*ပuBiuင#
မ91 *ဖစ#ပ?တယ#။ c ဟ1 d ထက#ငယ#တ85 အတ>က# [Link](d) ရ8S ရလဒ#*ဖစ#တ85 result2 ဟ1လ8 -1 ပ8*ဖစ#ပ?
မယ#။

Learning Java 44 မင#&လ>င#


Java Developer Class

static void compare(int a, int b) {


int result = a - b;

String sign = result == 0 ? "=" : result > 0 ? ">" : "<";

[Link]("%d %s %d%n", a, sign, b);


}

Compare လuပ#တ85 eနရ1မ91 အထက#ပ?အတiuင#& a - b ဆiu6ပ7& ရလဒ#န8Aလ8 0ကည#5လiuရပ?တယ#။ result ဟ1 0 *ဖစ#ရင# a


န8A b ဟ1 တReန6ပ7&၊ 0 ထက#Xက7&ရင# a ဟ1 b ထက#Xက7&eနပ?မယ#။ တဖန# result က 0 ထက#ငယ#eနရင#eတ15 a ဟ1
b ထက#ငယ#တယ#ဆiu6ပ7&လ8 သiBiuင#ပ?တယ#။

တRမတRစစ#ခ(င#တ85အခ?မ91

Variable တန#ဖiu&B9စ#ခu တRမတR စစ#ခ(င#တ85 အခ?မ91 Primitive eတ>မ91 Operators eတ>ကiu အသuP&*ပu6ပ7& စစ#လiuရပ?
တယ#။ ဒ?eပမ85 Wrapper Class eတ>မ91 [Link] ကeနပ?လ1တ85 equals() method ကiu အသuP&*ပu6ပ7&
စစ#eဆ&Biuင#မ91 *ဖစ#ပ?တယ#။

public static void main(String[] args) {


double a = 10.101;
double b = 10.101;

[Link](a == b);

Double c = a;

[Link]([Link](b));
}

အထက#ပ?အတiuင#& a န8A b ဟ1 Primitive Type eတ>*ဖစ#0ကတ85 အတ>က# a == b ဆiu6ပ7& တRမတR စစ#eဆ&Biuင#ပ?တယ#။


တဖန# c ဟ1 Wrapper Type *ဖစ#တ85 အတ>က# Object ကeန ပ?လ1တ85 equals() method ကiu အသuP&*ပu6ပ7&
စစ#eဆ&Biuင#ပ?တယ#။ ဒ7eနရ1မ91 [Link](b) လiueရ&ထ1&တ85 eနရ1မ91 b ဟ1 primitive type *ဖစ#eပမ85 Auto
Boxing - Unboxing e0က1င#5 Wrapper Object အ*ဖစ#အလiuလiue*ပ1င#&6ပ7& စစ#eဆ&Biuင#တ1 *ဖစ#ပ?တယ#။

Type e*ပ1င#&ခ(င#တ85အခ?

Primitive Type eတ>မ91 Integer eတ>အတ>က# Type eလ&မ(iu&Di6ပ7” Floating Number eတ>အတ>က# Type B9စ#
မ(iu&Diပ?တယ#။ Primitive Type eတ>မ91 ဆiuရင# Type eတ>ကiu e*ပ1င#&ခ(င#ရင# Type Casting ကiu အသuP&*ပuလiuရပ?
တယ#။ ဒ?eပမ85 Wrapper Object eတ>ကiu အသuP&*ပuမယ#ဆiuရင# Type တစ#မ(iu&ကeန တစ#မ(iu&ကiu အလ>ယ#တကR
e*ပ1င#&လiuရပ?တယ#။ Number Type Wrapper Class eတ>ဟ1 Number ဆiuတ85 Abstract Class ကiu Extends
လuပ#ထ1&0က6ပ7& Number Class ထ8မ91 Type e*ပ1င#&ဖiu eအ1က#ပ? Method eတ>ကiu *ပင#ဆင#ထ1&ပ?တယ#။

Learning Java 45 မင#&လ>င#


Java Developer Class

Modifier and Type Method and Description


byteValue()
byte Returns the value of the specified number as a byte, which may
involve rounding or truncation.
doubleValue()
abstract double Returns the value of the specified number as a double, which may
involve rounding.
floatValue()
abstract float Returns the value of the specified number as a float, which may
involve rounding.
intValue()
abstract int Returns the value of the specified number as an int, which may
involve rounding or truncation.
longValue()
abstract long Returns the value of the specified number as a long, which may
involve rounding or truncation.
shortValue()
short Returns the value of the specified number as a short, which may
involve rounding or truncation.

အထက$eဖ'(ပပ* Method eတ,ကiu Number အမ0iu2အစ42 Wrapper Object eတ,အ42လu62မ74 အသu62(ပu9iuင$မ74


(ဖစ$ပ*တယ$။

public static void main(String[] args) {


Double d = 10.11;
float floatData = [Link]();
byte byteValue = [Link]();
short shortValue = [Link]();
int intData = [Link]();
long longData = [Link]();
}

အထက$ပ*အတiuင$2 Primitive Wrapper Object eတ,ကiu အသu62(ပuမယ$ဆiuရင$ အ(ခ42 Type eတ,ကiuလ@ အသင$A
(ပင$ထ42တ@A Method eတ,ကiu အသu62(ပuBပC2 အလ,ယ$တကDe(ပ4င$2လ@9iuင$မ74 (ဖစ$ပ*တယ$။ အ(ခ42eသ4 Number
Type Wrapper Class eတ,မ74လ@ Type e(ပ4င$2တ@A Method eတ,ကiu အသu62(ပu9iuင$မ74 (ဖစ$ပ*တယ$။

Learning Java 46 မင#&လ>င#


Java Developer Class

Arrays
ဒ7အခန#&မ91eတ15 Java မ91 အသuP&*ပueနတ85 Array ဆiuတ1 ဘယ#လiuဟ1မ(iu&လ8။ 6ပ7&eတ15 Array eတ>ကiu ဘ1လiu
လiuအပ#0ကတ1လ8၊ Array Object eတ>ကiu ဘယ#လiu တည#eဆ1က#0ကမလ8၊ Array ထ8မ91ပ?တ85 Element eတ>ကiu
ဘယ#လiu Access လuပ#မလ8ဆiuတ1န8A Multi Dimension Array eတ>အe0က1င#&ကiu eလ5လ1သ>1&0ကပ?မယ#။

Array ဆiuတ1က အမ(iu&အစ1&တRတ85 Data eတ>ကiu အစ7အစ]#တက( သiမ#&eပ&ထ1&Biuင#တ85 Object တစ#မ(iu&*ဖစ#ပ?


တယ#။ ဒ?eပမ85 Array eတ>က Object eတ>*ဖစ#eပမ85 တစ#*ခ1& Object eတ>န8A eရ&သ1&ပuP မတRည70ကပ?ဘR&။ တစ#*ခ1&
Object eတ>ဆiuရင# Type eတ>ကiu Class Name န8A eရ&သ1&eဖL*ပ6ပ7& new Operator ကiu အသuP&*ပu6ပ7&
Constructor eတ>ကeန တဆင#5 Object eတ>ကiu တည#eဆ1က#Biuင#ပ?တယ#။

ဒ?eပမ85 Array eတ>က Type eရ&သ1&ပuPeက1၊ Object တည#eဆ1က#ပuPeတ>ပ? တစ#*ခ1& Object eတ>န8A မတR0ကပ?
ဘR&။ ဒ7 အခန#&မ91eတ15 Array eတ>အe0က1င#&ကiu eလ5လ1သ>1&0ကပ?မယ#။

ဘ-လiu Array eတ#ကiuအသu516ပuဖiuလiuအပ)ခ9?တ-လ9


တကယ#လiu ကOန#eတ1#တiu Twitter eပLမ91eရ&သ1&ခ85တ85 Twit eတ>ကiu eဖL*ပBiuင#တ85 Application တစ#ခueရ&eနတယ#
လiu eတ>&0ကည#50ကရeအ1င#။ ပထမဆuP& Twit ကiueဖL*ပဖiu String firstTweet ဆiuတ85 Variable န8A eဖL*ပမယ#၊ ဒuတiယ
Twit အတ>က# secondTwit ဆiuတ85 Variable န8A eဖL*ပ0ကမယ#။ ဒ7လiuန8A တတiယ Twit eန1က#ဆuP& Twit တစ#ခu
အတ>က# Variable တစ#ခu eရ&eနရရင# Variable eတ>ဆuP&မ91 မဟuတ#eတ15ပ?ဘR&။

ဒ7လiueနရ1မ(iu&မ91 String အမ(iu&အစ1& Data eတ>ကiu အစ7စ]#အလiuက# သiမ#&eပ&ထ1&Biuင#တ85 Object eတ>လiuလ1ပ?


မယ#။ ဒ7လiu လiuအပ#ခ(က#eတ>အတ>က# အသuP&*ပuBiuင#တ1ကeတ15 Array Object eတ>*ဖစ#0ကပ?တယ#။
Array eတ>ဟ1 Type အမ(iu&အစ1&တRတ85 Data eတ>ကiu သiမ#&eပ&ထ1&Biuင#တ85 ခ>က#eလ&eတ>ကiu တန#&စ76ပ7&ထ1&တ85
ရထ1&တ>8 တစ#ခuန8A တRပ?တယ#။ Array တစ#ခuကiu အသuP&*ပueတ15မယ#ဆiuရင# ဘ1 Type ကiu သuP&မယ#ဆiuတ1န8A ဘယ#B9စ#ခu
သiမ#&ထ1&Biuင#တယ#ဆiuတ1ကiu Xကiu6ပ7& သတ#မ9တ#eပ&ထ1&ဖiuလiuအပ#ပ?တယ#။

အစကထ8က int အမ(iu&အစ1& Data ၁၀ ခu အသuP&*ပuမယ#လiu သတ#မ9တ#ထ1&တယ#ဆiuရင# အ85ဒ7 Array ထ8ကiu int ကလ>8
6ပ7& သiမ#&ထ1&လiuမရBiuင#ပ?ဘR&။ 6ပ7&eတ15 Data ကiuလ8 10 ခuထက#ပiu6ပ7& သiမ#&မeပ&ထ1&Biuင#ပ?ဘR&။ 6ပ7&eတ15 Array ထ8
မ91 သiမ#&ထ1&တ85 Data eတ>ကiuလ8 အခန#& နPပတ#န8A သ7&*ခ1& သတ#မ9တ#6ပ7& သiမ#&eပ&ထ1&Biuင#ပ?တယ#။ 6ပ7&eတ15 ဘယ#
နPပ?တ#က Data လiuခ(င#လ8 ဆiuတ1န8A ဘယ#နPပ?တ#မ91 ဘယ# Data ကiu သiမ#&မယ#ဆiuတ1ကiu သတ#မ9တ# အသuP&*ပuBiuင#ပ?
တယ#။

Array မ91 အသuP&*ပueနတ85 အခန#& နPပ?တ#eတ>ဟ1 သuညကeနစပ?တယ#။ ဒ?5e0က1င#5 Data 10 ခu သiမ#&ထ1&Biuင#တ85


Array ဆiuရင# နPပ?တ# 0 ကeန 9 အထiအခန#&eတ>ကiu ပiuင#ဆiuင#မ91 *ဖစ#ပ?တယ#။

Declaring Arrays
Array eတ>ကiu အသuP&*ပueတ15မယ#ဆiuရင# အန8ဆuP& ဘယ#လiuအမ(iu&အစ1& Array ကiu အသuP&*ပuမ91လ8 ဆiuတ1ကiu သiဖiu
လiuအပ#ပ?တယ#။ Array အeနန8A Primitive Type eတ>ကiuလ8 အသuP&*ပuBiuင#သလiu Reference Type eတ>ကiuလ8
အသuP&*ပuBiuင#ပ?တယ#။ int type eတ>ကiu အသuP&*ပuတ85 Array ဆiuရင# int array လiueခLဆiueလ5Diသလiu String Type eတ>
ကiu အသuP&*ပuတ85 Array eတ>ကiu String Array လiuလ8 eခLeလ5Diပ?တယ#။ Custom Object *ဖစ#တ85 User Class eတ>
ကiu အသuP&*ပuBiuင#တ85 Array eတ>ဆiuရင#လ8 User Array လiueခLeလ5Diပ?တယ#။

အe*ခခPအ1&*ဖင#5 Array Type Variable eတ>ကiu Declare လuပ#ပuP B9စ#မ(iu&DiBiuင#ပ?တယ#။ ပထမတစ#နည#&ကeတ15


ပ?ဝင#မည#5 Element eတ>ကiuXကiuသiတ85 အခ?eရ&သ1&တ85 နည#&န8A eန1က#တစ#နည#&ကeတ15 အရင#ဆuP& Array ကiu Xကiu

Learning Java 47 မင#&လ>င#


Java Developer Class
eဆ1က#ထ1&6ပ7& eန1က#မ9 Program ထ8ကeန Element eတ>ကiu Set လuပ#လiuတ85 အခ?မ(iu&မ91 အသuP&*ပuBiuင#တ85 နည#&
တiuပ8 *ဖစ#ပ?တယ#။

If you know Array Elements


Array ထ8မ91ပ?မည#5 Element eတ>ကiu Xကiu6ပ7&သiထ1&မယ#ဆiuရင# eအ1က#ပ? အတiuင#& Array ကiu Declare လuပ#Biuင#ပ?
တယ#။

int [] array = {1,2,3,4,5};

Array Variable ကiu Declare လuပ#မယ#ဆiuရင# Array မ91 အသuP&*ပuမည#5 Type ရယ# Array Sing *ဖစ#တ85 [] ရယ#၊
Variable Name ကiu eရ&သ1&eပ&ရပ?မယ#။ အထက#ပ?အတiuင#& int [] array လiueရ&သ1&ထ1&ရင# int ဆiuတ1 Array
ကiu အသuP&*ပuမည#5 Type *ဖစ#6ပ7& array ဆiuတ1က Variable Name *ဖစ#ပ?တယ#။

Array ကiu eရ&တ85 eနရ1မ91 int [] array လiuလ8 eရ&သ1&Biuင#သလiu int array [] လiuလ8 eရ&သ1&Biuင#မ91 *ဖစ#ပ?တယ#။
အထက#ပ?အတiuင#& Array ကiu Declare လuပ#မယ#ဆiuရင# {1,2,3,4,5} န8Aတiuက#^iuက# အစ1&ထiu& eရ&သ1&Biuင#eသ1#လ8
Array ကiu Declare လuပ#6ပ7&တ85 အခ?က(မ9 Assign လuပ#တ85 Statement eတ>မ91ဆiuရင#eတ15 eရ&သ1&လiuရမ91 မဟuတ#
ပ?ဘR&။

int [] array;
array = {1,2,3,4,5};

အထက#ပ?အတiuင#& Assignment Statement eတ>ကiu eရ&သ1&လiu မရBiuင#ပ?ဘR&။ eရ&သ1&မယ#ဆiuရင# eအ1က#ပ?


အတiuင#& eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။

int [] array;
array = new int[] {1,2,3,4,5};

တကယ#eတ15 new int[] {1,2,3,4,5} လiueရ&တ1က Array Object ကiu Instantiate လuပ#တ85 eရ&သ1&ပuPအမ9န# *ဖစ#ပ?
တယ#။ Java မ91က Declaration Statement eတ>မ91 အလ>ယ#eရ&သ1&Biuင#eအ1င# {1,2,3,4,5} ကiu Short Cut
အeနန8A *ပင#eပ&ထ1&တ1*ဖစ#6ပ7& Declaration Statement eတ>မ91ပ8 eရ&သ1&လiuရမ91 *ဖစ#ပ?တယ#။

eန-က)မ2 Program ထ9ကeန Element eတ#ကiu Set လuပ)လiuတ9? အခA


တစ#ခ?တစ#eလက(ရင# Array ထ8မ91 ဘ1eတ>Dieနမယ#ဆiuတ1 Xကiu6ပ7&သiမeနပ8 User Input eတ>ကeန ရယRလ1တ85
Data eတ>ကiu Array ထ8မ91 သiမ#&ထ1&လiuတ85 အခ?မ(iu&eတ> Diတတ#ပ?တယ#။ အ85ဒ7လiuအခ?မ(iu&eတ>မ91 Array Object ကiu
Xကiueဆ1က#ထ1&ဖiuလiuအပ#ပ?တယ#။

တစ#ခu သတiထ1&ရမ91က Array Object ကiu တည#eဆ1က#လiuတ85အခ?မ(iu&မ91 Element ဘယ#B9စ#လuP&ကiu အသuP&*ပuမယ#


ဆiuတ1ကiu Xကiu6ပ7& သတ#မ9တ#ထ1&ဖiuလiuအပ#ပ?တယ#။

int
Learning [] array = new int [5];
Java 48 မင#&လ>င#
Java Developer Class
String 10 ခuကiu အသuP&*ပuခ(င#တ85 Array မ(iu&ဆiuရင# eအ1က#ပ? အတiuင#&eရ&သ1&ရပ?မယ#။

String [] array = new String [10];

Array မ91 Java Language မ91ပ?တ85 Data Type eတ>သ1မက မiမiကiuယ#တiuင#eရ&သ1&ထ1&တ85 Custom Object
eတ>ကiuလ8 Type အeနန8A အသuP&*ပuBiuင#ပ?တယ#။ Student Type eတ>ကiu Array မ91 အသuP&*ပuခ(င#တယ#ဆiuရင#လ8
အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။

public class ArrayDemo {

public static void main(String[] args) {

int array [] = new int[3];

Student [] students = new Student[3];

}
}

class Student {
String name;
int age;
}

အထက#နမRန1ထ8မ91လiu new int[10] လiueရ&လiuက#တ1န8A int ရ8S Default တန#ဖiu&န8A {0, 0, 0} ကiu တည#eဆ1က#သ>1&
မ91 *ဖစ#6ပ7& new Student[3] လiueရ&လiuက#တ1န8A {null, null, null} ဆiu6ပ7& Array Object ကiuတည#eဆ1က#eပ&မ91
*ဖစ#ပ?တယ#။ students Array ထ8မ91 Student Object ၃ ခuကiu သiမ#&eပ&ထ1&Biuင#မ91 *ဖစ#ပ?တယ#။

Learning Java 49 မင#&လ>င#


Java Developer Class

Accessing Array Elements


Array Object eတ>ဟ1 Element eတ>ကiu Index Number eတ>န8A သတ#မ9တ#6ပ7& သiမ#&eပ&ထ1&Biuင#မ91 *ဖစ#တယ#။
Array eတ>ကiu အသuP&*ပuမယ#ဆiuရင# Array ထ8ကiu Element eတ> ထည#56ပ7& သiမ#&ထ1&တတ#ဖiuလiuအပ#သလiu Array ထ8
မ91 သiမ#&ထ1&တ85 Element eတ>ကiuလ8 *ပန#6ပ7& eခLယR အသuP&*ပuတတ#ဖiuလiuအပ#ပ?တယ#။

Array ထ8မ91 Element eတ>ကiu သiမ#&ထ1&မယ#ဆiuရင#လ8 Index Number eတ>န8A သiမ#&ထ1&ဖiuလiuအပ#သလiu *ပန#6ပ7&
eခLယRမယ#ဆiuရင#လ8 Index Number န8A *ပန#6ပ7&eခLယR အသuP&*ပuရမ91 *ဖစ#ပ?တယ#။

public class ArrayDemo {

public static void main(String[] args) {

Student [] students = new Student[3];

students[0] = new Student("Aung Aung", 20);


students[1] = new Student("Maung Maung", 21);
students[2] = new Student("Thidar", 25);
}
}

class Student {
String name;
int age;

public Student(String name, int age) {


[Link] = name;
[Link] = age;
}
}

Array ကiu Declare လuပ#စ]# Type မ(iu&ဆiuပ?က Array ထ8မ91 လ16ပ7& သiမ#&ထ1&Biuင#မ91 *ဖစ#ပ?တယ#။ Student []
students လiu Declare လuပ#ထ1&တ85 အတ>က# students Array ထ8မ91 Student Object eတ>န8A Student Class ရ8S
Sub Class Object eတ>ဆiuရင# လ16ပ7& သiမ#&ထ1&လiuရပ?မယ#။

တဖန# Array ကiu Object eဆ1က#ထ1&သeလ1က# အeရအတ>က# သiမ#&eပ&ထ1&Biuင#မ91 *ဖစ#တယ#။ new Student
[3] လiueရ&ထ1&တ85 အတ>က# Array မ91 အခန#& ၃ ခန#&ပiuင#6ပ7& Student Object ၃ ခuသiမ#&eပ&ထ1&Biuင#မ91 *ဖစ#ပ?တယ#။
Array ရ8S Index Number ဟ1 0 ကeန စတ1*ဖစ#တ85 အတ>က# student[0] ဆiu6ပ7& eရ&မယ#ဆiuရင# eUဆuP&အခန#&မ91
Student Object တစ#ခuကiu လ1သiမ#&မ91 *ဖစ#ပ?တယ#။

တကယ#လiu Diတ85 အခန#&ထက#eက(1#6ပ7& Array ကiu Access လuပ#မiရင# ArrayIndexOutOfBoundsException ကiu


*ဖစ#eပLeစမ91 *ဖစ#ပ?တယ#။ students[-1] တiu students[3] တiuကiu Access လuပ#ရင# အထက#ပ? Exception ကiu *ဖစ#
eပLeစမ91ပ8 *ဖစ#ပ?တယ#။

Learning Java 50 မင#&လ>င#


Java Developer Class

public class ArrayDemo {

public static void main(String[] args) {

Student [] students = new Student[3];

students[0] = new Student("Aung Aung", 20);


students[1] = new Student("Maung Maung", 21);
students[2] = new Student("Thidar", 25);

Student firstStudent = students[0];


[Link]([Link]);

Student secondStudent = students[1];


[Link]([Link]);

Student lastStudent = students[2];


[Link]([Link]);
}
}

class Student {
String name;
int age;

public Student(String name, int age) {


[Link] = name;
[Link] = age;
}
}

အထက#ပ?အတiuင#& Array ထ8က Element eတ>ကiu *ပန#ယRခ(င#တယ#ဆiuရင#လ8 သiမ#&ထ1&တ85 index number န8Aပ8 *ပန#
6ပ7& eခLယRရမ91 *ဖစ#ပ?တယ#။ students[0] ဆiuရင# Array ထ8က ပထမဆuP& Student ကiu ရDiBiuင#6ပ7& students[1] ဆiu
ရင# ဒuတiယ Student Object ကiuရDiမ91 *ဖစ#ပ?တယ#။ students[2] ဆiuရင#eတ15 eန1က#ဆuP& Student ကiuရDiBiuင#မ91
*ဖစ#ပ?တယ#။

ဒ7eနရ1မ91လ8 မDiတ85 index န8A Access လuပ#မiမယ#ဆiuရင# ArrayIndexOutOfBoundsException ကiu *ဖစ#ပ>1&eစမ91


*ဖစ#ပ?တယ#။

Learning Java 51 မင#&လ>င#


Java Developer Class

Multi Dimension Arrays


Array ဆiuတ1က Java Data eတ>ကiu တစ#ခuထက#မက အစ7အစ]#လiuက# သiမ#&eပ&ထ1&Biuင#တ85 Container Object
တစ#မ(iu&*ဖစ#ပ?တယ#။ Multi Dimension Array ဆiuတ1က Array Object eတ>ကiu သiမ#&eပ&ထ1&Biuင#တ85 Array
Object တစ#မ(iu&သ1 *ဖစ#ပ?တယ#။ ဒ?5e0က1င#5 Multi Dimension Array ထ8မ91 အ*ခ1&eသ1 Array Object eတ>ကiu
သiမ#&eပ&ထ1&Biuင#မ91 *ဖစ#ပ?တယ#။

Multi Dimension Array ကiu ဘယ)လiu Declare လuပ)ရလ9


Multi Dimension Array အမ(iu&အစ1& Variable တစ#ခuကiu eရ&သ1& အသuP&*ပuလiuတ85 အခ?မ91 Variable ကiu Declare
လuပ#တတ#ဖiuလiuပ?တယ#။ Multi Dimension Array ဆiuတ1 Array eတ>ကiu သiမ#&eပ&ထ1&Biuင#တ85 Array *ဖစ#တ85
အတ>က# အသuP&မ*ပuခင# ဘယ#လiu အမ(iu&အစ1& Array ကiu အသuP&*ပuမ91လ8 ဆiuတ1ကiu သiထ1&ဖiuလiuပ?တယ#။

ဆiu0ကပ?စiu၊ int array eတ>ကiu သiမ#&eပ&ထ1&Biuင#တ85 Multi Dimension Array မ(iu&၊ ဒ?မ9မဟuတ# String Array eတ>ကiu
သiမ#&eပ&ထ1&Biuင#တ85 Array မ(iu&ဆiu6ပ7& သiDieနဖiuလiuအပ#ပ?တယ#။

int [][] array; // Array of int array


String [][] strArray; // Array of String Array

သ1မန# Array eတ>ကiu eရ&ခ85တuန#&ကလiu ဘယ#လiuအမ(iu&အစ1& Array ဆiuတ1ကiu သiဖiuလiuအပ#6ပ7&၊ အ85ဒ7 Type ကiu
eUဆuP&မ91 eရ&eပ&ရပ?မယ#။ 6ပ7&ရင# Array Sign *ဖစ#တ85 [] ကiu အသuP&*ပuလiuသeလ1က# eရ&သ1&eပ&ရပ?မယ#။ Two
Dimension ဆiuရင# [][] လiueရ&6ပ7& Three Dimension ဆiuရင#eတ15 [][][] လiu eရ&သ1&eပ&ရပ?မယ#။ 6ပ7&က1မ9
Identifier *ဖစ#တ85 Variable Name ကiu eရ&သ1&ရမ91ပ?။

ဒ?eပမ85 Multi Dimension Array ကiu Declare လuပ#တ85 eနရ1မ91 eအ1က#ပ?အတiuင#& eရ&သ1&ရင#လ8 Compile
Error *ဖစ#မ91 မဟuတ#ပ?ဘR&။

int [][] array; မင#&လ>င#


Learning Java 52
int array [][];
int [] array [];
Java Developer Class
Identifier *ဖစ#တ85 Array Name ကiu Array Sign eတ>ရ8S eUမ91eက1၊ eန1က#မ91eက1၊ 0က1&မ91ပ? eရ&သ1eဖL*ပBiuင#
မ91 *ဖစ#ပ?တယ#။ ဒ?eပမ85 Appication တစ#ခuထ8မ91 ဟiuလiueရ&လiuက# ဒ7လiueရ&လiuက#ဟ1 eက1င#&တ85 အက(င#5 တစ#ခu
မဟuတ#ပ?ဘR&။ Application တစ#ခuကiu eရ&သ1&တ85 eနရ1မ91 မiမiတစ#eယ1က#ထ8 eရ&သ1&eနတ1မဟuတ#ပ8 အ*ခ1&သR
eတ>ပ? ပ?ဝင#eရ&သ1&eနBiuင#တယ#ဆiuတ1ကiu သတiထ1&သင#5ပ?တယ#။

ဒ?5e0က1င#5 ဖတ#ရလ>ယ#6ပ7& န1&လည#ရလ>ယ#ကReအ1င# အ6မ8သတiထ1&6ပ7& eရ&သ1&သင#5ပ?တယ#။

ဘယ)လiu Instantiate လuပ)8ကမလ9


Instantiate လuပ#တယ#ဆiuတ1က Object eဆ1က#တ1ကiu e*ပ1တ1ပ?။ ဒ7eနရ1မ91 Multi Dimension Array တစ#ခu
ကiu ဘယ#လiu Object eဆ1က#0ကမလ8ဆiuတ1ကiu eလ5လ1သ>1&0ကရeအ1င#။ Array eတ>ကiu eလ5လ1ခ85တuန#&ကလiuပ?ပ8၊
Object တည#eဆ1က#လiuတ85 အခ?မ91 ပ?ဝင#မ85 Element eတ>ကiu Xကiu6ပ7& သiထ1&Biuင#သလiu၊ မသiထ1&Biuင#တ85 အခ?မ(iu&လ8
Diပ?လiမ#5မယ#။ အရင#ဆuP& ပ?ဝင#မည#5 Element eတ>ကiu Xကiuသiထ1&ရင# ဘယ#လiu Instantiate လuပ#မယ#ဆiuတ1ကiu
eလ5လ10ကည#50ကရeအ1င#။

int [][] array = {{1,2}, {3,4}};

အထက#ပ?အတiuင#& eရ&သ1&ထ1&တ1ဟ1 int array eတ>ကiuသiမ#&ထ1&Biuင#တ85 array Object တစ#ခiuကiu အရင#ဆuP&


Declare လuပ#ပ?တယ#။ 6ပ7&မ9 {1,2} ပ?တ85 int Array န8A {3,4} ပ?တ85 int Array ကiu အသuP&*ပu6ပ7& int Array eတ>ကiu
သiမ#&ထ1&Biuင#တ85 Array Object တစ#ခuကiu တည#eဆ1က#သ>1&တ1*ဖစ#ပ?တယ#။

int [][] array = {{1,2}, {3,4,5}};

အထက#ပ?ကuဒ#ကiu 0ကည#50ကည#5ရeအ1င#။ Multi Dimension Array eနရ1ကiu Dimension မတRတ85 Array eတ>န8A
လ16ပ7& အစ1&ထiu&eနပ?တယ#။ ဒ?eပမ85 ဒ7လiueရ&ထ1&ရင#လ8 မ91&မ91မဟuတ#ပ?ဘR&။ Java မ91 အသuP&*ပueနတ85 Array ဟ1
C တiu C++ တiuမ91 အသuP&*ပueနတ85 Array န8A မတRပ8 Object တစ#ခuသ1 *ဖစ#တ85 အတ>က# Int Array *ဖစ#မယ#ဆiuရင#
အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။ Dimension eတ>ဆiuတ1ထက# Int အမ(iu&အစ1& Array eတ>ကiu သiမ#&eပ&ထ1&Biuင#တ85
Array eတ>လiu *မင#တ1eက1င#&ပ?မယ#။

Array ထ8မ91 ပ?မည#5 Element eတ>ကiu Xကiu6ပ7& မသiထ1&တ85 အခ?မ(iu&မ91 ဆiuရင#လ8 eအ1က#ပ?အတiuင#& Array Object
မ(iu&ကiu တည#eဆ1က#Biuင#ပ?မယ#။

int [][] array1 = new int[2][3];


int [][] array2 = new int[2][];

အထက#ပ?အတiuင#& new int[2][3] ဆiu6ပ7& Initialize လuပ#မယ#ဆiuရင# {{0,0,0}, {0,0,0}} ဆiu6ပ7& Array Object ကiu
တည#eဆ1က#သ>1&မ91 *ဖစ#ပ?တယ#။ တဖန# new int[2][] လiu eရ&မယ#ဆiuရင#လ8 *ဖစ#ပ?တယ#။ ဒ7အတiuင#& eရ&သ1&ထ1&
မယ#ဆiuရင#eတ15 {null, null} ဆiu6ပ7& Array Object ကiu တည#eဆ1က#သ>1&ပ?မယ#။

Learning Java 53 မင#&လ>င#


Java Developer Class
Int [2][] လiueရ&ထ1&တ1က int [] ၂ ခuပ?တ85 Array Object လiu eဖL*ပeနတ1*ဖစ#ပ?တယ#။ int [2][] လiueရ&ထ1&တ85
အတ>က# အထ8မ91ပ?တ85 Array ကiu Initialize မလuပ#Biuင#ပ?ဘR&။ Array ဆiuတ1လ8 Object *ဖစ#တ85 အတ>က# Object ရ8S
Default တန#ဖiu&*ဖစ#တ85 null eတ>န8A Object ကiu တည#eဆ1က#သ>1&တ1 *ဖစ#ပ?တယ#။

Multi Dimension Array ကiu ဘယ#လiueဆ1က#ရမလ8ဆiuတ1ကiu eလ5လ16ပ7&တ85 eန1က#မ91 Array ထ8က Element
eတ>ကiu ဘယ#လiu Access လuပ#0ကမယ#ဆiuတ1ကiu ဆက#လက# eလ5လ1သ>1&0ကရeအ1င#။

Multi Dimension Array Member eတ#ကiu ဘယ)လiu Access လuပ)မ2-လ9


Member eတ>ကiu Access လuပ#တယ#လiu e*ပ1တ85 eနရ1မ91 Member eတ>ကiu Asign လuပ#တ1လ8ပ?သလiu၊ Member
eတ>ရ8S တန#ဖiu&ကiu ရယRတ1လ8 ပ?ပ?တယ#။ သ1မန# Array eတ>တuန#&လiuပ8 Multi Dimension Array eတ>ရ8S Member
eတ>ကiu Access လuပ#တ85 eနရ1မ91 index Number ကiu အသuP&*ပu6ပ7& Access လuပ#ရမ91 *ဖစ#ပ?တယ#။

jshell> int [][] array = {{1,2},{3,4}}


array ==> int[2][] { int[2] { 1, 2 }, int[2] { 3, 4 } }

jshell> int a = array[0][0]


a ==> 1

jshell> int b = array[0][1]


b ==> 2

jshell> int c = array[0][2]


Primitive Wrapper Argument Types of Constructor
| [Link] thrown: 2
boolean| at (#5:1)
Boolean boolean or String
char Character char
byte Byte byte or String
short Short short or String
int Integer int or String
long Long long or String
float Float float, double or String
double Double double or String

အထက#ပ?အတiuင#& Multi Dimension Array တစ#ခuကiu jshell ကiu အသuP&*ပu6ပ7& တည#eဆ1က#0ကည#5ပ?မယ#။ အထက#
နမRန1ပ? int [][] array ဟ1 int [] eတ>ကiu သiမ#&eပ&ထ1&Biuင#တ85 Array တစ#မ(iu&သ1&*ဖစ#ပ?တယ#။ array ထ8မ91 int []
eတ> *ဖစ#0ကတ85 {1,2} န8A {3,4} ကiu သiမ#&eပ&ထ1&ပ?တယ#။

ဒ?5e0က1င#5 array [0][0] လiue*ပ1ရင# eUဆuP& Array ထ8က ပထမဆuP&eနရ1မ91 Diတ85 1 ကiu ရDiမ91 *ဖစ#ပ?တယ#။ တဖန#
array[0][1] လiueဖL*ပရင# eUဆuP& Array ထ8က ဒuတiယeနရ1မ91Diတ85 2 ကiu ရDiမ91 *ဖစ#ပ?တယ#။ ဒ?eပမ85 array[0][2]
လiueရ&လiuက#တ85အခ?မ91 eUဆuP& Array ရ8S တတiယeနရ1ဟ1 မDiတ85 အတ>က#
ArrayIndexOutOfBoundsException ကiu *ဖစ#eပLeစတ1 *ဖစ#ပ?တယ#။ သ1မန# Array eတ>တuန#&ကလiuပ8 မDiတ85
Index Number ကiu Access လuပ#မiရင# ArrayIndexOutOfBoundsException ကiu *ဖစ#eပLeစမ91 *ဖစ#ပ?တယ#။

Learning Java 54 မင#&လ>င#


Java Developer Class

jshell> array[0][0] = 10
$6 ==> 10

jshell> a = array[0][0]
a ==> 10

jshell> array[0][2] = 15
| [Link] thrown: 2
| at (#9:1)

Multi Dimension Array ထ8မ91Diတ85 တန#ဖiu&ကiu Assign လuပ#လiuတ85 အခ?မ(iu&မ91လ8 Index Number eတ>ကiuပ8
အသuP&*ပuရမ91 *ဖစ#ပ?တယ#။ array[0][0] = 10 လiueရ&6ပ7& *ပန#0ကည#5တ85အခ?မ91 တန#ဖiu&ဟ1 10 ကiu e*ပ1င#&သ>1&တ1ကiu
eတ>Zရပ?မယ#။ မDiတ85 Index ကiu အသuP&*ပuမiရင#လ8 ArrayIndexOutOfBoundsException ကiu *ဖစ#eပLeစမ91 *ဖစ#ပ?
တယ#။
Multi Dimension Array ထ8က တန#ဖiu&eတ>လiue*ပ1တ85eနရ1မ91 eအ1က#ဆuP& Element အထi Access လuပ#Biuင#
သလiu Array eတ>ကiuလ8 *ပန#6ပ7& Access လuပ#Biuင#ပ?တယ#။

jshell> int [] array2 = array[1]


array2 ==> int[2] { 3, 4 }

jshell> array[0] = new int[]{30,40}


$11 ==> int[2] { 30, 40 }

jshell> array
array ==> int[2][] { int[2] { 30, 40 }, int[2] { 3, 4 } }

အထက#ပ?အတiuင#& Multi Dimension Array ကiu First Level Index ကiu အသuP&*ပu6ပ7& Access လuပ#မယ#ဆiuရင# အ85ဒ7
Array ထ8မ91 သiမ#&eပ&ထ1&တ85 Array ကiu *ပန#လည#ရDiBiuင#မ91 *ဖစ#ပ?တယ#။ array[1] ဆiu6ပ7& Access လuပ#ရင# ဒuတiယ
eနရ1မ91 Diတ85 Array Object ကiu *ပန#လည#ရDiမ91 *ဖစ#ပ?တယ#။ အ85ဒ7အတiuင#&ပ8 array[0] ရ8Seနရ1ကiu အ*ခ1& int
Array တစ#ခuန8A အစ1&ထiu&Biuင#မ91 *ဖစ#ပ?တယ#။

ဒ?eပမ85 Type မတRရင#eတ15 Asign လuပ#တ1eက1 Reference လuပ#တ1eက1 လuပ#လiuရမ91 မဟuတ#ပ?ဘR&။ array
variable ရ8S type ကiu int [][] လiu Declare လuပ#ထ1&တ85 အတ>က# First Level Element ဟ1 int [] ပ8 *ဖစ#eနမ91 *ဖစ#
ပ?တယ#။ 6ပ7&eတ15 Second Level Element ရ8S type ဟ1လ8 int ပ8*ဖစ#ရပ?မယ#။
အထက#ပ?အတiuင#& int data eနရ1မ91 array[0] န8A အစ1&ထiu&လiuမရပ?ဘR&။ အစ1&ထiu&ခ(င#တ85 data ရ8S type ဟ1 int
*ဖစ#6ပ7& array[0] လiue*ပ1ရင# ရDiBiuင#မည#5 type ဟ1 int [] *ဖစ#eနတ85 အတ>က#*ဖစ#ပ?တယ#။ တဖန# array [1] eနရ1ကiu
လ8 10 ဆ7uတ85 တန#ဖiu&န8A အစ1&ထiu&လiu မရBiuင#ပ?ဘR&။ ဘ1လiuလ8 ဆiueတ15 array[1] ဟ1 int [] *ဖစ#6ပ7& 10 ဟ1 int *ဖစ#
တ85 အတ>က# *ဖစ#ပ?တယ#။

Java Programming Language မ91က Variable တစ#ခuကiu Declare လuပ#ခ85တ85 Type န8A မတRတ85 တန#ဖiu&eတ>ကiu
အစ1&ထiu&လiu မရBiuင#ပ?ဘR&။

Learning Java 55 မင#&လ>င#


Java Developer Class

jshell> int data = array[0]


| Error:
| incompatible types: int[] cannot be converted to int
| int data = array[0];
| ^------^

jshell> array[1] = 10
| Error:
| incompatible types: int cannot be converted to int[]
| array[1] = 10
| ^^

Operators
ကOန#eတ1#တiu eUခန#&eတ>မ91 Class eတ>၊ Variable eတ>၊ Method eတ>န8A Java မ91 အသuP&*ပuBiuင#တ85 Data Type
eတ>အe0က1င#&eလ5လ1ခ850ကပ?တယ#။ ဒ7တစ#eခ?က#eတ15 Variable eတ>န8A တ>86ပ7& eရ&သ1&Biuင#တ85 Operator eတ>န8A
Expression eတ> အe0က1င#&ကiu eလ5လ10ကပ?မယ#။

Operator ဆiuတ1ကeတ15 Variable eတ>ကiu ဘ1လuပ#မ91လ8ဆiuတ1ကiu eဖL*ပeပ&Biuင#တ85 အရ1eတ>*ဖစ#6ပ7&eတ15


Expression ဆiuတ1ကeတ15 Variable eတ>န8A Operator eတ>ကiu အသuP&*ပu6ပ7& eရ&သ1&ရမည#5 eဖL*ပခ(က#eတ> *ဖစ#
0ကပ?တယ#။ JVM က Expression eတ>ကiu န1&လည#6ပ7& OS ကeန တဆင#5 Computer ကiu လuပ#eဆ1င#မ-eတ>ကiu
mnန#0က1&ခ(က#eတ> eပ&Biuင#မ91 *ဖစ#ပ?တယ#။

ဆiu0ကပ?စiuဗ(1။ Variable a န8A Variable b တiuကiu eပ?င#&eစခ(င#တယ#ဆiuရင# eပ?င#&ပ?ဆiuတ1ကiu Operator န8A eဖL*ပeပ&
ရမ91 *ဖစ#တယ#။ တဖန# Variable တစ#ခuဟ1 Type တစ#ခuရ8S Object *ဖစ#ရ8Sလ1&ဆiuတ1ကiu စစ#ခ(င#တယ#၊ Variable
eတ>ကiu တစ#တiu&တ1တiu တစ#B-တ#တ1တiu လuပ#ခ(င#တယ#၊ eန1က#6ပ7& Variable B9စ#ခuရ8S တန#ဖiu&ဟ1 တRမတR စစ#eဆ&
ခ(င#တယ#၊ Variable eတ>ကiu အသuP&*ပu6ပ7& Logic eတ>ကiu စစ#eဆ&ခ(င#တယ#၊ Variable B9စ#ခuဘယ#သRXက7&လ8 ငယ#
လ8ဆiuတ1ကiu သiခ(င#တယ#၊ ဒ7လiuအခ?မ(iu&eတ>မ91 Operator eတ>ကiu အသuP&*ပuရမ91 *ဖစ#ပ?တယ#။

Java ဘ1သ1ရပ#မ91 အသuP&*ပuBiuင#တ85 Operators eတ>ကeတ15 eအ1က#ပ? အတiuင#&*ဖစ#0ကပ?တယ#။


Bitwise Operator ဆiuတ1Diပ?eသ&တယ#။ ဒ?eပမ85 Binary တန#ဖiu&eတ>ကiuတ>က#ခ(က#တ85 eနရ1မ91 အသuP&*ပuတ1ပ?။
ဒ?eပမ85 Business Application eတ>ကiu eရ&သ1&တ85 eနရ1မ91 သiပ#6ပ7& မသuP&တ85 အတ>က# ဒ7စ1အuပ#ထ8မ91eတ15 ထည#5
မeရ&eတ15ပ?ဘR&။

Learning Java 56 မင#&လ>င#


Java Developer Class

Assignment Operator
Assignment Operator eတ>ကeတ15 Variable eနရ1မ91 အစ1&ထiu&eပ&Biuင#တ85 Operator မ(1&*ဖစ#0ကပ?တယ#။
သ1မန# “=“ တစ#ခuကiu အသuP&*ပu6ပ7& eရ&သ1&Biuင#သလiu အ*ခ1&eသ1 Arithmetic Operator eတ>န8Aလ8 တ>8ဖက#6ပ7&
eရ&သ1&Biuင#ပ?တယ#။

Assignment in Primitive Type Variables


= Assignment Operator eတ>ကiueတ15 Primitive Variable eတ>ကiueရ1 Reference Variable eတ>မ91ပ?
အစ1&ထiu&တ85 eနရ1မ91 အသuP&*ပuBiuင#ပ?တယ#။ ဒ?eပမ85 Variable Type အeပLမRတည#6ပ7& လuပ#eဆ1င#ပuPခ(င#& မတRည70က
ပ?ဘR&။

jshell> int a
a ==> 0

jshell> a = 10
a ==> 10

jshell> int b = a
b ==> 10

jshell> a = 15
a ==> 15

jshell> b
b ==> 10

အထက#ပ?အတiuင#& JShell န8A eရ&သ1&ထ1&တ85 Code Snippet eတ>ကiu eလ5လ10ကည#50ကရeအ1င#။ အရင#ဆuP& int a
ဆiu6ပ7& Variable တစ#ခuကiu Declare လuပ#ပ?တယ#။ Primitive Type Variable တစ#ခuပ?။

ထiueန1က# a = 10 လiueရ&ထ1&ပ?တယ#။ a variable ရ8S တန#ဖiu&ကiu 10 န8A အစ1&ထiu&တ1*ဖစ#ပ?တယ#။ a ရ8S တန#ဖiu&ဟ1


10 *ဖစ#သ>1&ပ?မယ#။ တဖန# int b = a လiu eရ&ထ1&တ1ကiu eတ>Zရပ?မယ#။ ဒ7စ1e0က1င#&ဟ1 int type variable b ရ8Sတ
ဖiu&ကiu a ရ8S တန#ဖiu&န8A အစ1&ထiu&eနတ1 *ဖစ#ပ?တယ#။ ဒ7eနရ1မ91 a ရ8S တန#ဖiu&ဟ1 10 *ဖစ#တ85 အတ>က# b ရ8S တန#ဖiu&ဟ1
လ8 10 *ဖစ#သ>1&ပ?မယ#။

တဖန# a = 15 ဆiu6ပ7& အစ1&ထiu&*ပန#တယ#။ ဒ7eနရ1မ91 a ရ8S တန#ဖiu&သ1 e*ပ1င#&သ>1&မ91 *ဖစ#ပ?တယ#။ b eနရ1ကiu a


က အစ1&ထiu&ထ1&လiuဆiu6ပ7& b ရ8S တန#ဖiu&ဟ1 လiuက#မe*ပ1င#&သ>1&ပ?ဘR&။ b ရ8S တန#ဖiu&ဟ1 နဂiuမRလအတiuင#& 10 ပ8 *ဖစ#
eနမ91 *ဖစ#ပ?တယ#။ Primitive Type Variable eတ>မ91 Assignment eတ>ကiu လuပ#တ85 အခ?မ91 Actual Value ကiu
အစ1&ထiu&0ကတ1 *ဖစ#တ85 အတ>က# *ဖစ#ပ?တယ#။

ဆက#လက#6ပ7& Reference Type Variable eတ>ကiu Assign လuပ#တ1ကiu eလ5လ10ကည#50ကရeအ1င#။

Learning Java 57 မင#&လ>င#


Java Developer Class

Assignment in Reference Type Variables


Assignment Operator *ဖစ#တ85 = ကiu Reference Type Variable eတ>ကiu အစ1&ထiu&တ85 eနရ1မ91လ8 အသuP&*ပuBiuင#
မ91 *ဖစ#ပ?တယ#။ ဒ?eပမ85 Reference Type eတ>ရ8S အစ1&ထiu&ပuPက Primitive Type eတ>န8A မတRပ?ဘR&။ Primitive
Type Variable eတ>မ91 Actual Value ကiu အစ1&ထiu&တ1မ9န#eပမ85 Reference Type Variable eတ>မ91eတ15
Variable ကeန Reference လuပ#မ85 Object ရ8S Memory Address ကiuသ1 အစ1&လ1ထiu&မ91 *ဖစ#ပ?တယ#။

Variable မ91လ1အစ1&ထiu&ထ1&တ85 Memory Address ကeန တဆင#5 Object eတ>ကiu Reference လuပ#6ပ7&
အသuP&*ပueနတ1 *ဖစ#တ85 အတ>က# Reference Variable eတ>လiu eခLခ850က*ခင#&လ8 *ဖစ#ပ?တယ#။
အထက#ပ?နမRန1မ91 အရင#ဆuP& String Type Variable တစ#ခuပ?တ85 Person Class ကiu တည#eဆ1က#ပ?တယ#။ 6ပ7&
eတ15မ9 Person Type Variable *ဖစ#တ85 thidar ဆiuတ85 eနရ1မ91 new Person() ဆiu6ပ7& Person Object ကiu
တည#eဆ1က#6ပ7&eတ15 Assign လuပ#eနပ?တယ#။ ဒ7eနရ1မ91 Person Type ဟ1 Reference Type *ဖစ#တ85 အတ>က#
new Person() လiu Object ကiu Heap Memory eပLမ91တည#eဆ1က#6ပ7& အ85ဒ7 Object ရ8S Memory Address ကiu
thidar eနရ1မ91 အစ1&ထiu&eနတ1 *ဖစ#ပ?တယ#။

jshell> class Person {


...> String name;
...> }
| created class Person

jshell> Person thidar = new Person();


thidar ==> Person@6fd02e5

jshell> [Link] = "Thidar"


$3 ==> "Thidar"

jshell> Person nilar = thidar;


nilar ==> Person@6fd02e5

jshell> [Link] = "Nilar"


$5 ==> "Nilar"

jshell> [Link]
$6 ==> "Nilar"

jshell> [Link]
$7 ==> "Nilar"

တဖန# Person nilar = thidar လiueရ&ထ1&တ1ကလ8 Assignment Operator ကiu အသuP&*ပu6ပ7& nilar eနရ1ကiu
thidar ရ8S တန#ဖiu&န8A အစ1&ထiu&eန*ပန#တယ#။ ဒ7eနရ1မ91 thidar မ91Dieနတ85 တန#ဖiu&ဟ1 Person Object ကiu
Reference လuပ#Biuင#တ85 Memory Address ကiuပ8 nilar eနရ1မဟ1 အစ1&ထiu&သ>1&*ပန#တယ#။ Object တစ#ခuထ8ကiu
Reference Variable B9စ#ခuကeန Reference လuပ#eနတယ#ဆiuတ1ကiu သiDiထ1&သင#5ပ?တယ#။ ဒ?5e0က1င#5မiuလiu
[Link] = “Nilar” လiuအစ1&ထiu&လiuက#တ85 အခ?မ91 [Link] ဟ1လ8 “Nilar” လiue*ပ1င#&သ>1&တ1 *ဖစ#ပ?
တယ#။

Learning Java 58 မင#&လ>င#


Java Developer Class
Primitive Type Variable eတ>ကiu Assign လuပ#တ85အခ?မ91 Actual Value ကiu အစ1&ထiu&6ပ7& Reference Type
Variable eတ>ကiu Assign လuပ#တ85အခ?မ91 Heap Memory eပLမ91Diတ85 Object ကiu Reference လuပ#Biuင#တ85
Memory Address ကiuသ1 အစ1&ထiu&သ>1&တယ#ဆiuတ1ကiu သiDiထ1&ဖiuလiuအပ#ပ?တယ#။

Assignment Operators *ဖစ#တ85 = န8A Arithmetic Operators eတ>န8A တ>8ဖက#6ပ7&လ8 အသuP&*ပuBiuင#ပ?တယ#။ ဒ7


အe0က1င#&ကiueတ15 Arithmetic Operators eတ>အခန#&မ91 eဖL*ပသ>1&ပ?မယ#။

Arithmetic Operators
Number အမ(iu&အစ1& Primitive Type Variable eတ>ကiu အသuP&*ပu6ပ7& သခo(1 ပiuင#&ဆiuင#ရ1 တ>က#ခ(က#မ-eတ> *ပuလuပ#
လiuတ85 အခ?မ(iu&မ91 Arithmetic Operators eတ>ကiu အသuP&*ပuBiuင#ပ?တယ#။ အeပ?င#&၊ အB9တ#၊ အe*မp1က#၊ အစ1&န8A
အqကင#&D1တ85 တ>က#ခ(က#မ-eတ>ကiu *ပuလuပ#လiuတ85 အခ?မ(iu&မ91 Arithmetic Operators eတ>ကiu အသuP&*ပuBiuင#ပ?တယ#။

Arithmetic Operators eတ>န8A အသuP&*ပuBiuင#တ85 Data Type eတ>ကeတ15 Number အမ(iu&အစ1& Primitive Data
Type eတ>န8A သRတiuရ8S Wrapper Class eတ>မ91 အသuP&*ပuBiuင#ပ?တယ#။ 6ပ7&eတ15 Number အeနန8A eဖL*ပBiuင#တ85
Primitive Data Type *ဖစ#တ85 char type eတ>မ91လ8 အသuP&*ပuBiuင#ပ?တယ#။ ခOင#&ခ(က#အeနန8A String Type
Variable eတ>မ91လ8 + Operator ကiu အသuP&*ပuBiuင#ပ?တယ#။

jshell> int a = 10
a ==> 10

jshell> int b = 20
b ==> 20

jshell> a + b
$3 ==> 30

အထက#ပ? နမRန1ထ8မ91 အရင#ဆuP& a eနရ1မ91 10 ကiu အစ1&ထiu&ပ?တယ#။ 6ပ7&eတ15 b eနရ1မ91 20 ကiu အစ1&ထiu&ပ?
တယ#။ a + b လiu eရ&လiuက#တ85 အခ?မ91 30 ရပ?တယ#။ Number အမ(iu&အစ1& Variable eတ>ရ8S eပ?င#&*ခင#&ကiu +
Operator ကiu အသuP&*ပuBiuင#ပ?တယ#။

jshell> char a = 'a' + 'b'


a ==> 'Ã'

jshell> Character c = a
c ==> 'Ã'

jshell> char d = a + c
| Error:
| incompatible types: possible lossy conversion from int to char
Learning| Java
char d = a + c; 59 မင#&လ>င#
| ^---^
Java Developer Class
အထက#ပ? နမRန1ကuဒ#ထ8မ91လiu Number တန#ဖiu&အeနန8A e*ပ1င#&6ပ7& အသuP&*ပuBiuင#တ85 char type eတ>မ91လ8 +
Operators eတ>ကiu အသuP&*ပuBiuင#ပ?တယ#။ ဒ?eပမ85 တစ#ခu သတi*ပuဖiu လiuအပ#တ1က Wrapper Class *ဖစ#တ85
Character Class eတ>မ91eတ15 Arithmetic Operator eတ>ကiu အသuP&*ပuလiu ရမ91 မဟuတ#ပ?ဘR&။

jshell> String hello = "Hello"


hello ==> "Hello"

jshell> String java = "Java"


java ==> "Java"

jshell> hello + " " + java


$15 ==> "Hello Java"

အထက#ပ? နမRန1ကeတ15 String Type Variable eတ>0က1&မ91 + Operator ကiu အသuP&*ပuတ1ပ?။ ခOင#&ခ(က#အeနန8A
Object eတ>မ91 String တစ#ခuထ8 + Operator ကiu အသuP&*ပuBiuင#တ1 *ဖစ#ပ?တယ#။ + Operator ကiu String Type
Variable eတ>န8A String Litrals eတ>န8A ပR&တ>8အသuP&*ပuBiuင#ပ?တယ#။ String Value eတ>ကiu ဆက#eပ&တ85 အeနန8A
အသuP&*ပuBiuင#ပ?တယ#။

String Variable hello ရ8S တန#ဖiu&ဟ1 “Hello” *ဖစ#6ပ7& java ရ8S တန#ဖiu&ဟ1 “Java” *ဖစ#တ85 အတ>က# hello + “ “ +
java ဆiuတ1ဟ1 “Hello Java” *ဖစ#သ>1&တ1ကiu eတ>Zရပ?မယ#။

ဆက#လက#6ပ7& String Type Variable န8A Primitive Type Variable eတ>0က1&မ91 + Operator ကiu တ>86ပ7& အသuP&*ပu
တ85အခ?မ(iu&မ91 ဘယ#လiu လuပ#eဆ1င#သ>1&တယ#ဆiuတ1ကiu eလ5လ10ကည#50ကရeအ1င#။

jshell> 10 + "1"
$18 ==> "101"

10 + “1” ရ8S အe*ဖဟ1 “101” *ဖစ#ပ?တယ#။ 10 ဟ1 Primitive type အမ(iu&အစ1&*ဖစ#တ85 int *ဖစ#6ပ7& “1” ဟ1 String
*ဖစ#ပ?တယ#။ Primitive Type တစ#ခuန8A String တစ#ခuကiu + ကiu အသuP&*ပu6ပ7& eရ&သ1&တ85 အခ?မ91 Primitive ရ8Sတန#ဖiu&
ကiu String တစ#ခuအeနန8AယR6ပ7& တ>က#ခ(က#သ>1&ပ?မယ#။ ဒ?5e0က1င#5 “10” + “1” လiuယR6ပ7& အe*ဖဟ1 “101” *ဖစ#သ>1&ပ?
မယ#။

jshell> 10 + 1 + "1"
$19 ==> "111"

အထက#ပ?နမRန1ထ8မ91 10 + 1 + “1” လiu Expression တစ#ခuကiu eရ&ထ1&ပ?တယ#။ 10 န8A 1 တiuဟ1 primitive type
*ဖစစ#6ပ7& “1” ဟ1 String *ဖစ#ပ?တယ#။ အပLမ91ပ?ခ85တ85 နမRန1ထ8မ91ဆiuရင# String တစ#ခuန8Aeပ?င#&ရင# အ1&လuP&ဟ1
String လiue*ပ1င#&6ပ7&eပ?င#&0ကတယ#ဆiuတ1ကiu eတ>Zခ85ရမ91ပ?။ ဒ?eပမ85 Expression တစ#ခuမ91 Arithmetic Operator
eတ> တစ#ခuထက#မကပ?ဝင#0ကတ85 အခ?မ91 Operator Precident လiueခLတ85 ဦ&စ1&eပ&စ]#&စ1&ပuPန8A ဘယ# Operator
ကiu အရင# Eveluate လuပ#မယ#ဆiuတ1ကiu စ7&မ(]#&သတ#မ9တ#ထ1&ပ?တယ#။

Operator eတ>တReနရင#eတ15 ဘယ#ဘက#မ91Diတ85 Operator ကiu အရင# Evaluate လuပ#ပ?တယ#။ ဒ?5e0က1င#5 10 န8A 1
ကiu အရင#eပ?င#&6ပ7& ရလ1တ85 ရလဒ#*ဖစ#တ85 11 ကiu “1” န8A eပ?င#&6ပ7& “111” လiu အe*ဖထ>က#တ1 *ဖစ#ပ?တယ#။

Learning Java 60 မင#&လ>င#


Java Developer Class

jshell> "1" + 10 + 1
$20 ==> "1101"

တကယ#လiu “1” + 10 + 1 လiueရ&ခ85ရင# အရင#ဆuP& “1” + 10 ကiu Evaluate လuပ#ပ?မယ#။ ဒ?5e0က1င#5 အe*ဖဟ1 “101”
*ဖစ#6ပ7& “101” + 1 ကiu Evaluate လuပ#6ပ7& ရလဒ#ကeတ15 “1101” *ဖစ#သ>1&မ91 *ဖစ#ပ?တယ#။

Operator Precedents
Variable eတ>န8A Operators eတ>ကiuတ>86ပ7& eရ&သ1&ထ1&တ1eတ>ကiu Expression လiueခLဆiueလ5Diပ?တယ#။
Expression တစ#ခuမ91 Operator တစ#ခuအထက#မက ပ?ဝင#eလ5Diပ?တယ#။ Arathematic Operator eတ>ကiu
အသuP&*ပuတ85eနရ1မ91 Operator တစ#ခuဆiuပ?က *ပဿန1မDiပ?ဘR&။

jshell> 10 + 1
$21 ==> 11

jshell> 10 - 5
$22 ==> 5

jshell> 10 * 3
$23 ==> 30

jshell> 10 / 3
$24 ==> 3

jshell> 10 % 3
$25 ==> 1

Operator ရ8S ဘယ#ဘက#န8A ည1ဘက#မ91Diတ85 တန#ဖiu&eတ>ကiu Operator အတiuင#& တ>က#ထuတ#6ပ7& အe*ဖကiu*ပ^uPပ?ပ8။


အထက#ပ?အတiuင#& အeပ?င#&၊ အB9တ#၊ အe*မp1က#၊ အစ1&န8A အqကင#&D1တ1ကiu တ>က#ခ(က#6ပ7& eဖL*ပ^uPပ?ပ8။ ဒ?eပမ85
Expression တစ#ခuအတ>င#&မ91 Operator တစ#ခuအထက#မကပ?ဝင#လ16ပ7ဆiuရင#eတ15 Java Programming မ91
eတ15 Operator Precidents အတiuင#& တ>က#ခ(က# လuပ#eဆ1င#သ>1&မ91 *ဖစ#ပ?တယ#။

Multiple Operator Expression eတ>န8A ပတ#သက#6ပ7& တRည7တ85 Operator eတ>Xက7&ဆiuရင#eတ15 ဘယ#ဘက#ကeန


ည1ဘက#ကiu Evaluate လuပ#သ>1&ပ?မယ#။ ဒ?eပမ85 ပ?ဝင#တ85 Operator eတ>မတRရင#eတ15 Operator Precident
အeပLမ91 မRတည#6ပ7& Operator Precident *မင#5မ1&တ85 အရ1eတ>ကiu အရင#ဆuP& Evaluate လuပ#သ>1&ပ?မယ#။

jshell> 5 + 1 * 10
$26 ==> 15

အထက#ပ? နမRန1ကiu 0ကည#5မယ#ဆiuရင# 5 + 1 * 10 လiueရ&ထ1&ပ?တယ#။ ဘယ#ဘက#ကeန ည1ဘက#ကiu Evaluate


လuပ#သ>1&တယ#ဆiuရင# အe*ဖက 5 + 1 ကiu အရင# Evaluate လuပ#6ပ7& ရလ1တ85 ရလဒ#*ဖစ#တ85 6 * 10 ဆ7u6ပ7& အe*ဖက
60 *ဖစ#သ>1&သင#5ပ?တယ#။

Learning Java 61 မင#&လ>င#


Java Developer Class

ဒ?eပမ85 1 * 10 ကiu အရင# Evaluate လuပ#6ပ7& ရလ1တ85 ရလဒ#*ဖစ#တ85 10 န8A 5 ကiueပ?င#&သ>1&ပ?တယ#။ ဒ?5e0က1င#5 အe*ဖ
ဟ1 15 *ဖစ#သ>1&ပ?တယ#။ Java မ91က အeပ?င#&အB-တ# Operator eတ>ထက# အe*မp1က# အစ1& Operator eတ>က
ပiu6ပ7& Precident *မင#5မ1&တ85 အတ>က# Precident *မင#5မ1&တ85 Expression ကeန စ6ပ7& Evaluate လuပ#သ>1&တ1 *ဖစ#
ပ?တယ#။

ဆက#လက#6ပ7& အစ1&န8A အe*မp1က# 6ပ7&eတ15 အeပ?င#&န8A အB-တ#ကeက1 Precedent ဘယ#သRXက7&လ8၊ 6ပ7&eတ15 အ85ဒ7
Operator eတ>ကiu Expression ထ8မ91 eရ1eရ&ရင#ဘယ#လiu *ဖစ#မလ8ဆiuတ1ကiu eလ5လ10ကည#50ကရeအ1င#။

jshell> 5 / 2 * 3
$28 ==> 6

jshell> 3 * 5 / 2
$29 ==> 7

အစ1&န8A အe*မ1က#ဟ1 Operator Precedent အရ အတRတRပ8 *ဖစ#ပ?တယ#။ ဒ?5e0က1င#5 Left to Right Evaluate
လuပ#သ>1&တ1ကiu eတ>Zရမ91 *ဖစ#ပ?တယ#။ အeပLဆuP& အe0က1င#&မ91 5 / 2 * 3 လiueရ&ထ1&ပ?တယ#။ အရင#ဆuP& 5 ကiu 2
န8A စ1&6ပ7& ရလ1တ85 ရလဒ#*ဖစ#တ85 2 ကiu 3 န8A e*မp1က#ပ?တယ#။ ဒ?5e0က1င#5 အe*ဖဟ1 6 *ဖစ#သ>1&ပ?တယ#။

eန1က#တစ#e0က1င#&မ91eတ15 3 * 5 / 2 လiueရ&ထ1&ပ?တယ#။ 3 * 5 ရ8S ရလဒ#ဟ1 15 *ဖစ#6ပ7& 15 / 2 ကiu Evaluate


ဆက#လuပ#ပ?တယ#။ ဒ?5e0က1င#5 အe*ဖဟ1 7 *ဖစ#သ>1&ပ?တယ#။ အeပ?င#&အB-တ#ဟ1လ8 Operator Precedent တRတ85
အတ>က# ဘယ#ဘက#ကeန ည1ဘက#ကiu Evaluate လuပ#သ>1&0ကမ91 *ဖစ#ပ?တယ#။

Assignment Operator န9< တ#9BပC1 အသu516ပu6ခင)1


Arithmetic Operator eတ>ကiu Assignment Operator eတ>န8Aလ8 တ>8ဖက#6ပ7& eရ&သ1& အသuP&*ပuBiuင#ပ?တယ#။
Variable B9စ#ခuအ0က1&မ91 += တiuလiu -= တiuလiu Operator eတ>ကiu ပR&တ>86ပ7& eရ&သ1&Biuင#ပ?တယ#။ ဒ7လiueရ&သ1&တ85
eနရ1မ91 ဘယ#ဘက#မ91 အစ1&ထiu&ခ(င#တ85 Variable ကiueရ&6ပ7& Operator eတ>ကiu eရ&6ပ7&မ9 Oponent ကiu eရ&သ1&ရ
မ91 *ဖစ#ပ?တယ#။

jshell> a += 5
$31 ==> 15

jshell> a -= 1
$32 ==> 14

jshell> a *= 2
$33 ==> 28

jshell> a /= 1
$34 ==> 28

jshell> a %= 3
$35 ==> 1

Learning Java 62 မင#&လ>င#


Java Developer Class
အထက#ပ?နမRန1မ91လiu a += 5 လiu eရ&ထ1&ရင# a ရ8S တန#ဖiu&န8A 5 ကiu eပ?င#&6ပ7&မ9 ရတ85 တန#ဖiu&န8A a ကiu အစ1&ထiu&မ91
*ဖစ#ပ?တယ#။ တစ#*ခ1& Arathmetic Operator eတ>ကiuလ8 အလ1&တReရ&သ1&Biuင#မ91 *ဖစ#ပ?တယ#။

jshell> String str = "Hello "


str ==> "Hello "

jshell> str += "Java"


$37 ==> "Hello Java"

String Type Variable eတ>မ91လ8eပ?င#&6ပ7& အစ1&ထiu&တ85 Operator မ(iu&ကiu အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။ str ရ8S
တန#ဖiu&ဟ1 “Hello “ *ဖစ#တ85 အတ>က# “Java” န8A eပ?င#&6ပ7& အစ1&ထiu&လiuက#တ85 အခ? str ရ8S တန#ဖiu&ဟ1 “Hello Java”
*ဖစ#သ>1&မ91 *ဖစ#ပ?တယ#။

Relational Operator
Variable B9စ#ခuရ8S တန#ဖiu&eတ>ကiu Bi-င#&ယ9]#တ85eနရ1မ(iu&မ91 Relational Operator eတ>ကiu အသuP&*ပuBiuင#ပ?တယ#။
Bi-င#&ယ9]#တယ#လiu e*ပ1တ85eနရ1မ91 Variable B9စ#ခuဟ1 တRသလ1& မတRဘR&လ1&၊ 6ပ7&eတ15 ဘယ#သRက Xက7&သလ8
ငယ#သလ8 အစDiသ*ဖင#5 Bi-င#&ယ9]#တ1ကiu ဆiuလiuတ1 *ဖစ#ပ?တယ#။

အရင#ဆuP& တRမတR Bi-င#&ယ9]#တ85 အပiuင#&ကeနစ6ပ7& eလ5လ1သ>1&0ကရeအ1င#။ ပuPမ9န#အ1&*ဖင#5 a = b လiueရ&ရင# a န8A b


ဟ1တRတယ#လiuယRဆ0ကမ91 *ဖစ#eပမ85 Java မ91eတ15 a = b ဆiuတ1က a eနရ1ကiu b န8A အစ1&ထiu&တ1လiu ဆiuလiuပ?
တယ#။ a န8A b န8A တRတယ#ဆiuတ1ကiu a == b န8A eဖL*ပeလ5Diပ?တယ#။ Relational Operator eတ>ကiu Variable B9စ#ခu
အ0က1&မ91 eရ&သ1&ရမ91 *ဖစ#6ပ7& Expression ကiu Evaluate လuပ#လiuက#တ85 အခ?မ(iu&မ91 boolean တန#ဖiu&တစ#ခuကiu
ရလဒ#အeနန8A ရDiမ91 *ဖစ#ပ?တယ#။ a == b လiueရ&ထ1&ရင# a ဟ1 b န8A တRရင# true ကiu ရDiမ91 *ဖစ#6ပ7& မတRရင#eတ15
false ကiu ရDiမ91 *ဖစ#ပ?တယ#။

jshell> int a = 4, b = 5
a ==> 4
b ==> 5

jshell> a == b
$41 ==> false

jshell> a != b
$42 ==> true

အထက#ပ? နမRန1ထ8မ91 a ကiu 4 န8A 6ပ7&eတ15 b ကiu 5 န8A အစ1&ထiu&ထ1&ပ?တယ#။ အ85ဒ?e0က1င#5 a == b လiu eရ&သ1&
ထ1&တ85 Expression မ91 a န8A b ဟ1 မတRတ85 အတ>က# false လiu အe*ဖထ>က#တ1 *ဖစ#ပ?တယ#။ တဖန# a != b လiu
eရ&ထ1&တ1ဟ1 a န8A b ဟ1 မတRပ?ဘR&လiu eရ&သ1&ထ1&တ1*ဖစ#6ပ7& တကယ#လ8 မတRတ85 အတ>က# true လiu အe*ဖရ
တ1 *ဖစ#ပ?တယ#။

တRလ1&မတRဘR&လ1&ဆiuတ1ကiu Bi-င#&ယ9]#တ85 Operator ကiu String Type Variable eတ>မ91 အသuP&*ပuBiuင#ပ?တယ#။


String မ91အသuP&*ပuတ85 == ရ8S ထR&*ခ1&ပuPကiu eလ5လ1သ>1&0ကရeအ1င#။

Learning Java 63 မင#&လ>င#


Java Developer Class

Equality in Strings
Java မ91 အသuP&*ပueနတ85 String ဟ1 အမတန#ထR&ဆန#&တ85 Type အမ(iu&အစ1&*ဖစ#ပ?တယ#။ String ဟ1 Java
Standard API ထ8မ91 ပ?ဝင#တ85 Class တစ#မ(iu&*ဖစ#6ပ7& Data Type အeနန8A အသuP&*ပuBiuင#ပ?တယ#။ String Data
Type eတ>ဟ1 Character Array eတ>ကiu ကiuယ#စ1&*ပuBiuင#တ85 Object တစ#မ(iu&*ဖစ#ပ?တယ#။

Object *ဖစ#တ85 အတ>က# String ဟ1 Reference Data Type *ဖစ#ပ?တယ#။ ဒ?eပမ85 ထR&ထR&*ခ1&*ခ1& String တစ#မ(iu&
ထ8 Source Code eတ>ထ8မ91 Literals eတ>ကiu အသuP&*ပu6ပ7& တန#ဖiu&ကiu တiuက#^iuက# အစ1&ထiu&eရ&သ1&Biuင#တ1 *ဖစ#ပ?
တယ#။ String str = “Hello Java” လiueရ&ထ1&ခ85ရင# “Hello Java” ဆiuတ1က String ရ8S တန#ဖiu&ကiu Literal အeနန8A
eရ&သ1&ထ1&တ1 *ဖစ#ပ?တယ#။

တဖန# Constructor ကiu အသuP&*ပu6ပ7& String str2 = new String(“Hello Java”) လiu eရ&သ1&Biuင#ပ?တယ#။ ဒ?eပမ85
str န8A str2 တiuရ8S သဘ1ဝeတ>ဟ1 မတRည70ကပ?ဘR&။

jshell> String str = "Hello Java"


str ==> "Hello Java"

jshell> String str1 = "Hello Java"


str1 ==> "Hello Java"

jshell> str == str1


$45 ==> true

အထက#ပ?အတiuင#& str န8A str1 တiuကiu Literal တန#ဖiu&*ဖစ#တ85 “Hello Java” န8A အစ1&ထiu&eရ&သ1&ခ85ပ?တယ#။ 6ပ7&eတ15
str == str1 လiu eရ&လiuက#တ85အခ?မ91 true လiu အe*ဖရပ?တယ#။

jshell> String str2 = new String("Hello Java")


str2 ==> "Hello Java"

jshell> str == str2


$47 ==> false

အထက#ပ? ကuဒ#ထ8မ91eတ15 String str2 = new String(“Hello Java”) ဆiu6ပ7&eရ&သ1&ထ1&*ပန#တယ#။ 6ပ7&eတ15 str
== str2 လiu eရ&သ1&ထ1&တ85 အခ?မ91 မတRဘR&ဆiu6ပ7& false လiu အe*ဖထ>က#ပ?တယ#။ ဒ7eနရ1မ91 သiသ1တ1က
Literal န8A eရ&သ1&ထ1&တ85 Variable ရ8S တန#ဖiu&န8A Constructor ကiu အသuP&*ပu6ပ7& တည#eဆ1က#ထ1&တ85 တန#ဖiu&ခ(င#&
မတRဘR&လiu e*ပ1eနတ1လiu သiထ1&သင#5ပ?တယ#။

String Type Variable eတ>ကiu Literal eတ>န8Aလ8 Assign လuပ#Biuင#သလiu၊ Constructor မ9တဆင#5လ8 Object
eဆ1က#6ပ7& Assign လuပ#Biuင#ပ?တယ#။ Literal န8A eရ&သ1&ထ1&တ85 String eတ>ကiu Heap Memory eပLမ91Diတ85
String Pools ဆiuတ85 eနရ1မ91 သiမ#&ထ1&eလ5Diပ?တယ#။ တကယ#လiu Source Code ထ8မ91 “Hello Java” ဆiu6ပ7&
Literal န8A eရ&သ1&ထ1&တ1ကiu eတ>Zရင# အရင#ဆuP& String Pools ထ8မ91Diလ1&လiu0ကည#5ပ?တယ#။ Diတယ#ဆiuရင# အ85ဒ7
Reference ကiu အသuP&*ပuပ?တယ#။ တကယ#လiu မDiဘR&ဆiuရင#eတ15 String Pools ထ8မ91 String Object ကiu eဆ1က#
6ပ7&မ9 အ85ဒ7 Reference ကiu အသuP&*ပuမ91 *ဖစ#ပ?တယ#။

Learning Java 64 မင#&လ>င#


Java Developer Class
ဤနည#&အ1&*ဖင#5 တRည7တ85 String Object eတ> အမ(1&Xက7& Diမeနeအ1င# Memory ကiu Optimize လuပ#ခ850ကတ1
*ဖစ#ပ?တယ#။ ဒ?5e0က1င#5 str == str1 ဟ1 true လiu အe*ဖထ>က#တ1 *ဖစ#ပ?တယ#။

ဒ?eပမ85 String str2 = new String(“Hello Java”) လiu eရ&လiuက#တ1ဟ1 new Operator ကiu အသuP&*ပuထ1&တ85
အတ>က# String Object အသစ#တစ#ခuကiu Heap Memory eပLမ91 တည#eဆ1က#သ>1&မ91 *ဖစ#ပ?တယ#။ String Pool
ထ8က Reference ကiu အသuP&*ပuမ91 မဟuတ#ပ?ဘR&။ ဒ?5e0က1င#5 str == str2 လiueရ&လiuက#တ85အခ?မ91 တန#ဖiu&eတ>က တR
ရ8Sသ1&န8A false ဆiu6ပ7& အe*ဖထ>က#တ1 *ဖစ#ပ?တယ#။

ဒ?5eပမ85 String eတ>မ91eတ15 Memory Address တRတ1 မတRတ1ထက# သRတiuရ8S တန#ဖiu&တRရင# တRတယ#လiu အe*ဖ
ထ>က#ခ(င#0ကမ91 *ဖစ#ပ?တယ#။ ဒ?5e0က1င#5 String eတ>ကiu တRတယ#မတRဘR&ဆiuတ1ကiu စစ#eဆ&ခ(င#ရင# equals method
ကiu အသuP&*ပu6ပ7& စစ#eဆ&သင#5ပ?တယ#။

jshell> String str2 = new String("Hello Java")


str2 ==> "Hello Java"

jshell> str == str2


$47 ==> false

jshell> [Link](str2)
$48 ==> true

အ6ခ-1eသ- Relational Operators


Primitive တန#ဖiu&B9စ#ခu ဒ?မ9မဟuတ# Primitive Variable B9စ#ခuဟ1 မတRပ?ဘR&ဆiuတ1ကiu စစ#eဆ&ဖiu != Operator ကiu
အသuP&*ပuBiuင#ပ?တယ#။

jshell> 10 != 11
$49 ==> true

jshell> 10 != 10
$50 ==> false

10 != 11 လiueရ&ထ1&ရင# မတRတ1မ9န#တ85 အတ>က# အe*ဖဟ1 true *ဖစ#မ91 *ဖစ#6ပ7& 10 != 10 လiueရ&ထ1&ရင# မတRဘR&


ဆiuတ1 မ91&eနတ85 အတ>က# အe*ဖဟ1 false *ဖစ#ပ?တယ#။

jshell> 10 > 10
$52 ==> false

jshell> 10 >= 10
$53 ==> true

Learning Java 65 မင#&လ>င#


Java Developer Class
အထက#ပ?နမRန1မ91eတ15 10 > 10 လiueရ&ထ1&တ1ဟ1 10 ဟ1 10 ထက#Xက7&ပ?တယ#လiu စစ#eဆ&eနတ1ပ8 *ဖစ#6ပ7&
10 ဟ1 10 ထက#မXက7&တ85 အတ>က# အe*ဖဟ1 false ထ>က#ပ?တယ#။ eန1က#တစ#e0က1င#&မ91eတ15 10 >= 10 လiu
Expression ကiu eရ&သ1&ထ1&တ85 eနရ1မ91 10 ဟ1 10 ထက#Xက7&တယ#ဒ?မ9မဟuတ# တRတယ#လiu eဖL*ပထ1&တ1 *ဖစ#
ပ?တယ#။ ဒ7eနရ1မ91eတ15 10 န8A 10 ဟ1တRည7တ85 အတ>က# အe*ဖဟ1 true *ဖစ#သ>1&တ1 *ဖစ#ပ?တယ#။

အလ1&တR Less Than န8A Less Than or Equal To (<, <=) Operator eတ>ကiuလ8 အသuP&*ပuBiuင#မ91*ဖစ#ပ?တယ#။ ဒ7
လiu Relation Operator eတ>န8A Variable eတ>ကiu တ>8ဖက#eရ&သ1&ထ1&တ1eတ>ကiu Relational Expression လiu
eခLဆiueလ5Diပ?တယ#။ eသ*ခ1တ1တစ#ခuကeတ15 Relational Expression eတ>ရ8S ရလဒ#eတ>ဟ1 Boolean တန#ဖiu&
*ဖစ#တ85 true သiuမဟuတ# false ပ8 *ဖစ#eနမ91ပ8 *ဖစ#ပ?တယ#။

Logical Operators
Boolean တန#ဖiu&eတ>၊ Boolean Expression eတ>န8A တ>8ဖက#6ပ7& Logic eတ>ကiu eရ&သ1&လiuတ85 အခ?မ(iu&မ91
အသuP&*ပuBiuင#တ85 Operator eတ> *ဖစ#0ကပ?တယ#။ အသuP&*ပuBiuင#တ85 Operator eတ>ကeတ15 AND (&), OR (|), Short
Cut AND (&&), Short Cut OR (||), NOT (!) န8A XOR (^) တiuပ8 *ဖစ#0ကပ?တယ#။

Not (!) Operator


Boolean တန#ဖiu&eတ>၊ Boolean Type Variable eတ>န8A Boolean Expression eတ>ရ8S eUမ91 eရ&သ1& အသuP&*ပu
Biuင#ပ?တယ#။ လက#Di Boolean ရ8S တန#ဖiu&ကiu e*ပ1င#&*ပန#*ပuလuပ#eပ&Biuင#ပ?တယ#။ Not True ဆiuရင# False *ဖစ#သ>1&မ91
*ဖစ#6ပ7& Not False ဆiuရင#eတ15 True *ဖစ#သ>1&မ91ပ?။

jshell> !true
$64 ==> false

jshell> !false
$65 ==> true

AND (&) Operator


And Operator eတ>ကiu Boolean တန#ဖiu&B9စ#ခu ဒ?မ9မဟuတ# Boolean Expression B9စ#ခuအ0က1&မ91 အသuP&*ပuBiuင#6ပ7&
တန#ဖiu&B9စ#ခuလuP&မ9န#တယ#ဆiuပ?မ9 true ကiu *ပန#eပ&ပ?မယ#။ ဒ?မ9မဟuတ#ရင#eတ15 false တန#ဖiu&ကiu ရDiမ91 *ဖစ#ပ?တယ#။

jshell> true & true


$58 ==> true

jshell> true & false


$59 ==> false

jshell> false & true


$60 ==> false

jshell> false & false


$61 ==> false

Learning Java 66 မင#&လ>င#


Java Developer Class

OR (|) Operator
And Operator eတ>ကiu Boolean တန#ဖiu&B9စ#ခu ဒ?မ9မဟuတ# Boolean Expression B9စ#ခuအ0က1&မ91 တစ#ခuခuမ9န#eန
ရင# True ကiu *ပန#eပ&မ91 *ဖစ#6ပ7& B9စ#ခuစလuP&မ91&မ9သ1 False ကiu *ပန#eပ&မ91 *ဖစ#ပ?တယ#။

jshell> true | true


$66 ==> true

jshell> true | false


$67 ==> true

jshell> false | true


$68 ==> true

jshell> false | false


$69 ==> false

Short Cut Logical Operators


AND န8A OR Operator eတ>ကiu 0ကည#5မယ#ဆiuရင# Boolean Expression B9စ#ခuအ0က1&မ91 eရ&6ပ7& အသuP&*ပuရပ?
တယ#။ AND ကiu 0ကည#5မယ#ဆiuရင#လ8 Expression B9စ#ခuလuP& မ9န#တယ#ဆiuမ9သ1 true ကiu ရDiမ91 *ဖစ#6ပ7& OR ဆiuရင#လ8
B9စ#ခuလuP&မ91&မ9သ1 false ကiu ရDiမ91 *ဖစ#ပ?တယ#။ Logical Operator eတ>မ91 ဘယ#ဘက#က Expression ကiu အရင#
လuပ#6ပ7&မ9 ည1ဘက#က Expression ကiu Evaluate လuပ#eနပ?တယ#။ ဒ?5e0က1င#5 AND Operator ရ8S ဘယ#ဘက#က
Expression ရ8S တန#ဖiu&ဟ1 false *ဖစ#ခ856ပ7ဆiuရင# ဘယ#လiuအe0က1င#&e0က1င#5မ9 true *ဖစ#မလ1Biuင#eတ15ပ?ဘR&။ ဒ7လiuပ8
OR Operator ရ8S ဘယ#ဘက#က Experssion ရ8S တန#ဖiu&က true *ဖစ#ခ856ပ7ဆiuရင#လ8 ဘယ#လiu အe0က1င#&e0က1င#5မ9
false *ဖစ#မလ1Biuင#eတ15ပ?ဘR&။

ဒ7လiuအeနအထ1&မ(iu&မ91 သ1မန# AND (&) န8A OR (|) Operator eတ>ကiu အသuP&*ပuခ85ရင# ဘယ#ဘက#က Expression
ကiu Evaluate လuပ#6ပ7& true *ဖစ#စရ1 အe0က1င#&မDiခ85ရင#eတ1င#မ9၊ ဒ?မ9မဟuတ# false *ဖစ#စရ1အe0က1င#&မDiခ85
ရင#eတ1င#မ9၊ ည1ဘက#က Expression ကiu ဆက#6ပ7& Evaluate လuပ#eနမ91 *ဖစ#ပ?တယ#။ အ85ဒ?eတ>ကiu စမ#&သပ#ဖiu
Boolean တန#ဖiu&eတ>ကiu *ပန#eပ&Biuင#တ85 Method eလ&eတ>ကiu *ပင#ထ1&0ကည#50ကရeအ1င#။

jshell> boolean getTrue() {


...> [Link]("Call True");
...> return true;
...> }
| created method getTrue()

jshell> boolean getFalse() {


...> [Link]("Call False");
...> return false;
...> }
| created method getFalse()

Learning Java 67 မင#&လ>င#


Java Developer Class
အထက#ပ? နမRန1မ91ပ?တ85 getTrue() method ကeတ15 true ကiu Return *ပန#eပ&Biuင#တ85 Method *ဖစ#6ပ7&
getFalse() method ကeတ15 false ကiu Return *ပန#eပ&Biuင#တ85 method *ဖစ#ပ?တယ#။ Method eတ>ထ8မ91eတ15
ဘယ# Method ကiu Evluate လuပ#သ>1&တယ#ဆiuတ1ကiu သieအ1င# [Link] eတ>ကiu eရ&သ1&ထ1&ပ?တယ#။

jshell> getFalse() & getTrue()


Call False
Call True
$3 ==> false

အထက#ပ? နမRန1ထ8မ91ဆiuရင# getFalse() ကiu Evaluate လuပ#6ပ7&တ85အခ?မ91 false ကiu ရမ91 *ဖစ#တ85 အတ>က# AND
Expression ရ8S ရလဒ#ဟ1 ဘယ#eတ15မ9 true *ဖစ#လ1စရ1 အe0က1င#&မDieတ15ပ?ဘR&။ ဒ?eပမ85 ည1ဘက#က
getTrue() Expression ကiu Evaluate လuပ#သ>1&တ85 အတ>က# Call False လiueဖL*ပ6ပ7&တ85 အခ?မ91 Call True ကiu
eဖL*ပသ>1&တ1 *ဖစ#ပ?တယ#။

jshell> getFalse() && getTrue()


Call False
$5 ==> false
Operator Type Operators Description

Assignment =, +=, -=, *=, /= Variable မ(1&အ1& အစ1&ထiu&ရ1တ>င# အသuP&*ပu

Primitive Variable မ(1&အ1& အeပ?င#၊ အBuတ#၊


Arithmetic +, -, *, /, %, ++, - - အe*မp1က#၊ အစ1&၊ အqကင#&D1 ရ1တ>င# အသuP&*ပu

Relational <, <=, >, >=, !=, == Primitive Variable မ(1&အ1& Bi-င#&ယ9]#ရ1တ>င# အသuP&*ပu

Primitive Variables မ(1& NOT, AND, OR, XOR


Logical !, &, &&, |, ||, ^ Logic မ(1&အ1& အသuP&*ပuBiuင#

အထက#ပ?နမRန1ကeတ15 AND (&) အစ1& Short Cut AND (&&) ကiu အသuP&*ပuသ>1&တ1ပ?။ ဘယ#ဘက#က
Expression *ဖစ#တ85 getFalse() ကiu Evaluate လuပ#6ပ7&တ85အခ?မ91 ရလဒ#ဟ1 false *ဖစ#eန6ပ7ဆiuရင# ဘယ#နည#&န8Aမ9
true *ဖစ#စရ1မDieတ15တ85 အတ>က# ည1ဘက#က Expression *ဖစ#တ85 getTrue() ကiu Evaluate မလuပ#eတ15ပ8 တန#&
6ပ7& အe*ဖကiu false လiuထuတ#eပ&ခ85ပ?တယ#။

jshell> getTrue() || getFalse()


Call True
$6 ==> true

အလ1&တRပ8 Short Cut OR (||) Operator ဟ1လ8 ဘယ#ဘက#က Expression ကiu Evaluate လuပ#6ပ7& အe*ဖဟ1
True *ဖစ#ခ85ရင# ဘယ#နည#&န8Aမ9 False *ဖစ#စရ1အe0က1င#&မDieတ15တ85 အတ>က# ည1ဘက#က Expression *ဖစ#တ85
getTrue() method ကiu Invoke မလuပ#eတ15ပ8 အe*ဖကiu တန#&6ပ7& true လiu ထuတ#eပ&ခ85တ1*ဖစ#ပ?တယ#။

Short Cut eတ>ဟ1 ဘယ#ဘက#က Expression ရ8S တန#ဖiu&ကiu 0ကည#56ပ7& အe*ဖကiuမရeသ&ဘR&ဆiuရင#eတ15 ည1ဘက#
က Expression ကiu ဆက#6ပ7& Evaluate လuပ#သ>1&မ91 *ဖစ#ပ?တယ#။
Learning Java 68 မင#&လ>င#
Java Developer Class

jshell> getFalse() || getTrue()


Call False
Call True
$7 ==> true

အထက#ပ?နမRန1မ91ဆiuရင# getFalse() ကiueခLတ85အခ?မ91 false *ဖစ#eနတ85 အတ>က# eန1က#တစ#ခuက false *ဖစ#ရင#


false *ဖစ#6ပ7& true *ဖစ#ရင# true *ဖစ#eနBiuင#ပ?eသ&တယ#။ ဒ?5e0က1င#5 ည1ဘက#က Expression *ဖစ#တ85 getTrue()
Method ကiu ဆက#6ပ7& Evaluate လuပ#သ>1&6ပ7&မ91 အe*ဖကiu *ပန#6ပ7&eဖL*ပeပ&သ>1&ပ?တယ#။
AND ပ8 သuP&သuP& Short Cut AND ပ8သuP&သuP&၊ OR ပ8 သuP&သuP& Short Cut OR ပ8 သuP&သuP& eန1က#ဆuP&ရလဒ#ကeတ15
အတRတRပ8 *ဖစ#ပ?မယ#။ ဒ?eပမ85 Short Cut eတ>က ဘယ#ဘက#က တန#ဖiu&ကiu0ကည#56ပ7& အe*ဖကiu ရသ>1&ရင# ည1ဘက#
က Expression eတ>ကiu Evaluate လuပ#စရ1မလiuတ85 အတ>က# Performance မ91ပiu6ပ7& eက1င#&မ>န#eစBiuင#ပ?တယ#။

XOR (^) Operator


Exclusive OR ရ8S အတiueက1က# အeခLအeဝL*ဖစ#6ပ7& တစ#ဘက#ဘက#မ91 true ပ?မ9 true *ဖစ#မ91 *ဖစ#6ပ7& B9စ#ဘက#
လuP& true Xက7&ပ8 ဒ?မ9မဟuတ# false Xက7&ပ8 *ဖစ#eနရင#eတ15 false ကiuပ8ရDiမ91 *ဖစ#ပ?တယ#။

jshell> true ^ true


$14 ==> false

jshell> false ^ true


$15 ==> true

jshell> true ^ false


$16 ==> true

jshell> false ^ false


$17 ==> false

Notes
Logical Operator eတ>ကiu Boolean Expression eတ>0က1&မ91 အသuP&*ပuတ1eတ>ကiuပ8 eဖL*ပခ85ပ?တယ#။ ဒ?eပမ85
Logical Operator eတ>ကiu အ*ခ1&eသ1 Primitive Type eတ>0က1&မ91လ8 အသuP&*ပuBiuင#ပ?တယ#။ အ85ဒ7လiu အသuP&*ပuတ85
အခ?မ91 Binary Data အ*ဖစ#e*ပ1င#&6ပ7&တ>က#ခ(က#သ>1&မ91 *ဖစ#ပ?တယ#။ ဒ?eပမ85 Business Application ရ8S Logic
eတ>ကiu eရ&သ1&0ကတ85အခ?မ(iu&မ91 Boolean Expression eတ>ကiu အသuP&*ပuတ1ကမ(1&တ85အတ>က# ဒ7စ1အuပ#ထ8မ91
eတ15 Boolean Expression န8A တ>8ဖက#အသuP&*ပuတ85 နမRန1မ(1&ကiu eဖL*ပသ>1&တ1 *ဖစ#ပ?တယ#။

Learning Java 69 မင#&လ>င#


Java Developer Class

Statements
Java Compilar ကeန Compile လuပ#Biuင#6ပ7& JVM ကeန လuပ#eဆ1င#မ-eတ>ကiu လuပ#eဆ1င#eပ&Biuင#တ85 အဓiပr?ယ#*ပ
ည#5စuPတ85 စ1e0က1င#&eတ>ကiu Statement လiueခLပ?တယ#။ eအ1က#က နမRန1အတiuင#& Class တစ#ခuကiu eရ&သ1&ထ1&ပ?
မယ#။

public class Hello {


String str = "Hello"
}

အထက#ပ?အတiuင#&eရ&ထ1&တ85 Class ကiu Compile လuပ#လiuက#တ85အခ?မ91 Error တက#ပ?လiမ#5မယ#။ String Type


Variable *ဖစ#တ85 str ကiu Assignment Operator *ဖစ#တ85 = ကiu အသuP&*ပu6ပ7& “Hello” န8A အစ1&ထiu&eနတ1မ9န#ပ?
တယ#။ ဒ?eပမ85 အ85ဒ7 စ1e0က1င#&ဟ1 6ပ7&*ပည#5စuPတ85 ဝ?က(တစ#e0က1င#& မဟuတ#eသ&ပ?ဘR&။ ဒ?5e0က1င#5 Compile Time
မ91 Error ကiu *ဖစ#eပLeစမ91 *ဖစ#ပ?တယ#။ က8 ဒ7အခန#&မ91eတ15 6ပ7&*ပည#5စuPတ85 ဝ?က( ဒ?မ9မဟuတ# Statement eတ>ကiu
ဘယ#လiu eရ&ရမယ#၊ 6ပ7&eတ15 Statement အမ(iu&အစ1&eတ> ဘယ#လiuDiတယ#ဆiuတ1ကiu eလ5လ1သ>1&0ကရeအ1င#။

Statement eတ>ကiu အမ(iu&အစ1&ခ>8*ခ1&မယ#ဆiuရင# eအ1က#ပ?အတiuင#&ခ>8*ခ1&Biuင#မ91 *ဖစ#ပ?တယ#။

✓ Declaration Statements
Variable eတ>ကiu အသuP&*ပuမယ#ဆiu6ပ7& Declare လuပ#တ85အခ?မ91 eရ&သ1&Biuင#တ85 ဝ?က(အမ(iu&အစ1&မ(iu&*ဖစ#ပ?တယ#

✓ Expression Statements
Variable eတ>န8A Operator eတ>ကiu တ>8ဖက#eရ&သ1&ထ1&တ1ကiu Expression လiueခLတယ#ဆiuတ1 eUခန#&မ91
eလ5လ1ခ856ပ7&ပ?6ပ7။ အခ(iueသ1 Expression eတ>ကiu eန1က#မ91 ; ကiueရ&လiuက#^uPန8A Statement အeနန8A အသuP&*ပu
Biuင#ပ?တယ#။

✓ Control Flows Statements


Code Block eတ>န8A Method eတ>ထ8မ91 eရ&သ1&Biuင#တ85 Statement eတ>*ဖစ#0က6ပ7&၊ Program ရ8S Flow eတ>ကiu
Control လuပ#eပ&Biuင#တ85 Statement eတ>*ဖစ#0ကပ?တယ#။ အဓiကအ1&*ဖင#5 Decision Statements eတ>န8A
Looping Statement eတ>ဆiu6ပ7& Di0ကပ?တယ#။ ဒ7လiuအeနအထ1&မ(iu&ဆiuရင# ဒ7လiuလuပ#မယ#ဒ?မ9မဟuတ#ရင#ဒ7လiuလuပ#
မယ#ဆiuတ85 eရ&သ1&ပuPeတ>ကiu Decision Statement eတ>န8A eရ&သ1&eဖL*ပBiuင#6ပ7&။ အe*ခအeနအeပLမRတည#6ပ7&
အXကiမ#Xကiမ# လuပ#eဆ1င#မ-eတ>ကiu *ပuလuပ#eစလiuတ85 အခ?မ(iu&eတ>မ91 Looping Statement eတ>ကiu အသuP&*ပuBiuင#ပ?
တယ#။

Learning Java 70 မင#&လ>င#


Java Developer Class

Declaration Statements
Java Program eတ>ထ8မ91 အသuP&*ပuမည#5 Variable eတ>ကiu Declare လuပ#တ85အခ?မ91 အသuP&*ပuBiuင#တ85 Statement
eတ> *ဖစ#ပ?တယ#။ Variable eတ>ကiu Declare လuပ#တ85အခ?မ91 Type န8A Name တiuကiu eရ&သ1&ရန#လiuအပ#6ပ7&
အeန1က#မ91 ; ကiu eရ&သ1&လiuက#ရင#ရပ?6ပ7။ အe*ခခPအ1&*ဖင#5 Variable eတ>ကiu Declare လuပ#တ85 eနရ1မ91 ပuPစPB9စ#
မ(iu&DiBiuင#ပ?တယ#။ အ85ဒ?eတ>ကeတ15 Variable eတ>ကiu ^iu&^iu& Declare လuပ#တ85ပuPစPန8A Initial တန#ဖiu&eတ>ကiu Assign
လuပ#6ပ7& Declare လuပ#တ85 ပuPစPဆiu6ပ7& B9စ#မ(iu&DiBiuင#ပ?တယ#။

public class Hello {

String str1;

public Hello() {
str1 = "Hello Java";
}
}

အထက#ပ?ပuPစPကeတ15 သ1မန# ^iu&^iu& Declare လuပ#တ85 ပuPစPပ?။ String str1; ဆiuတ1က str1 Variable ကiu Declare
လuပ#eနတ85 Declaration Statement *ဖစ#ပ?တယ#။ 6ပ7&eတ15မ9 Constructor ထ8မ91 str1 = “Hello Java”; ဆiu6ပ7&
Assignment လuပ#eနပ?တယ#။

public class Hello {

public Hello() {
String str1;
}
}

အထက#ပ?နမRန1ကeတ15 String str1; ကiu Constructor ထ8မ91 eရ&သ1&ထ1&တ85 အတ>က# Local Variable တစ#ခu
ကiu Declare လuပ#eနတ1ပ8 *ဖစ#ပ?တယ#။ Local Variable eတ>ကiu အသuP&*ပuတ85 eနရ1မ91 တစ#ခu သတiထ1&သင#5တ1
ကeတ15 Initialize မလuပ#ပ8 အသuP&*ပuလiu မရတ85 အခ(က#*ဖစ#ပ?တယ#။ Local Variable eတ>ဟ1 Default တန#ဖiu&ပ?မ
လ1တ85 အတ>က# တန#ဖiu&ကiu Assign မလuပ#ခင# အသuP&*ပuလiu မရပ?ဘR&။

public class Hello {

public Hello() {
String str1 = “Hello Java”;
}
}

အထက#ပ?နမRန1ကeတ15 တန#ဖiu&ကiu အစ1&ထiu&6ပ7& Declare လuပ#တ85 ပuPစP*ဖစ#ပ?တယ#။

Learning Java 71 မင#&လ>င#


Java Developer Class

Expression Statements
Expression eတ>ရ8S eန1က#မ91 ; eရ&လiuက#တ1န8A Statement *ဖစ#သ>1&တ85 Statement မ(iu&ကiu Expression
Statement လiueခL0ကပ?တယ#။ မ(1&eသ1အ1&*ဖင#5 Assignment Expression eတ>၊ Increment, Decrement
Expressioneတ>၊ Method Invocation Expression eတ>န8A Object Creation Expression eတ>ကiu Statement
အeနန8A အသuP&*ပuBiuင#ပ?တယ#။

public class Hello {

String str1;

public Hello() {
str1 = "Hello Java";
}
}

အထက#ပ?နမRန1မ91 str1 = “Hello Java” လiueရ&သ1&ထ1&တ1ဟ1 str variable eနရ1ကiu “Hello Java” န8A
အစ1&ထiu&eနတ85 Assignment Expression *ဖစ#6ပ7& eန1က#ကeန ; ကiueရ&သ1&လiuက#တ1န8A Assign Statement
တစ#e0က1င#& *ဖစ#လ1ပ?တယ#။

public class Hello {

int count;

public void countUp() {


count ++;
}

public static void main(String [] args) {


Hello h;
h = new Hello();
[Link]();
}
}

အထက#ပ?နမRန1ထ8မ91 countUp() method ထ8မ91 count ++; လiueရ&သ1&ထ1&တ1ဟ1 count ကiu တစ#တiu&eပ&တ85
increment statement *ဖစ#ပ?တယ#။ 6ပ7&eတ15 main method ထ8မ91 eရ&သ1&ထ1&တ85 h = new Hello(); ဆiuတ1
က Object Creation Statement *ဖစ#6ပ7& [Link](); ဆiuတ1ကeတ15 Method Invocation Statement *ဖစ#ပ?
တယ#။

eဖL*ပပ?အတiuင#& Expression eတ>ရ8S အeန1က#မ91 ; ကiu eရ&လiuက#^uPန8A Statement *ဖစ#သ>1&တ85 Statement eတ>
ဟ1 Expression Statement eတ> *ဖစ#0ကပ?တယ#။

Learning Java 72 မင#&လ>င#


Java Developer Class

Control Flow Statements


Program တစ#ခuရ8S အသ>1&အလ1eတ>ကiu Flow လiueခLပ?တယ#။ အ85ဒ7 Flow eတ>ကiu Control လuပ#eပ&Biuင#တ85
Statement eတ>ကiu Control Flow Statement eတ>လiueခLပ?တယ#။ Java Program ဆiuတ1ဘ1လ8 ဆiuတ1ကiu *ပန#
6ပ7& eတ>&0ကည#50ကရeအ1င#။ Java Programming ဘ1သ1ရပ#န8A eရ&သ1&ထ1&တ85 အစ7အစ]# အစuအeဝ&တစ#ခuသ1
*ဖစ#ပ?တယ#။

6ပ7&ခ85တ85 အခန#&eတ>တuန#&က Variable eတ>၊ Java Data Type eတ>၊ Method eတ>၊ Class eတ> န8A Operator eတ>
အe0က1င#&ကiu eလ5လ1ခ850ကပ?တယ#။ ဒ?eပမ85 Method eတ>ထ8မ91 ကuဒ#eတ>ကiu ဘယ#လiu eရ&ရမယ#ဆiuတ1ကiueတ15 မ
သiရeသ&ပ?ဘR&။ ဘယ#လiuလuပ#ရမယ#ဆiuတ1ကiu eရ&သ1&Biuင#တ1eတ>ကeတ15 Control Flow Statements eတ>ပ8
*ဖစ#0ကပ?တယ#။

Controls Flow Statement အeနန8A Decision Statement န8A Looping Statement ဆiu6ပ7&အမ(iu&အစ1&ခ>8*ခ1&ထ1&
ပ?တယ#။ အe*ခအeနတစ#ခuအeပLမRတည#6ပ7& Program အသ>1&အလ1eတ>ကiu ခ>8*ခ1&eပ&Biuင#တ85 Statement eတ>က
Decision Statemebt eတ>*ဖစ#0က6ပ7& အXကiမ#Xကiမ# လuပ#eဆ1င#မ-eတ>ကiu eရ&သ1&Biuင#eအ1င#*ပင#eပ&ထ1&တ85
Statement eတ>ကeတ15 Looping Statement eတ>ပ8 *ဖစ#0ကပ?တယ#။

Decision Statement
ဆiu0ကပ?စiu၊ ကiန#&*ပည#5တစ#ခuကiu 0ကည#56ပ7& စuPကiန#&လ1& မကiန#&လ1&ဆiuတ1ကiu ခ>8*ခ1&ခ(င#တယ#။ ဒ?ကiu ဘယ#eနရ1မ91သuP&
ရမလ8။ သuP&လiuရတ85 eနရ1eတ> အမ(1&Xက7&Diပ?တယ#။ View eပLမ91 Table တစ#ခuရ8S Row eတ>ကiu စuPမeပLမ91ခ>86ပ7&
အeရ1င#မတReအ1င#eဖL*ပခ(င#ရင# ဒ7လiuမ(iu& ကiန#&တစ#ခuကiu 0ကည#56ပ7& စuPလ1&မလ1&ဆiuတ1ကiu ခ>8*ခ1&ရeတ15မ91ပ?ပ8။

စuPဆiuရင# အ*ဖteရ1င#ကiueဖL*ပ6ပ7& မဆiuရင# မ7&ခiu&eရင#န8A eဖL*ပခ(င#တယ#ဆiuရင# အရင#ဆuP& Row ရ8S Index Number ဟ1
စuPလ1&မလ1&ဆiuတ1ကiu ခ>8*ခ1&ဖiuလiuပ?တယ#။ ဒ7လiuအeနအထ1&မ(iu&ဆiuရင# ကOန#eတ1#တiuဟ1 Decision Statement
eတ>ကiu အသuP&*ပueတ15မ91 *ဖစ#ပ?တယ#။

Java Programming Language မ91 အသuP&*ပuBiuင#တ85 Decision Statement eတ>ကeတ15 eအ1က#ပ? အတiuင#& *ဖစ#
0ကပ?တယ#။

If Statement

အeနအထ1&တစ#ခuအeပLမRတည#6ပ7& မ9န#ကန#တ85အခ?မ9 လuပ#eဆ1င#မ-eတ>ကiu လuပ#eဆ1င#eစလiuတ85အခ?မ(iu&မ91 If


Statement ကiu eရ&သ1& အသuP&*ပuBiuင#ပ?တယ#။

Learning Java 73 မင#&လ>င#


Java Developer Class
If Statement ကiu eရ&သ1&မယ#ဆiuရင# အရင#ဆuP& Condition တစ#ခuကiu if (Condition) ကiu eရ&သ1&ရမ91 *ဖစ#ပ?
တယ#။ 6ပ7&မ9 Condition သ1မ9န#ခ85ရင# အလuပ#လuပ#eစခ(င#တ85 ကuဒ#eတ>ကiu { If Clause } ထ8မ91 eရ&သ1&ရမ91 *ဖစ#ပ?
တယ#။ Condition eနရ1မ91 Boolean တန#ဖiu&တစ#ခuကiu *ပန#eပ&Biuင#တ85 eရ&သ1&ပuP (Boolean Expression) တစ#ခuခu
ကiu eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။

Boolean Expression ဆiuတ1 eရ&သ1&ထ1&တ85 Expression ကiu Evaluate လuပ#6ပ7&တ85အခ?မ91 eန1က#ဆuP&


Boolean တန#ဖiu&တစ#ခu ထ>က#လ1Biuင#တ85 Expression မ(iu&*ဖစ#ပ?တယ#။ eအ1က#ပ? Expression eတ>က Boolean
တန#ဖiu&ကiu *ပန#eပ&Biuင#တ85 Expression eတ>ပ8 *ဖစ#ပ?တယ#။

• Boolean Type Literals (true or false)


• Boolean Type Variables
• Boolean Type ကiu Return *ပန#eစတ85 Method Invocations
• Relational Expressions
• Logical Expressions

ဒ7eနရ1မ91 Literals eတ>eရ&တ1eတ15 ရeက1င#&ရပ?မယ#။ ဒ?eပမ85 လuP&ဝ အဓiပr?ယ#မDiပ?ဘR&။ if(true) လiueရ&ထ1&ရင#


အ6မ8တမ#& မ9န#eနမ91 *ဖစ#6ပ7& if(false) လiueရ&ထ1&ရင# အ6မ8တမ#&မ91&eနမ91 *ဖစ#တ85 အတ>က# if Statement က
အလuပ#လuပ#မ91 မဟuတ#ပ?ဘR&။ Xကiuသi6ပ7&သ1& ကiစuတစ#ခu *ဖစ#တ85 အတ>က# ဆuP&*ဖတ#စရ1eတ1င#မလiuအပ#ပ?ဘR&။
ကOန#eတ1#တiu JShell ကiu အသuP&*ပu6ပ7& စမ#&သပ#0ကည#50ကရeအ1င#။

jshell> int a = 10
a ==> 10

jshell> if (a == 10) {
...> [Link]("A is " + 10);
...> }
A is 10

jshell> if (a > 10) {


...> [Link]("A is greater than " + 10);
...> }

အထက#ပ?နမRန1ထ8မ91 a ရ8S တန#ဖiu&ကiu 10 န8A အစ1&ထiu&ထ1&ပ?တယ#။ 6ပ7&eတ15 eအ1က#မ91 a == 10 ဆiuတ1က


Condition အပiuင#*ဖစ#တ85 Boolean Expression ပ?။ a က 10 *ဖစ#ရင# true ကiu*ပန#6ပ7& မ*ဖစ#ရင# false ကiu *ပန#eပ&မ85
Expression တစ#ခu*ဖစ#ပ?တယ#။

If Clause ထ8မ91eတ15 Condition ကiu စစ#6ပ7&မ9န#ရင# အလuပ#လuပ#မည#5 ကuဒ#eတ>ကiu eရ&ထ1&ပ?တယ#။ အထက#ပ?


Condition က မ9န#တ85 အတ>က# eရ&သ1&ထ1&တ85 အတiuင#& A is 10 ဆiu6ပ7& အလuပ#လuပ#သ>1&တ1ကiu eတ>မရပ?မယ#။

ဆက#လက#6ပ7& မမ9န#တ85 Expression တစ#ခuကiu eအ1က#မ91eရ&သ1&ထ1&ပ?တယ#။ a > 10 လiu eရ&သ1&ထ1&ပ?တယ#။


a ဟ1 10 *ဖစ#တ85 အတ>က# a ဟ1 10 ထက#မXက7&ပ?ဘR&။ ဒ?5e0က1င#5 a > 10 Expression ကiu Evaluate လuပ#ရင#
false ပ8ရမ91 *ဖစ#ပ?တယ#။ ဒ?5e0က1င#5 If Clause ထ8မ91 eရ&သ1&ထ1&တ85 Code eတ>ကiu အလuပ#လuပ#eစမ91 မဟuတ#
eတ15ပ?ဘR&။

ဒ?5e0က1င#5 IF Statement eတ>ကiu Condition မ91eရ&သ1&တ1&တ85 အတiuင#&မ9န#ရင# အလuပ#လuပ#eစလiuတ85အခ?မ(iu&မ91


eရ&သ1& အသuP&*ပuသင#5ပ?တယ#။

Learning Java 74 မင#&လ>င#


Java Developer Class

IF / ELSE Statement

IF / ELSE Statement eတ>ကiu eရ&သ1&ထ1&တ85 Expression အeပLမ91မRတည#6ပ7& မ9န#ရင#တစ#မ(iu& အလuပ#လuပ#eစ


လiu6ပ7& မ91&ရင# တစ#မ(iu&လuပ#eစလiuတ85 အခ?မ(iu&မ91 အသuP&*ပuBiuင#ပ?တယ#။

If Else Statement eတ>ကလ8 IF Statement န8A အeတ1#eလ&ဆင#တRပ?တယ#။ မတRတ1ကeတ15 တကယ#လiu


Condition ကiu စစ#eဆ&တ85eနရ1မ91 မဟuတ#ခ85ရင# ဘ1လuပ#မလ8ဆiuတ1ကiu eရ&သ1&ထ1&Biuင#တ1ပ?ပ8။ အထက#ပ?
အတiuင#& If Clause န8A Else Clause တiuကiu eရ&သ1&ထ1&Biuင#6ပ7& တကယ#လiu Condition ကမ9န#ခ85ရင# If Statement
က အလuပ#လuပ#မ91 *ဖစ#6ပ7& မ91&ခ85ရင# Else Clause ထ8က လuပ#eဆ1င#ခ(က#eတ>ကiu လuပ#eဆ1င#eစမ91 *ဖစ#ပ?တယ#။

jshell> int a = 10
a ==> 10

jshell> if (a > 10) {


...> [Link]("A is greater than " + 10);
...> } else {
...> [Link]("A is not greater than " + 10);
...> }
A is not greater than 10

အထက#ပ?နမRန1မ91 eဖL*ပထ1&တ85 အတiuင#& a ကiu 10 န8A အစ1&ထiu&ပ?တယ#။ 6ပ7&eတ15 Condition မ91 a > 10 လiu စစ#
ထ1&ပ?တယ#။ ဒ?eပမ85 a ဟ1 10 *ဖစ#တ85 အတ>က# a ဟ1 10 ထက#မXက7&တ85 အတ>က# IF Clause ကiu အလuပ#မလuပ#
eတ15ပ8 Else Clause ကiuပ8အလuပ#လuပ#သ>1&မ91 *ဖစ#ပ?တယ#။

ဒ?5e0က1င#5 Else Clause ထ8မ91 eရ&သ1&ထ1&တ85 ကuဒ#eတ>ကiu အလuပ#လuပ#သ>1&မ91 *ဖစ#တ85 အတ>က# A is not greater
than 10 ဆiu6ပ7& eဖL*ပသ>1&တ1ကiu eတ>Zရပ?မယ#။

Learning Java 75 မင#&လ>င#


Java Developer Class

IF / ELSE IF / ELSE Statement

ဒ7 Statement ကeတ15 Condition အမ(iu&အမ(iu&အeပLမRတည#6ပ7& မiမiရ8S Program ရ8S Flow ကiue*ပ1င#&လ8လiuတ85 အခ?
မ(iu&မ91 eရ&သ1&အသuP&*ပuBiuင#ပ?တယ#။

ပထမ Conditon ကiu စစ# ကiuက#ရင#တစ#မ(iu&လuပ#၊ မကiuက#ရင# eန1က# Condition တစ#မ(iu&ကiu စစ# ကiuက#ရင#တစ#မ(iu&
လuပ# မကiuက#ရင# eန1က#ထပ# Condition အမ(iu&မ(iu&ကiu ထပ#စစ#eဆ&Biuင#ပ?တယ#။ eန1က#ဆuP&မ9 မစစ#ခ(င#ရင#လ8 Else
Clause ကiu eရ&သ1&ထ1&လiuရပ?တယ#။

jshell> /list showGrade

8 : void showGrade(int mark) {


if(mark < 40) {
[Link]("Fails");
} else if (mark >= 40 && mark < 80) {
[Link]("Pass");
} else if (mark >= 80 && mark < 100) {
[Link]("Excellent");
} else if (mark == 100) {
[Link]("Perfect");
} else {
[Link]("Impossible");
}
}

အထက#ပ?အတiuင#& showGrade Method ကiu eရ&သ1&ထ1&ပ?တယ#။ Argument အeနန8A int mark ကiu ရယReနပ?
တယ#။ အမ9တ#အeပLမRတည#6ပ7& အဆင#5ကiu ခ>8*ခ1&eပ&Biuင#တ85 Method တစ#ခuပ?။

Learning Java 76 မင#&လ>င#


Java Developer Class
ပထမဆuP& if ရ8S Decision အeနန8A mark < 40 လiueရ&ထ1&ပ?တယ#။ mark သ1 40 ထက#ငယ#ရင# if block ထ8က
ကuဒ#eတ>ကiu အလuပ#လuပ#eစမ91 *ဖစ#6ပ7 “Fails” လiu eဖL*ပသ>1&မ91 *ဖစ#ပ?တယ#။ အ85ဒ7 Condition မမ9န#eတ15မ9 အ*ခ1&
Condition eတ>ကiu *ပန#6ပ7& စစ#eဆ&မ91 *ဖစ#ပ?တယ#။

jshell> showGrade(39)
Fails

ဒ?5e0က1င#5 အထက#ပ? အတiuင#& showGrade(39) လiu eရ&တ85အခ?မ91 39 ဟ1 40 ထက#ငယ#eနတ85 အတ>က# if


Condition န8A ကiuက#ည76ပ7& Fails လiueဖL*ပသ>1&တ1*ဖစ#ပ?တယ#။

jshell> showGrade(40)
Pass

jshell> showGrade(79)
Pass

6ပ7&eတ15 ဒuတiယ Condition *ဖစ#တ85 else if ထ8မ91 mark >= 40 && mark < 80 လiueရ&ထ1&ပ?တယ#။ ဒ?e0က1င#5
ပထမ Condition န8A မကiuက#တ1eတ>အ1&လuP&ဟ1 ဒuတiယ Condition ထ8ကiu eရ1က#လ10ကပ?တယ#။ အထက#ပ? နမR
န1ထ8မ91eတ15 showGrade Method ကiu Parameter အeနန8A 40 န8A 79 ကiu အသuP&*ပuထ1&0ကပ?တယ#။ 40 eက1
79 ပ? 40 ဒ?မ9မဟuတ# 40 ထက#Xက7&eန6ပ7& 80 ထက#လ8 ငယ#eန0ကပ?တယ#။ ဒ?5e0က1င#5 Pass ဆiu6ပ7& eဖL*ပBiuင#0ကတ1
*ဖစ#ပ?တယ#။

jshell> showGrade(80)
Excellent

jshell> showGrade(99)
Excellent

jshell> showGrade(100)
Perfect

jshell> showGrade(101)
Impossible

အထက#ပ?နမRန1ထ8မ91eဖL*ပထ1&တ85 အတiuင#& 80 ဒ?မ9မဟuတ# 99 န8A showGrade Method ကiu eခLတ85 အခ?မ91


Excellent ဆiuတ1ကiu eဖL*ပeနတ1ကiu eတ>Zရပ?မယ#။ ဒ?ဟ1 တတiယ Condition ထ8မ91 eရ&သ1&ထ1&တ85 mark >=
80 && mark < 100 န8A ကiuက#ည7သ>1&လiu *ဖစ#ပ?တယ#။

eန1က#6ပ7& showGrade(100) လiu Method ကiu Invoke လuပ#လiuက#တ85 အခ?မ91လ8 Excellent လiu eဖL*ပBiuင#တ1ကiu
eတ>Zရပ?တယ#။ ဒ?ကလ8 စတuတv Condition *ဖစ#တ85 mark == 100 န8A ကiuက#ည7တ85 အတ>က# eဖL*ပeပ&သ>1&တ1 *ဖစ#
6ပ7& eန1က#ဆuP& 101 န8A Method ကiu Invoke လuပ#တ85အခ? အထက#eဖL*ပပ? Condition တစ#ခuန8Aမ9 မကiuက#ည7ပ8 Else
Clause ထ8က လuပ#eဆ1င#ခ(က#eတ>ကiuလuပ#eပ&သ>1&တ85 အတ>က# Impossible ဆiuတ1ကiu eဖL*ပeပ&သ>1&တ1 *ဖစ#ပ?
တယ#။ ဒ7 Statement မ(iu&ကiu Condition အမ(iu&မ(iu&အeပL လuပ#eဆ1င#မ-eတ>ခ>8*ခ1&6ပ7& လuပ#eဆ1င# eစလiuတ85အခ?မ91
အသuP&*ပuBiuင#ပ?မယ#။

Learning Java 77 မင#&လ>င#


Java Developer Class

Switch Statement

Switch Statement eတ>ဟ1 if - else if - else statement eတ>န8A တRည7သလiuDiပ?တယ#။ Decision အမ(1&အeပL
မ91 မRတည#6ပ7& လuပ#eဆ1င#မ-eတ>ကiu ခ>8*ခ1&လuပ#eဆ1င# eစBiuင#မ91 *ဖစ#ပ?တယ#။ ဒ?eပမ85 if - else if - else statement
eတ>မ91eတ15 Decision eတ>ကiu လiuအပ#သလiu eရ&သ1&Biuင#eပမ85၊ switch statement eတ>မ91eတ15 decision eတ>
ကiu Xကiuတင#သတ#မ9တ#ထ1&တ85 တန#ဖiu&အeပLမ91 မRတည#6ပ7& လuပ#eဆ1င#မ-eတ>ကiu ခ>8*ခ1&လuပ#eဆ1င# eစမ91 *ဖစ#ပ?
တယ#။

Switch Statement eတ>ဟ1 တန#ဖiu&တစ#ခuအeပLမ91 မRတည#6ပ7& လuပ#eဆ1င#မ-မeတ>ကiu ခ>8*ခ1&6ပ7& လuပ#eဆ1င#eပ&Biuင#


ပ?တယ#။ Switch မ91 အသuP&*ပuBiuင#တ85တန#ဖiu&eတ>ကeတ15 Primitive Data Type ထ8ကeတ15 char, byte, short န8A
int တiuကiu အသuP&*ပuBiuင#ပ?တယ#။ Java SE 5 အeန1က#မ91 ပ?ဝင#လ1ခ85တ85 enum type eတ>ကiuလ8 Switch
Statement ရ8S တန#ဖiu&အeနန8A အသuP&*ပuBiuင#ပ?တယ#။ Java SE 7 ကeနစ6ပ7&eတ15လ8 String Type Variable eတ>
ကiuလ8 Switch Statement ရ8S Value အeနန8A အသuP&*ပuလ1Biuင#ပ?တယ#။

Switch Statement eတ>ထ8မဟ1 Case eတ>ကiu တစ#ခuထက#မက eရ&သ1&လiuရBiuင#ပ?တယ#။ Case eတ>မ91 Switch
လuပ#မည#5 Value ရ8S Type အလiuက# Literals eတ> ဒ?မ9မဟuတ# Constant eတ>ကiu eရ&သ1&Biuင#ပ?တယ#။ တကယ#လiu
Value ဟ1 Switch လuပ#မည#5 Value န8A ကiuက#ည7မယ#ဆiuရင# Case eတ>ထ8ကiu ဝင#eရ1က# အလuပ#လuပ#eစမ91 *ဖစ#ပ?
တယ#။

Case eတ>ရ8S Statement eတ>ကiu လuပ#eဆ1င#ရင#& break; ကiu eတ>ရင#eတ15 Switch Statement eတ>ထ8ကeန
ထ>က#ခ?သ>1&မ91 *ဖစ#ပ?တယ#။ ဒ?eပမ85 beak ကiu မeတ>Zခ85ရင#eတ15 Statement eတ>အ1&လuP&ကiu 6ပ7&တ85 အထi
လuပ#eဆ1င# သ>1&မ91 *ဖစ#ပ?တယ#။

Learning Java 78 မင#&လ>င#


Java Developer Class
နမRန1အeနန8A int Number အလiuက# ဘ1သ1ရပ#eတ>ကiu eဖL*ပတ85 Statement တစ#ခuကiu eရ&သ1&0ကည#5ပ?မယ#။

void show(int i) {

switch(i) {
case 1:
[Link]("Java SE");
break;
case 2:
[Link]("Java EE");
break;
case 3:
[Link]("Spring");
break;
default:
[Link]("Everything");
break;
}
}

အထက#ပ?အတiuင#& JShell န8A show ဆiuတ85 Method တစ#ခuကiu eရ&သ1&ထ1&ပ?တယ#။ အ85ဒ7 Method မ91 int အမ(iu&
အစ1& Variable i ကiu Parameter အeနန8A ရယRထ1&ပ?တယ#။ 6ပ7&eတ15မ9 i အeပLမRတည#6ပ7& Switch လuပ#eနပ?
တယ#။ { န8A စ6ပ7& } န8A ဆuP&တ85 eနရ1ကeတ15 Switch Block *ဖစ#6ပ7& အ85ဒ7အထ8မ91eတ15 case eတ>ကiu တစ#ခuထက#
မက eရ&သ1&Biuင#ပ?တယ#။

အထက#ပ? နမRန1ထ8မ91ဆiuရင# i ရ8S တန#ဖiu&ဟ1 ပထမဆuP& case မ91eဖL*ပထ1&တ85 တန#ဖiu&န8A ကiuက#ည7ရင# အ85ဒ7 case ထ8
မ91 eရ&သ1&ထ1&တ85 Statement eတ>ကiu အလuပ#လuပ#eစမ91 *ဖစ#ပ?တယ#။ အ85ဒ7eနရ1မ91 break ကiu eတ>Zတ1န8A
switch statement ထ8က ထ>က#သ>1&မ91 *ဖစ#6ပ7& တကယ#လiu break သ1 မeတ>Zပ?က Switch Statement ဆuP&တ85
အထi အထ8က Statement eတ>ကiu လuပ#eဆ1င#သ>1&မ91 *ဖစ#ပ?တယ#။

Switch Block ထ8မ91 case eတ>အ*ပင# default ကiuလ8 တစ#ခu eရ&သ1&Biuင#ပ?တယ#။ i ရ8S တန#ဖiu&ဟ1 ဘယ# case ရ8S
တန#ဖiu&န8Aမ9 မတRခ85ရင# default ထ8က Statement ကiuလuပ#eဆ1င#သ>1&မ91 *ဖစ#ပ?တယ#။

jshell> show(2)
Java EE

jshell> show(4)
Everything

အထက#ပ? ကuဒ#eတ>ထ8မ91လiu show method ကiu 2 န8A eပ&eခL0ကည#5တ85 အခ?မ91 case 2 ထ8က Statement ကiu
လuပ#eဆ1င# သ>1&တ85 အတ>က# Java EE လiu eဖL*ပeန6ပ7& 4 န8A eပ&eခL0ကည#5တ85အခ? default ထ8က Statement ကiu
လuပ#eဆ1င#သ>1&eစတ85 အတ>က# Everything လiueဖL*ပeပ&သ>1&တ1ကiu eတ>Zရမ91 *ဖစ#ပ?တယ#။

Learning Java 79 မင#&လ>င#


Java Developer Class

Looping Statements
လuပ#eဆ1င#မ-eတ>ကiu အXကiမ#Xကiမ# လuပ#eဆ1င#စလiuတ1တiu၊ Array ဒ?မ9မဟuတ# Collection ထ8က Object eတ>ကiu
အသuP&*ပu6ပ7& တစ#ခu6ပ7&တစ#ခu လuပ#eဆ1င#မ-eတ>ကiu *ပuလuပ#eစလiuတ85 အခ?မ(iu&eတ>မ91 Looping Statement eတ>ကiu
အသuP&*ပuBiuင#ပ?တယ#။ Java မ91 Looping Statement အeနန8A for statement, while statement န8A do - while
statement ဆiu6ပ7& eရ&သ1&Biuင#0ကပ?တယ#။

For statement

Java Programming မ91 For Statement ကiu Counter အeပLမ91 မRတည#6ပ7& Loop ပတ#တ85 ပuPစPန8A Collection ဒ?မ9
မဟuတ# Array တစ#ခuထ8မ91 Diတ85 Element eတ>ကiu Iterate လuပ#တ85 ပuPစPဆiu6ပ7& B9စ#မ(iu&eရ&သ1&Biuင#ပ?တယ#။ အထက#ပ?
ပuPစPကeတ15 Counter အeပLမ91မRတည#6ပ7& Loop ပတ#တ85 ပuPစPမ(iu& *ဖစ#ပ?တယ#။

Counter ကiu အသuP&*ပu6ပ7& eရ&သ1&တ85 For Statement eတ>ဟ1 Java Programming ကiu စတင# Release လuပ#
တ85 အခ(iန#ကထ8က ပ?ဝင#ခ85တ85 eရ&သ1&ပuP*ဖစ#6ပ7& Index Number eတ>ကiu အသuP&*ပuလiuတ85 အခ?မ(iu&မ91 အသuP&*ပuသင#5တ85
eရ&သ1&ပuP*ဖစ#ပ?တယ#။

For Statement မ91 အစiတ# အပiuင#& ၄ ခuပ?ဝင#ပ?တယ#။ အ85ဒ?eတ>ကeတ15 Initialization, Condition, Update
Counter န8A For Block တiu *ဖစ#0ကပ?တယ#။ Initialization မ91eတ15 For Loop မ91 အသuP&*ပuမည#5 Counter ကiu
Initialize လuပ#တ85 Expression *ဖစ#ပ?တယ#။ int i = 0 လiueရ&သ1&ထ1&တ85 အပiuင#& *ဖစ#ပ?တယ#။ အ85ဒ7eနရ1မ91 i ဟ1
Counter *ဖစ#6ပ7& ကနဦ&တန#ဖiu&ကiu 0 လiu သတ#မ9တ#eနပ?တယ#။

ဒuတiယ Expression ကeတ15 Condition *ဖစ#6ပ7& Loop အတ>င#&ဝင#မယ# မဝင#ဘR&ဆiuတ1ကiu ဆuP&*ဖတ#eပ&Biuင#တ85


Expression *ဖစ#ပ?တယ#။ i < 10 လiu eရ&သ1&ထ1&တ85 အစiတ#အပiuင#&*ဖစ#6ပ7& i ဟ1 10 ထက#ငယ#eနရင# Loop
အတ>င#& ဝင#eရ1က#မ91 *ဖစ#6ပ7& for block { } အတ>င#&မ91 eရ&သ1&ထ1&တ85 Code eတ>ကiu လuပ#eဆ1င# eစမ91 *ဖစ#ပ?
တယ#။ တကယ#လiu i ရ8S တန#ဖiu&ဟ1 10 ထက# မငယ#eတ15ဘR&ဆiuရင#eတ15 for loop ကiu 6ပ7&ဆuP&eစမ91 *ဖစ#ပ?တယ#။

eန1က# Expression တစ#ခuကတ15 Counter ကiu Update လuပ#eပ&မည#5 Expression *ဖစ#ပ?တယ#။ Loop ထ8ကiu ဝင#
eရ1က#6ပ7& For Block ထ8က ကuဒ#eတ>ကiu လuပ#eဆ1င#6ပ7&တiuင#& Counter ကiu Update လuပ#မ91 *ဖစ#ပ?တယ#။
အထက#ပ? နမRန1ထ8မ91eတ15 i ++ ကiu eရ&သ1&ထ1&တ85 အတ>က# တစ#Xကiမ# Loop ပတ#6ပ7&တiuင#& Counter *ဖစ#တ85 i ရ8S
တန#ဖiu&က တစ#တiu&သ>1&မ91 *ဖစ#ပ?တယ#။

အထက#ပ? နမRန1 အတiuင#&ဆiuရင# ၁၀ Xကiမ# Loop ပတ#6ပ7&တ85 အခ?မ91 Counter ရ8S တန#ဖiu&ဟ1 10 *ဖစ#လ16ပ7& i < 10
ဆiuတ85 Boolean Expression ရ8S ရလဒ#ဟ1 false *ဖစ#လ1တ85အခ?မ91 For Loop ကiu 6ပ7&ဆuP&eစမ91 *ဖစ#ပ?တယ#။

Learning Java 80 မင#&လ>င#


Java Developer Class

jshell> String [] array = {"Hello", "Java"}


array ==> String[2] { "Hello", "Java" }

jshell> for(int i = 0; i < [Link]; i ++) {


...> [Link](array[i]);
...> }
Hello
Java

အထက#ပ? နမRန1ထ8မ91 array ထ8မ91 “Hello” န8A “Java” တiuကiu eရ&သ1&ထ1&ပ?တယ#။ 6ပ7&eတ15 for statement ရ8S
Counter ကiu 0 ကeန စeစ6ပ7& Condition အeနန8A i < [Link] လiu eရ&သ1&ထ1&တ85 အတ>က# i ဟ1 array ရ8S
Length *ဖစ#တ85 2 ထက# ငယ#eနသမT Loop အတ>င#&Di ကuဒ#eတ>ကiu အလuပ#လuပ#eစမ91 *ဖစ#ပ?တယ#။ ဒ?5e0က1င#5 B9စ#
Xကiမ# Loop ပတ#သ>1&တ1ကiu eတ>Zရပ?လiမ#5 မယ#။

ဒ7လiuမ(i& Array ထ8မ91Diတ85 Element eတ>ကiu Iterate လuပ#6ပ7& အသuP&*ပuခ(င#ရင# eန1က#တစ#မ(iu& eရ&သ1&Biuင#ပ?eသ&
တယ#။ အ85ဒ?ကeတ15 Java 5 မ91 စတင#ပ?ဝင#လ1ခ85တ85 Enhance For Statement *ဖစ#ပ?တယ#။

Enhance For Statement eတ>ဟ1 Array ဒ?မ9မဟuတ# Collection eတ>ရ8S Element eတ>ကiu တစ#ခuခ(င#& Iterate
လuပ#6ပ7& အသuP&*ပuလiuတ85 အခ?မ(iu&မ91 အသuP&ဝင#ပ?တယ#။ for (Type obj : list) ဆiu6ပ7& eရ&သ1&ထ1&တ85 eနရ1မ91 Type
ဟ1 list ထ8မ91Diတ85 Element ရ8S Type ကiu eရ&သ1&ရမ91 *ဖစ#ပ?တယ#။ list Object ထ8မ91Diတ85 Element eတ>က obj
အeနန8A Itelrate လuပ#6ပ7& for block ထ8မ91 အသuP&*ပuBiuင#မ91 *ဖစ#ပ?တယ#။ List ထ8မ91Diတ85 Element eတ> အ1&လuP&
Iterate လuပ#6ပ7&တ85 အခ?မ91 for loop ကiu 6ပ7&ဆuP&eစမ91 *ဖစ#ပ?တယ#။

jshell> for(String str : array) {


...> [Link](str);
...> }
Hello
Java

အထက#ပ?နမRန1ထ8မ91 for (String str : array) လiueရ&သ1&ထ1&တ85 အတ>က# array ထ8မ91Diတ85 String eတ>ကiu
Iterate လuပ#သ>1&မ91 *ဖစ#ပ?တယ#။ array = {“Hello”, “Java”} လiueရ&သ1&ထ1&တ85 အတ>က# array ထ8မ91 String
B9စ#ခuDi6ပ7& အe*ဖမ91လ8 Hello န8A Java ကiu ထuတ#*ပသ>1&Biuင#တ1ကiu eတ>Zရပ?မယ#။

Enhance For Statement eတ>ဟ1 Array ဒ?မ9မဟuတ# Collection ထ8က Element eတ>ကiu တစ#ခuခ(င#& Iterate
လuပ#6ပ7& အသuP&*ပuBiuင#တ85 အတ>က# For Each Statement လiueခLဆiueလ5Diပ?တယ#။

Learning Java 81 မင#&လ>င#


Java Developer Class

While Statement

Condition တစ#ခuဟ1 မ9န#eနသ၍ အXကiမ#Xကiမ# အလuပ#လuပ#eနeစလiuတ85 အခ?မ(iu&မ91 while statement ကiu eရ&သ1&
အသuP&*ပuBiuင#ပ?တယ#။

While Statement ကiu eရ&သ1&တ85 eနရ1မ91 While Block ထ8ကiu ဝင#6ပ7& အလuပ#လuပ#မလ1& မလuပ#ဘR&လ1&ဆiuတ1
ကiu စစ#eဆ&Biuင#တ85 Condition န8A အ85ဒ7 Condition မ9န#eနခ85ရင# အလuပ#လuပ#eစခ(င#တ85 ကuဒ#eတ>ကiu eရ&ထ1&လiuရတ85
While Block တiu ကiu eရ&သ1&ထ1&ဖiuလiuအပ#ပ?တယ#။

Program Flow အရဆiuရင# အရင#ဆuP& Condition န8A စစ#တယ#၊ ကiuက#ရင# While Block ထ8က ကuဒ#eတ>ကiu အလuပ#
လuပ#eစပ?တယ#။ မကiuက#ရင# While Loop ထ8က eန ထ>က#ပ?မယ#။ While Block ထ8က ကuဒ#eတ>ကiu လuပ#eဆ1င#6ပ7&
တiuင#& Condition ကiu *ပန#6ပ7& စစ#ပ?တယ#။ Condition န8A ကiuက#ည7eနတiuင#& While Block ထ8က ကuဒ#eတ>ကiu အလuပ#
လကuပ#eစမ91 *ဖစ#ပ?တယ#။ Condition eနရ1ကiu Boolean Expression န8A eရ&သ1&Biuင#6ပ7& အ85 ဒ7eနရ1မ91 true ကiu
သ1eရ&ထ1&မယ#ဆiuရင# While Loop ထ8ကeန ထ>က#Biuင#eတ15မ91 မဟuတ#ပ?ဘR&။

jshell> int count = 0;


count ==> 0

jshell> int limit = 5;


limit ==> 5

jshell> while(count < limit) {


...> [Link](count);
...> count++;
...> }
01234

အထက#ပ?နမRန1ထ8မ91eတ15 count ကiu 0 လiueပ&ထ1&6ပ7& limit ကiu 5 လiueပ&ထ1&ပ?တယ#။ While Loop ထ8ကiu ဝင#မ
လ1&ဆiuတ1ကiu စစ#တ85 Condition eနရ1မ91 count < limit လiu eရ&သ1&ထ1&ပ?တယ#။ count ဟ1 limit ထက#ငယ#
eနတ85 အတ>က# While Block ထ8က ကuဒ#eတ>ကiu အလuပ#လuပ#eစမ91 *ဖစ#ပ?တယ#။ While Block ထ8မ91eတ15 count
ကiu Print လuပ#6ပ7& count ကiu တစ#တiu&eနeစပ?တယ#။ ဒ?e0က1င#5 Loop တစ#ခ? ပတ#6ပ7&တiuင#& Count ဟ1 တစ#တiu&
သ>1&ပ?မယ#။ ၅ Xကiမ# Loop ပတ#6ပ7&တ85အခ?မ91eတ15 count ဟ1 5 *ဖစ#လ16ပ7& Condition စစ#တ85အခ?မ91 မမ9န#eတ15
ပ8 Loop ထ8ကeန ထ>က#သ>1&မ91 *ဖစ#ပ?တယ#။

While Statement ကiu eရ&သ1&တ85အခ?မ91 Condition ကiu တစ#ခ(iန#ခ(iန#မ91 ထ>က#eအ1င# eရ&သ1&ထ1&ဖiuလiuပ?တယ#။


မဟuတ#ရင# Endless Loop *ဖစ#သ>1&eစBiuင#တယ#ဆiuတ1ကiu သတiထ1&ဖiuလiuအပ#ပ?လiမ#5မယ#။

Learning Java 82 မင#&လ>င#


Java Developer Class

Do While Statement

Do While Statement ဟ1လ8 Condition တစ#ခuအeပLမRတည#6ပ7& အXကiမ#Xကiမ#လuပ#eဆ1င# eစBiuင#တ85 Statement


တစ#မ(iu&*ဖစ#ပ?တယ#။

While Statement န8A ဆင#တRသလiuDieပမ85 မတRည7တ1ကeတ15 Do Block ထ8က ကuဒ#eတ>ကiu အရင# အလuပ#လuပ#eစ
6ပ7&မ9 While ထ8က Condition ကiu စစ#eဆ&eနတ1ပ8 *ဖစ#ပ?တယ#။ အ85ဒ7အတ>က# Do Block ထ8က Code eတ>ကiu အ
န8ဆuP& တစ#Xကiမ#eတ15 အလuပ#လuပ#eစမ91 *ဖစ#ပ?တယ#။ Do Block ထ8က ကuဒ#eတ>ကiu အလuပ#လuပ#6ပ7&တ85 အခ?မ91
While Condition န8A စစ#eဆ&6ပ7& မ9န#တယ#ဆiuရင# eန1က#တစ#Xကiမ# Do Block ကiu အလuပ#လuပ#eစ6ပ7& မ91&တယ#ဆiuရင#
eတ15 Do While Loop ထ8ကeန ထ>က#သ>1&မ91 *ဖစ#ပ?တယ#။

jshell> int count = 3;


count ==> 3

jshell> while(count < 3) {


...> [Link](count);
...> count ++;
...> }

jshell>

jshell> do {
...> [Link](count);
...> count ++;
...> } while (count < 3);
3

အထက#ပ?နမRန1ထ8မ91 while ရ8S Condition န8A do while ရ8S Condition တiuဟ1 အတRတRပ?ပ8။ count ဟ1 3 *ဖစ#တ85
အတ>က# B9စ#ခuလuP& Condition န8A မကiuက#ည7ပ?ဘR&။ While Statement ရ8S Loop ထ8ကiu မဝင#eပမ85 Do While
Statement ထ8ကiueတ15 တစ#Xကiမ# အလuပ#လuပ#သ>1&တ1ကiu eတ>Zရပ?မယ#။ Do While Statement ဟ1 Do ကiu တစ#
Xကiမ# အလuပ#လuပ#ခiuင#&6ပ7&မ9 Condition ကiu စစ#eဆ&eနတ85 အတ>က# *ဖစ#ပ?တယ#။

Learning Java 83 မင#&လ>င#


Java Developer Class

Branching Statement
ပ^iuဂရမ# အသ>1&အလ1eတ>ကiu e*ပ1င#&ပစ#eအ1င# eရ&သ1&လiuတ85 အခ?မ(iu&eတ>မ91 Branching Statement eတ>ကiu
အသuP&*ပuBiuင#ပ?တယ#။ ဆiu0ကပ?ဆiu၊ Method တစ#ခuကiu eရ&သ1&eနတ85 eနရ1မ91 ^uတ#တရက# အ85ဒ7 Method ကiu6ပ7&ဆuP&
eစလiuတ85အခ?မ(iu& Diတတ#0ကပ?မယ#။ တဖန# Looping eတ>ထ8ကeန အeနအထ1&တစ#မ(iu&e0က1င#5 Looping ကiu ဆ
က#6ပ7P& မလuပ#eစလiuတ85 အခ?မ(iu&Diပ?မယ#။ 6ပ7&eတ15 လက#Di Looping ထ8က Statement eတ>ကiu ဆက#6ပ7& မလuပ#eစ
လiueတ15ပ8 eန1က#တစ#eခ?က# Loop ပတ#eစလiuတ85 အခ?မ(iu& Diတတ#0ကပ?မယ#။ အ85ဒ7လiu အခ?မ(iu&မ91 Branching
Statement eတ>ကiu အသuP&*ပuBiuင#ပ?တယ#။

Branching Statement အeနန8A အသuP&*ပuBiuင#တ1ကeတ15 Return, Break န8A Continue တiuပ8 *ဖစ#ပ?တယ#။
Branching Statement eတ>က Label eတ>န8Aတ>86ပ7& အသuP&*ပuBiuင#6ပ7& မiမiလiuအပ#သလiu Programming Flow ကiu
တစ#မ(iu&e*ပ1င#&6ပ7& လuပ#eဆ1င#eစBiuင#ပ?တယ#။

Return Statement

Return Statement eတ>ကiu Return Type ပ?တ85 Method eတ>မ91 eရ&သ1&ခ85ဘR&ပ?တယ#။ Method eတ>ရ8S
eန1က#ဆuP&eနရ1မ91 Method Signature မ91ပ?တ85 Return Type အလiuက# တန#ဖiu&တစ#ခuခuကiu Return *ပန#eပ&ခ850က
မ91ပ?။ တနည#&e*ပ1ရမယ#ဆiuရင# အ85ဒ?ဟ1လ8 Return Statement တစ#ခu*ဖစ#ပ?တယ#။

boolean isEven(int data) {


return data % 2 == 0;
}

အထက#ပ? Method ဟ1 int တန#ဖiu&တစ#ခuကiu စuPကiန#&တစ#ခu ဟuတ#မဟuတ# စစ#eပ&Biuင#တ85 Method တစ#ခu *ဖစ#ပ?
တယ#။ အ85ဒ7 Method မ91 boolean type ကiu Return Type အeနန8A သတ#မ9တ#ထ1&တ85 အတ>က# အ85ဒ7 Method ရ8S
eန1က#ဆuP&တစ#eနရ1မ91 boolean value ကiu Return *ပန#eပ&ရပ?မယ#။ ဒ7eနရ1မ91 return data % 2 == 0 လiu
Boolean Expression တစ#ခuကiu eရ&သ1&ထ1&ခ85ပ?တယ#။ ဒ?5e0က1င#5 data ကiu 2 န8A စ1&6ပ7& အqကင#&ဟ1 0 *ဖစ#ရင#
true ကiu return *ပန#မ91 *ဖစ#6ပ7& မဟuတ#ရင#eတ15 false ကiu return *ပန#မ91 *ဖစ#ပ?တယ#။

ဒ7eနရ1မ91 သတi*ပueစလiuတ1ကeတ15 return *ပန#6ပ7&ရင# အ85ဒ7 Method ထ8က ဘ1ကuဒ#ကiuမ9 eရ&လiu မရeတ15ဘR&ဆiu
တ1ကiuပ?ပ8။ ဒ7eနရ1မ91 eရ&သ1&ထ1&တ85 return data % 2 == 0; ဆiuတ1က return statement *ဖစ#ပ?တယ#။

Return *ပန#စရ1မလiuတ85 void method eတ>မ91လ8 return statement ကiu eရ&သ1&Biuင#ပ?တယ#။ void method eတ>
မ91 return; ဆiu6ပ7& eရ&သ1& လiuက#တ1န8A Method ထ8မ91Diတ85 return statement ရ8S eန1က#က ကuဒ#eတ>ကiu ဆက#မ
လuပ#eတ15ပ8 အ85ဒ7 Method ကiu 6ပ7&ဆuP&eစမ91 *ဖစ#ပ?တယ#။

void sell(String item) {


if(isNoItem()) {
return;
}
[Link](item + " has to sell");
}

Learning Java 84 မင#&လ>င#


Java Developer Class
အထက#ပ? sell method ထ8မ91 if statement တစ#ခuကiu eရ&သ1&ထ1&ပ?တယ#။ if statement ထ8မ91eတ15
isNoItem() ဆiuတ85 Method ကiu Invoke လuပ#ထ1&ပ?တယ#။ အ85ဒ7 isNoItem() ဆiuတ85 Method ကiu if statement ထ8
မ91 eရ&သ1&ထ1&တ85 အတ>က# isNoItem() method ရ8S Return Type ဟ1 boolean တန#ဖiu&တစ#ခu *ဖစ#ရပ?မယ#။
တကယ#လiu eရ1င#&စရ1 မDieတ15ရင# true ကiu Return *ပန#6ပ7& eရ1င#&စရ1Dieသ&ရင#eတ15 false လiu Return *ပန#ပ?
မယ#။

eရ1င#&စရ1မDieတ15ရင# if block ထ8ကiu ဝင#လ1မ91*ဖစ#6ပ7& retrun; statement ကiu အလuပ#လuပ#eစမ91 *ဖစ#ပ?တယ#။


return statement ကiu method ထ8မ91ပ8eရ&သ1&လiuရ6ပ7&၊ အ85ဒ7 Statement ကiu eတ>Zတ1န8A လက#Di Method ကiu
အလuပ#လuပ#eနတ1ကiu 6ပ7&ဆuP&eစမ91 *ဖစ#ပ?တယ#။ ဒ?5e0က1င#5 eရ1င#&စရ1မDieတ15ရင# အ85ဒ7eနရ1မ91ပ8 Method
လuပ#eဆ1င#မ-ကiu 6ပ7&ဆuP&eစ6ပ7& eန1က#က ကuဒ#eတ>ကiu ဆက#6ပ7& အလuပ#လuပ#မ91 မဟuတ#eတ15ပ?ဘR&။

Return Statement ကiu eရ&သ1&တ85 eနရ1မ91 return statement ဟ1 လက#DiDieနတ85 Block ရ8S eန1က#ဆuP& ကuဒ#
*ဖစ#ဖiuလiuအပ#ပ?တယ#။ အထက#ပ?နမRန1ထ8မ91 ဆiuရင# return ကiu if block ထ8မ91 eရ&သ1&ထ1&တ85 အတ>က# အ85ဒ7
Block ထ8မ91ဆiuရင# return ဟ1 eန1က#ဆuP& Statement *ဖစ#eနဖiuလiuပ?တယ#။

တကယ#လiu block တစ#ခuထ8မ91 return statement ရ8S eန1က#မ91 eရ&သ1&ထ1&တ85 ကuဒ#eတ>Dieနမယ#ဆiuရင#


Compile Time မ91တင# Exception ကiu *ဖစ#eပLeစမ91 *ဖစ#ပ?တယ#။ အထက#ပ?နမRန1ထ8မ91လiu [Link]
လiueရ&ထ1&တ85 ကuဒ#eတ>ဟ1 return eအ1က#မ91 Dieနeပမ85 Block မတRပ?ဘR&။ return ဟ1 if block ထ8မ91Di6ပ7&
[Link] ဟ1 method block ထ8မ91 Diပ?တယ#။ ဒ7လiuမ(iu& Block မတRရင#eတ15 eရ&သ1&လiuရပ?တယ#။
သတiထ1&ရမ91က Block တစ#ခuထ8မ91 return statement ရ8S eန1က#မ91 ဘ1ကuဒ#မ9 eရ&လiu မရဘR&ဆiuတ85 အခ(က#ပ8
*ဖစ#ပ?တယ#။ Java Programming ကiu စတင#eလ5လ1တ85 eနရ1မ91 ကuဒ# Block eတ>န8A သRတiuရ8S Scope eတ>ကiu
eက1င#&eက1င#& န1&လည#ထ1&ဖiuလiuအပ#ပ?တယ#။

Break Statement

Break Statement eတ>ကiu Switch Statement eတ>၊ Looping Statement eတ>*ဖစ#0ကတ85 For, While, Do
While Statement eတ>န8A တ>8ဖက#eရ&သ1&Biuင#ပ?တယ#။ အe0က1င#&တစ#မ(iu&မ(iu&e0က1င#5 break statement ကiu
eရ1က#6ပ7ဆiuတ1န8A လက#Di Statement ထ8ကeန ထ>က#eပ&Biuင#ပ?တယ#။ Switch Statement န8A break ကiu တ>8သuP&
တ1ကiueတ15 Switch Statement မ91 eလ5လ1ခ856ပ7& *ဖစ#တ85 အတ>က# ဒ7eနရ1မ91eတ15 Looping Statement န8A
ပတ#သက#တ1eတ>ကiuပ8 eဖL*ပသ>1&မ91 *ဖစ#ပ?တယ#။

for (int i = 0; i < 10; i ++) {


if(i == 5) {
break;
}
[Link]("Lopping count is " + (i + 1));
}

အထက#ပ?နမRန1ကuဒ#eတ>မ91 For Statement တစ#ခuကiu eရ&သ1&ထ1&ပ?တယ#။ Counter *ဖစ#တ85 i ဟ1 0 ကeန စ


6ပ7& 10 ထက#ငယ#eနသမTeတ15 ဒ7 Loop ထ8မ91 အလuပ#လuပ#eနမ91ပ?။ ဒ?eပမ85 for block ထ8မ91 if Statement တစ#ခu
ကiu eရ&သ1&ထ1&6ပ7& i == 5 လiu eရ&သ1&ထ1&ခ85ပ?တယ#။

ဒ?e0က1င#5 Counter *ဖစ#တ85 i ဟ1 5 *ဖစ#သ>1&ရင# break statement ကiu အလuပ#လuပ#eစမ91 *ဖစ#ပ?တယ#။ ဒ?5e0က1င#5 5
Xကiမ# Loop ပတ#6ပ7&တ85 အခ?မ91 i ရ8S တန#ဖiu&ဟ1 5 *ဖစ#သ>1&6ပ7& if block ကiu အလuပ#လuပ#eစမ91 *ဖစ#ပ?တယ#။ if block
ထ8မ91 break ကiu eရ&ထ1&တ85 အတ>က# for loop ကiu 6ပ7& ဆuP&eစမ91 *ဖစ#ပ?တယ#။

Learning Java 85 မင#&လ>င#


Java Developer Class
Break Statement ဟ1လ8 block ရ8S eန1က#ဆuP& Statement *ဖစ#တ85 အတ>က# break ရ8S eန1က#မ91 ဘ1ကuဒ#eတ>မ9
eရ&သ1&လiu ရမ91 မဟuတ#ပ?ဘR&။ break; လiueရ&ထ1&တ1န8A Current Looping Block ကiu 6ပ7&ဆuP&eစမ91 *ဖစ#ပ?တယ#။
တဖန# break statement ကiu label eတ>န8Aလ8 တ>86ပ7& အသuP&*ပuBiuင#ပ?တယ#။

ဒ7လiu eရ&သ1&ပuPက Nested Loop eတ>မ91 အသuP&*ပuBiuင#ပ?တယ#။ Nested Loop ဆiuတ1က Looping Statement
eတ>မ91 ထပ#6ပ7& Looping Statement eတ>ကiu eရ&သ1&တ85 ပuPစPပ?။

String [] months = {"JAN", "FEB", "MAR", "APR", "MAY",


"JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
int [] days = {1,2,3,4,5,6,7,8,9,10};

month:
for(String m : months) {

day:
for(int i : days) {

if(i == 5) {
continue month;
}

if(([Link]("MAR") || [Link]("MAY")) && i == 3) {


break day;
}

[Link]("%s : %d%n", m, i);


}
}

Java ဘ1သ1ရပ#မ91 Nested Loop ကiu eရ&သ1&တ85 eနရ1မ91 Condition အeပLမRတည#6ပ7& အတ>င#&ပiuင#& Loop ထ8
ကeန Label eတ>ကiu အသuP&*ပuက1 အ*ပင#ဘက#မ91Diတ85 Loop eတ>ကiuလ8 Control လuပ#Biuင#မ91 *ဖစ#ပ?တယ#။

အထက#ပ?နမRန1ထ8မ91 eဖL*ပထ1&တ85အတiuင#& အ*ပင#ဘက# For Loop ရ8SeUမ91 month: လiueရ&သ1&ထ1&6ပ7& month


ဟ1 အ*ပင#ဘက#က Loop ရ8S Label *ဖစ#ပ?တယ#။ တဖန# day: ကiuလ8 အတ>င#&ပiuင#& Loop မ91 eရ&သ1&ထ1&6ပ7& day
ဟ1 အတ>င#&ပiuင#& Loop ရ8S Label *ဖစ#ပ?တယ#။

Condition အeပLမRတည#6ပ7& continue တiu break တiuန8A Label eတ>ကiu အသuP&*ပu6ပ7& B9စ#သက#ရ1 Loop ကiu Control
လuပ#Biuင#မ91 *ဖစ#ပ?တယ#။ အထက#ပ?နမRန1ထ8မ91 i ဟ1 5 B9င#5တRရင# continue month လiueရ&သ1&ထ1&တ85 အတ>က# i
ဟ1 5 *ဖစ#တiuင#& အ*ပင#က Loop ကiu Continue လuပ#eစမ91 *ဖစ#ပ?တယ#။ ဒuတiယ Condition မ91eတ15 break day
လiueရ&သ1&ထ1&တ85အတ>က# အတ>င#&ပiuင#& Loop ကiu break လuပ#မ91 *ဖစ#ပ?တယ#။

တကယ#လiu Label *ဖစ#တ85 day ကiu မeရ&သ1&ထ1&ခ85ရင#လ8 Current Loop ကiu Break လuပ#မ91 *ဖစ#တ85 အတ>က#
အတ>င#&ပiuင#& Loop ကiuပ8 Break လuပ#သ>1&မ91 *ဖစ#ပ?တယ#။

Learning Java 86 မင#&လ>င#

You might also like