programming පට්ටටම දන්නා අය එන්න

DooA

Well-known member
  • Jun 22, 2011
    5,202
    773
    113
    ආගිය අතක් නැත
    programming පට්ටටම දන්නා අය එන්න


    ජාවා වල විතරක් නෙමෙයි ගොඩක් languages වල method වලට parameters pass කරද්දී variable වලට reference එක පාස් කරනේ නැනේ .
    Value එක විතරයි නේ පාස් කරන්නේ. එත් මචං මට පොඩි අවුලක් තියෙනවා. පල්ලෙහා තියෙන එක බලපන්. එකේ main method එකේදී Object කියල class එකේ instance එකක් හදලා DoThings කියල class එකට යවනවා. එතකොට යන්න ඕනේ actual instance වෙනුවට තියෙන instance එකේ copy එකක් නේ. එතකොට වෙන්න ඕනේ DoThing class එකේදී යවන instance එකේ variable එක වෙනස් (100) කරලා print කරනවා. එතකොට වෙනස් කරපු value එක ප්‍රින්ට් වෙනවා එක හරි. එත් දැන් main එකේදී තියෙන instance එකේ value එක display කරාම print වෙන්න ඕනේ 10 යි කියලා. මොකද default value එක 10 තියෙන්නේ. අනික method එකට pass කරන්නේ actual object එක නෙමෙයි. එක නිසා method එකේදී මොනවා කරත් main එකේ තියෙන object එකට අදාලා නෑනේ.

    එත වෙන්නේ අනික් පැත්ත main එකේ තියෙන object එකත් වෙනස් වෙනවා.

    එත මේක primitive data type වල නම් main එකේ තියෙන variable එක වෙනස් වෙන්නේ නෑ. ඒ කියන්නේ primitive data type වලදී යවන්නේ actual variable එක නෙමෙයි. නමුත් instance වලදී යවන්නේ actual එකද.

    මේක හෙන අවුල් බන්.


    Code:
    public class Main {
    
        public static void main(String[] args) {
            Object obj = new Object();
    
            new DoThing(obj); // send instance to dothing class
    
            obj.display(); // print current value . this must be 10
    
        }
    
    }


    Code:
    class DoThing {
    
        public DoThing(Object object) {
            
            object.setVALUE(100);    // set value to 100
            
            object.display(); // print it
        }
    }

    Code:
    class Object {
    
        private int VALUE = 10;
    
        public void display() {
            System.out.println(VALUE);
        }
    
        public void setVALUE(int a) {
            VALUE = a;
        }
    
        public int getVALUE() {
            return VALUE;
        }
    
    }
     
    Apr 8, 2014
    1,411
    35
    0
    ප්රෝග්‍රෑමරස් ක්ලබ් ත්‍රෙඩ් eke danna machan. quick response ekak hambenawa. coz eke sub wela inna programmers lata notification ekak widihata yanawa oyage q eka. :D
     
    • Like
    Reactions: DooA

    dnsuriya.pp

    Active member
  • Aug 20, 2013
    618
    69
    28
    New Zealand
    primitive data type වල නම් නෑ. instance variable වල දන්නේ නෑ

    Primitive data type wala mcn kohomath wenna be thama bn, man kiwe instance wala, java wala api salakanne hemadeyakam pointer wage kiyala( but hema deyakma pointer neme, security hinda pointer kathawa java walin ayin karala thiyenne) just like c++ wali pointers, apita copy ekak ganna one nam clone karana one
     
    • Like
    Reactions: DooA

    DooA

    Well-known member
  • Jun 22, 2011
    5,202
    773
    113
    ආගිය අතක් නැත
    Primitive data type wala mcn kohomath wenna be thama bn, man kiwe instance wala, java wala api salakanne hemadeyakam pointer wage kiyala( but hema deyakma pointer neme, security hinda pointer kathawa java walin ayin karala thiyenne) just like c++ wali pointers, apita copy ekak ganna one nam clone karana one

    ඔව් මචං instance වල තමයි අවුල තියෙන්නේ. මට මේක සෙට් උනේ observer design pattern එකේදී.
     

    dnsuriya.pp

    Active member
  • Aug 20, 2013
    618
    69
    28
    New Zealand

    ඔව් මචං instance වල තමයි අවුල තියෙන්නේ. මට මේක සෙට් උනේ observer design pattern එකේදී.

    Ubata oka value eken pass karanna one nam clone ekak gahala yawapan, clone() use karanwa nam object class eke copy constructer ekath hariyatama liyala idahan
     

    chrishanelloyd

    Well-known member
  • Oct 20, 2012
    5,680
    890
    113
    uba pass karana obj kiyanna heap eke thiyana object ekata address eka.. nathuwa object eka neme.. uba onenam nikan obj kiyana eka system.out.print ekaka gahala balahan.. address ekak enne.. oya address ekama thama uba parameters vidihata pass karanne.. ethakota argument eke thiyana "object" kiyana var ekath "obj" kiyana var ekath point karanne ekama object ekata
     

    CoolMan88

    Well-known member
  • Dec 27, 2012
    6,736
    2,780
    113
    "එතකොට යන්න ඕනේ actual instance වෙනුවට තියෙන instance එකේ copy එකක් නේ."

    meka weradi machan. methanadi pass wenne "obj" kiyana reference eka. e reference eka point wela thiyena object eke value eka doThing kiyana method eka athuledi wenas wenawa. umba main eka athuledi apahu print karanneth e "obj" reference eken point wela thiyena object ekamai. eka nisa print wenne wenas wechcha value eka.

    therune nethi thenak thiyenawanam ahapan
     
    • Like
    Reactions: DooA

    DooA

    Well-known member
  • Jun 22, 2011
    5,202
    773
    113
    ආගිය අතක් නැත
    uba pass karana obj kiyanna heap eke thiyana object ekata address eka.. nathuwa object eka neme.. uba onenam nikan obj kiyana eka system.out.print ekaka gahala balahan.. address ekak enne.. oya address ekama thama uba parameters vidihata pass karanne.. ethakota argument eke thiyana "object" kiyana var ekath "obj" kiyana var ekath point karanne ekama object ekata

    ඔව් මචං same address ඒ කියන්නේ. එකම memory location එක තමයි pass කරන්නෙත් අනේ මන්ද බං :baffled:
     

    DooA

    Well-known member
  • Jun 22, 2011
    5,202
    773
    113
    ආගිය අතක් නැත
    "එතකොට යන්න ඕනේ actual instance වෙනුවට තියෙන instance එකේ copy එකක් නේ."

    meka weradi machan. methanadi pass wenne "obj" kiyana reference eka. e reference eka point wela thiyena object eke value eka doThing kiyana method eka athuledi wenas wenawa. umba main eka athuledi apahu print karanneth e "obj" reference eken point wela thiyena object ekamai. eka nisa print wenne wenas wechcha value eka.

    therune nethi thenak thiyenawanam ahapan

    primitive data type එකක් ගත්තොත් int වගේ. එහෙම නෑනේ.

    Code:
    public class Main {
    
        public static void main(String[] args) {
    
            int a = 10;
    
            doThing(a);
    
            System.out.println("Main method value = " + a);
        }
    
        public static void doThing(int x) {
            x = 20;
            
            System.out.println("doMehod value = " + x);
        }
    
    }

    මේකෙදි original variable එක වෙනස් වෙන්නේ නෑ
     

    dj_keeper

    Active member
  • Apr 5, 2008
    471
    146
    43
    machang java wala primitive data type arenna anith hama ekakma treat karanne pointer ekak widiyata.

    eg :
    int num[] = {1, 2, 3, 4, 5}; array eka method ekakata pass karala values wenas karala balap athatama num array eke values wenas wenne.
     

    saurtech

    Well-known member
  • Feb 18, 2007
    1,811
    298
    83
    පලමු රාජධානියේ
    primitive : pass by value
    objects : pass by reference


    objects are not passed by reference in Java.
    objects are passed by reference meaning that a reference/memory address is
    passed when an object is assigned to another
    but that reference is actually passed by value. The
    reference is passed by value because a copy of
    the reference value is created and passed into
    the other object.
     

    DooA

    Well-known member
  • Jun 22, 2011
    5,202
    773
    113
    ආගිය අතක් නැත
    objects are not passed by reference in Java.
    objects are passed by reference meaning that a reference/memory address is
    passed when an object is assigned to another
    but that reference is actually passed by value. The
    reference is passed by value because a copy of
    the reference value is created and passed into
    the other object.

    තේරුනේ නෑ බන් :no:
     

    amilabanuka

    Well-known member
  • Sep 30, 2006
    7,298
    892
    113
    Thama math hoyanooo....
    objects are not passed by reference in Java.
    objects are passed by reference meaning that a reference/memory address is
    passed when an object is assigned to another
    but that reference is actually passed by value. The
    reference is passed by value because a copy of
    the reference value is created and passed into
    the other object.

    Yes. it is not the pass by reference in C/C++, that is a different thing. In java objects are passed using references and the references is passed by value. So yes Java is Pass-by-value language. but when passing an object the value of the object reference is passed. so both sender and receiver points to the same object in the memory. If the object is pass-by-value then the receiving party should have their own copy of the object. that is not the case with java. this is bit tricky.

    the pointer value of the object which lives in the heap is given to another variable. so both original variable and the new variable which got the pointer holds the reference (pointer to the memory in the heap). the object reference is passed by value. so the reference to an object which lives in the heap is passed to the method by passing the memory address of the object.

    So when considering how the receiving party got hold of the object. it receives the object via getting the location of the object as a value in the parameter. Java cannot pass the actual object (as in C/C++) as it only supports passing values. but eventually the the receiving party gets a reference (which was passed as a value) to the object (not the object itself).


    edit: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value has some good explanation.
     
    Last edited: