Boxing Conversion
Boxing ဆိုတာနဲ႕ လက္ေ၀ွ႕ထုိးတယ္မ်ားထင္မလားမသိဘူး။ primitive type ေတြကို သူတို႕အတြက္လုပ္ထားတဲ႔ refernce type ေတြဆီကို ေျပာင္းတာကို Boxing လုိ႕ေခၚတာပါ။ ေသတၱာထဲထည္႕တာေပါ႔ခမ်ာ။ ေအာက္မွာရွစ္ခုရိွတယ္တဲ႔ခင္ဗ်။
- From type
booleanto typeBoolean - From type
byteto typeByte - From type
charto typeCharacter - From type
shortto typeShort - From type
intto typeInteger - From type
longto typeLong - From type
floatto typeFloat - From type
doubleto typeDouble
Double ဆိုတာေတြ႕ရင္ မေနႏုိင္မထိုင္ႏိုင္ 1 cent issue ကိုသတိရမိတယ္ဗ်။ double ေတြဟာ သိမ္းစရာရိွတာကို အတည္႕မသိမ္းဘူးခင္ဗ်။ ဘာနဲ႕တူသလဲဆိုရင္ ဟာထဲကလို ႏြားေတြျဖတ္ေျပးတာကို ေျခေထာက္ေတြလုိက္ေရၾကည္႕ျပီး ေလးနဲ႕စားသလိုမ်ိဳးေပါ႔။ လြယ္တာကို ခက္ေအာင္လုပ္တယ္။ 60 ကို ၅၉.၉၉၉၉၉၉၉၉၉၉၉၉၉၉၉၉၉၉၉၉ ဆိုျပီးသိမ္းတဲ႔အတြက္ အတြက္အခ်က္ေတြ လုပ္ရင္း လုပ္ရင္း လုပ္ရင္း လုပ္ရင္း ေနာက္ဆံုး တစ္ျပားကြာကုန္တယ္ေပါ႔ဗ်ာ။ အဲဒါေၾကာင္႔လည္း BigDecimal ပဲေနာက္ပိုင္းေျပာင္းသံုးၾကတယ္။
Run time ၾကရင္ Boxing conversion ေတြက ဒီလိုေျပာင္းလဲသတဲ႔။
- If p is a value of type
boolean, then boxing conversion converts p into a reference r of class and typeBoolean, such that r.booleanValue()== p
p က boolean ဆိုပါေတာ႔။ လြယ္ေအာင္ေရးလိုက္ရင္ boolean p=true;
ဆိုပါေတာ႔။ run time ေရာက္ရင္ primitive type boolean ေလးကို Reference Type အျဖစ္ေျပာင္းပစ္လုိက္တယ္။ ေျပာင္းပစ္လုိက္တယ္ဆိုတာမွာ Reference Type Boolean r ထဲကို boolean တန္ဖိုးေရာက္သြားတယ္။ B အၾကီးေတြ အေသးေတြ ရွဳပ္ေနတယ္လို႕မထင္ပါနဲ႕။ Java ရဲ႕ၾကီးက်ယ္ေသာ boxing ေျပာင္းလဲမႈၾကီးပါ။
Boolean b;
boolean a=true;
b = new Boolean(a);
System.out.println(b.booleanValue());
Boolean ၾကီးဟာ reference type ၾကီးပါ။ သူက class လည္းျဖစ္တဲ႔အတြက္ သံုးခ်င္ရင္ new လုပ္ေပးရပါမယ္။ Boolean b= new Boolean("true") ဆိုျပီး တစ္ခါတည္း create လုပ္လုိက္တာကိုၾကေတာ႔ auto boxing လို႕ေခၚတယ္ဗ်။ Class ေတြကို compare လုပ္ေနတာလို႕ ခင္ဗ်ားတို႕သတိထားမိရင္ == operator ကို သံုးရင္လြဲမယ္ဆိုတာကိုလည္း ေျပာစရာမလုိပါဘူး။ Boolean ၾကီးကို == နဲ႕သြားျပီးတုိက္ဆုိင္ရင္ လြဲမွာပဲ။ သူ႕ရဲ႕ value က .booleanValue() နဲ႕ရွာမွထြက္လာမယ္။ .equals () operator နဲ႕လည္း နည္းနည္းစမ္းျပီးေရးၾကည္႕ပါ။ က်န္တာေတြလည္း အလားတူပါပဲ။
အဲဒီရွစ္မ်ိဳးကို ေျပာင္းျပန္လွည္႕ေျပာရင္ တနည္းအားျဖင္႔ Reference Type ကေန primitive ကိုေျပာင္းတာကို unboxing ေသတၱာထဲက ထုတ္တယ္လို႕ဆိုတာပါပဲ။ လူမ်ားေတြ boxing/unboxing လုပ္တယ္ဘာညာဆိုတာေတြကို အၾကီးၾကီးထင္မွာစိုးလုိ႕ပါ။ သိေအာင္လုပ္ထားတာေကာင္းပါတယ္။
Boxing was introduced in JDK 1.5 to eliminate trivial conversions between primitives (int, boolean, etc) and corresponding wrapper objects (Integer, Boolean, etc). Boxing allows some code to be a bit more concise and legible. It's also useful when using collections (which only take objects, not primitives).
Integer, Boolean စတဲ႔ Reference Type Objects ေတြကို wrapper objects လို႕လည္းေခၚတယ္ဆိုတာကို ေျပာခ်င္လုိ႕ကူးလာတာပါ။ JDK 1.5 ရဲ႕ သိသာထင္ရွားတဲ႔အမွတ္အသားတစ္ခုပါပဲ။
အဆင္ေျပၾကမယ္လို႕ထင္ပါတယ္။
နားမလည္ဘူးလား။ နားလည္လားေတာ႔မသိပါဘူး။ ၾကိဳးစားျပီး ရွာဖတ္ၾကည္႕ပါ။ ေရးေတးေတးျဖစ္ေနရင္လည္း မ်ားမ်ားေလးေလ႔လာၾကည္႕ပါ။ အဆင္ေျပသြားမွာပါ။ တကယ္တမ္း အလုပ္မွာ ဒီေလာက္သိစရာမလိုဘူး။ ေရးေနတာၾကာျပီ။ ကြ်န္ေတာ္ဆို ဒါကို Boxing လား၊ ၀ူရွဴးလားမသိဘဲ တစ္ႏွစ္ခြဲေလာက္ေရးေနတာပဲ။ ေရးလို႕ျဖစ္ပါတယ္။ ကိုယ္သိေနတာေတြကို ဘယ္လုိေခၚလဲျပန္ျပီးေလ႔လာရတာလည္း ေပ်ာ္စရာေကာင္းပါရဲ႕ဗ်ာ။
ကဲ Boxing သင္ခန္းစာ ဆံုးေလ၏။ ။
Regards,
Zero.
Ref:
http://javaeye.wordpress.com/2008/06/17/boxing-and-unboxing-conversion/
http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html
http://forums.techarena.in/software-development/1297430.htm
http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html
http://forums.techarena.in/software-development/1297430.htm
No comments:
Post a Comment