Monday, August 10, 2015

Lesson 11


ඉතින් යාලුවනේ කොහොමද ගොඩ කාලෙකින් නේද.... හිතනවා දැන් ජාවා අමතක වෙලා නැතුව ඇති කියල අපේ කස්ටියට හික් හික්....

අද මම කියල දෙන්න යන්නේ ඔයාලට method එකක් overloading කරන විදිය. ඉතාම සරල දෙයක් මෙතන තියෙන්නේ මම උදාහරණයකින් ම පැහැදිලි කරන්නම් කෝ ඔයලට,


public class ExampleMethodOverloading{

   public static void main(String[] args) {
       
      int a = 10;
      
      int b = 5;
      
      double c = 7.5;
      
      double d = 9.3;
      
      int result1 = minFunction(a, b);
    
      double result2 = minFunction(c, d);
      
      System.out.println("Minimum Value = " + result1);
      
      System.out.println("Minimum Value = " + result2);
      
   }



   public static int minFunction(int n1, int n2) {
       
      int min;
      
      if (n1 > n2)
          
         min = n2;
      
      else
          
         min = n1;

      return min; 
      
   }
   
   public static double minFunction(double n1, double n2) {
       
     double min;
     
      if (n1 > n2)
          
         min = n2;
      
      else
          
         min = n1;

      return min; 
      
   }
   
}



බලන්නකෝ ඔයාලට රෙසුල්ට් එක විදියට එන්න ඕනේ 

Minimum Value = 5
Minimum Value = 7.5


අපි දැන් බලමු කොහොමද මේ උත්තරය අපිට එන්නේ කියලා.

මුලින් ම කරලා තියෙන්නේ මම variables ටික define කරලා තියෙන එකයි.
 int variable දෙකකුයි double variable දෙකකුයි.


 ඊටපස්සේ බලන්න මම හදල තියනවා methods දෙකක් එකම නමින් parameters දෙකක් දල හැබැයි එකේ තියන parameters දෙක එකට එකක් වෙනස්.

බලන්නකෝ මම methods දෙකේම if  statement එකකින් check කරලා තියනවා මොකක්ද මින් value එක කියල.

ඊටපස්සේ ඉතින් methods දෙක ප්‍රින්ට් කරලා තියනවා තව variable දෙකකට assign කරලා. ඔන්න ඕක තමයි method overloading කියල තියන කතාව.


ඊටපස්සේ මම කියල දෙන්න යන්නේ ජවා වල command line arguments ගැන පොඩි කතාවක්. මේක හරිම සරල දෙයක්

command line arguments ඕනේ වෙන්නේ ඔයාලට ජාවා program එක run වෙනකොට මොනවා හරි arguments pass කරන්න ඕනේ නම් pass කරන්නේ command line arguments හරහා තමයි.

බලන්නකෝ මේ උදාහරණයත් එහෙනම්,


public class CommandLine {

   public static void main(String args[]){ 
       
      for(int i=0; i<args.length; i++){
          
         System.out.println("args[" + i + "]: " + args[i]);
         
      }
      
   }
   
}





ඔයාල මේක compile කරලා run කරනකොට දෙන්න ඕනේ මෙන්න මෙහෙම commands සෙට් එකක්

 this is a java command line arguments 200 -100



ඔයාලට රෙසුල්ට් එක විදියට එතකොට මෙන්න මේවගේ එකක් එන්න ඕනේ.


args[0]: this
args[1]: is
args[2]: a
args[3]: java
args[4]: command
args[5]: line
args[6]: arguments
args[7]: 200
args[8]: -100





කොහොමද එල කිරි එකට වැඩ නේද :D ;) 

අපි දැන් බලමු ජාවා වල එන constructors ගැන පොඩි විස්තරයක්.
constructors initialize වෙන්නේ object එකක් හැදුනම විතරයි.
constructors උත් හරියට නිකන් අපි ලියන class වගේම තමයි. විශේෂ ලක්ෂණය තමයි constructor එකකට විශේෂ return type එකක් නෑ

අපි constructors හැදුවේ  නැති උනත් හැම class එකටම default
constructor එකක් තියනවා.


 බලන්නකෝ මේ උදාහරණය එහෙනම්,




class MyClass {
    
    int x;

    MyClass(int i) {
        
        x = i;
        
    }
    
}

public class TestClass {

    public static void main(String args[]) {
        
        MyClass t1 = new MyClass(30);
        
        MyClass t2 = new MyClass(50);
        
        System.out.println(t1.x + " " + t2.x);
        
    }
    
}





Result එක විදියට ඔයාලට එන්න ඕනේ 30 50 කියල. 




ඔයාලට තවත් වැදගත් වෙන දෙයක් තමයි දැන් මම කිය්ටල දෙන්න යන්නේ ඒ තමයි finalize method එකමේ method එක ඔයාලගේ code එකේ කොහේ හරි තිබුනොත් අනිවා run වෙන method එකක් තමයි මේක. ගොඩක් වෙලාවට මේක use කරන්නේ database connection එකක් වගේ close කරන්න වගේ වැඩකට තමයි.

බලන්නකෝ මෙන්න මෙහෙම තමයි මේක අපි ලියන්නේ.

protected void finalize( )
 
{
 
   // finalization code here
 
}


 එහෙනම් ඉතින් අදට මට යන්න වෙලාව හරි අයෙත් අලුත් පොස්ටුවක් අරගෙන නැවතත් හමුවෙමු.  හැමෝටම ජයවේවා....