Is Kotlin “pass-by-value” or “pass-by-reference”?










17














As I know Java is pass-by-value from this post. I am from Java background I wonder what Kotlin is using for passing values in between. Like in Extensions or Methods etc.










share|improve this question



















  • 6




    I think it's fair to say that everything works the same way as in Java.
    – zsmb13
    Jun 13 '17 at 7:33










  • i have read the differences between kotlin and java from various blogs, is there any kind of big internal change which needs to be covered?
    – Rahul Khurana
    Jun 13 '17 at 7:38










  • Probably not, Kotlin is compiled to bytecode in a pretty straightforward way in general.
    – zsmb13
    Jun 13 '17 at 7:40










  • Ok thanks i understand. I am curious to know why google has announced kotlin as first language for android
    – Rahul Khurana
    Jun 13 '17 at 7:42










  • @Pelocho consider converting your comment to an answer.
    – Kirill Rakhman
    Jun 13 '17 at 8:35















17














As I know Java is pass-by-value from this post. I am from Java background I wonder what Kotlin is using for passing values in between. Like in Extensions or Methods etc.










share|improve this question



















  • 6




    I think it's fair to say that everything works the same way as in Java.
    – zsmb13
    Jun 13 '17 at 7:33










  • i have read the differences between kotlin and java from various blogs, is there any kind of big internal change which needs to be covered?
    – Rahul Khurana
    Jun 13 '17 at 7:38










  • Probably not, Kotlin is compiled to bytecode in a pretty straightforward way in general.
    – zsmb13
    Jun 13 '17 at 7:40










  • Ok thanks i understand. I am curious to know why google has announced kotlin as first language for android
    – Rahul Khurana
    Jun 13 '17 at 7:42










  • @Pelocho consider converting your comment to an answer.
    – Kirill Rakhman
    Jun 13 '17 at 8:35













17












17








17


1





As I know Java is pass-by-value from this post. I am from Java background I wonder what Kotlin is using for passing values in between. Like in Extensions or Methods etc.










share|improve this question















As I know Java is pass-by-value from this post. I am from Java background I wonder what Kotlin is using for passing values in between. Like in Extensions or Methods etc.







kotlin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 20 at 12:28

























asked Jun 13 '17 at 7:32









Rahul Khurana

3,00141641




3,00141641







  • 6




    I think it's fair to say that everything works the same way as in Java.
    – zsmb13
    Jun 13 '17 at 7:33










  • i have read the differences between kotlin and java from various blogs, is there any kind of big internal change which needs to be covered?
    – Rahul Khurana
    Jun 13 '17 at 7:38










  • Probably not, Kotlin is compiled to bytecode in a pretty straightforward way in general.
    – zsmb13
    Jun 13 '17 at 7:40










  • Ok thanks i understand. I am curious to know why google has announced kotlin as first language for android
    – Rahul Khurana
    Jun 13 '17 at 7:42










  • @Pelocho consider converting your comment to an answer.
    – Kirill Rakhman
    Jun 13 '17 at 8:35












  • 6




    I think it's fair to say that everything works the same way as in Java.
    – zsmb13
    Jun 13 '17 at 7:33










  • i have read the differences between kotlin and java from various blogs, is there any kind of big internal change which needs to be covered?
    – Rahul Khurana
    Jun 13 '17 at 7:38










  • Probably not, Kotlin is compiled to bytecode in a pretty straightforward way in general.
    – zsmb13
    Jun 13 '17 at 7:40










  • Ok thanks i understand. I am curious to know why google has announced kotlin as first language for android
    – Rahul Khurana
    Jun 13 '17 at 7:42










  • @Pelocho consider converting your comment to an answer.
    – Kirill Rakhman
    Jun 13 '17 at 8:35







6




6




I think it's fair to say that everything works the same way as in Java.
– zsmb13
Jun 13 '17 at 7:33




I think it's fair to say that everything works the same way as in Java.
– zsmb13
Jun 13 '17 at 7:33












i have read the differences between kotlin and java from various blogs, is there any kind of big internal change which needs to be covered?
– Rahul Khurana
Jun 13 '17 at 7:38




i have read the differences between kotlin and java from various blogs, is there any kind of big internal change which needs to be covered?
– Rahul Khurana
Jun 13 '17 at 7:38












Probably not, Kotlin is compiled to bytecode in a pretty straightforward way in general.
– zsmb13
Jun 13 '17 at 7:40




Probably not, Kotlin is compiled to bytecode in a pretty straightforward way in general.
– zsmb13
Jun 13 '17 at 7:40












Ok thanks i understand. I am curious to know why google has announced kotlin as first language for android
– Rahul Khurana
Jun 13 '17 at 7:42




Ok thanks i understand. I am curious to know why google has announced kotlin as first language for android
– Rahul Khurana
Jun 13 '17 at 7:42












@Pelocho consider converting your comment to an answer.
– Kirill Rakhman
Jun 13 '17 at 8:35




@Pelocho consider converting your comment to an answer.
– Kirill Rakhman
Jun 13 '17 at 8:35












3 Answers
3






active

oldest

votes


















18














Every time I hear about the "pass-by-value" vs "pass-by-reference" Java debate I always think the same. The answer I give: "Java passes a copy (pass-by-value) of the reference (pass-by-reference)". So everyone is happy. I would say Kotlin does the same as is JVM based






share|improve this answer




























    11














    The semantics is identical to Java.



    In Java, when you have an instance of an object, and you pass it to a method, that method can change the state of that object, and when the method is done, the changes would have been applied to the object at the call site.



    The same applies in Kotlin.






    share|improve this answer




























      0














      In Java primitive types like int, float, double, boolean are passed to a method by value, if you modify them inside the receiver method they doesn't change into the calling method. But if the property/variable type isn't a primitive, like arrays of primitives or other classes when they are changed inside the method that receive them as parameter they also change in the caller method.
      But with Kotlin nothing seems to be primitive, so I think all is passed by reference.






      share|improve this answer




















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44515031%2fis-kotlin-pass-by-value-or-pass-by-reference%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        18














        Every time I hear about the "pass-by-value" vs "pass-by-reference" Java debate I always think the same. The answer I give: "Java passes a copy (pass-by-value) of the reference (pass-by-reference)". So everyone is happy. I would say Kotlin does the same as is JVM based






        share|improve this answer

























          18














          Every time I hear about the "pass-by-value" vs "pass-by-reference" Java debate I always think the same. The answer I give: "Java passes a copy (pass-by-value) of the reference (pass-by-reference)". So everyone is happy. I would say Kotlin does the same as is JVM based






          share|improve this answer























            18












            18








            18






            Every time I hear about the "pass-by-value" vs "pass-by-reference" Java debate I always think the same. The answer I give: "Java passes a copy (pass-by-value) of the reference (pass-by-reference)". So everyone is happy. I would say Kotlin does the same as is JVM based






            share|improve this answer












            Every time I hear about the "pass-by-value" vs "pass-by-reference" Java debate I always think the same. The answer I give: "Java passes a copy (pass-by-value) of the reference (pass-by-reference)". So everyone is happy. I would say Kotlin does the same as is JVM based







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 13 '17 at 8:41









            Pelocho

            4,09821332




            4,09821332























                11














                The semantics is identical to Java.



                In Java, when you have an instance of an object, and you pass it to a method, that method can change the state of that object, and when the method is done, the changes would have been applied to the object at the call site.



                The same applies in Kotlin.






                share|improve this answer

























                  11














                  The semantics is identical to Java.



                  In Java, when you have an instance of an object, and you pass it to a method, that method can change the state of that object, and when the method is done, the changes would have been applied to the object at the call site.



                  The same applies in Kotlin.






                  share|improve this answer























                    11












                    11








                    11






                    The semantics is identical to Java.



                    In Java, when you have an instance of an object, and you pass it to a method, that method can change the state of that object, and when the method is done, the changes would have been applied to the object at the call site.



                    The same applies in Kotlin.






                    share|improve this answer












                    The semantics is identical to Java.



                    In Java, when you have an instance of an object, and you pass it to a method, that method can change the state of that object, and when the method is done, the changes would have been applied to the object at the call site.



                    The same applies in Kotlin.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jun 13 '17 at 10:29









                    hasen

                    81.7k57163211




                    81.7k57163211





















                        0














                        In Java primitive types like int, float, double, boolean are passed to a method by value, if you modify them inside the receiver method they doesn't change into the calling method. But if the property/variable type isn't a primitive, like arrays of primitives or other classes when they are changed inside the method that receive them as parameter they also change in the caller method.
                        But with Kotlin nothing seems to be primitive, so I think all is passed by reference.






                        share|improve this answer

























                          0














                          In Java primitive types like int, float, double, boolean are passed to a method by value, if you modify them inside the receiver method they doesn't change into the calling method. But if the property/variable type isn't a primitive, like arrays of primitives or other classes when they are changed inside the method that receive them as parameter they also change in the caller method.
                          But with Kotlin nothing seems to be primitive, so I think all is passed by reference.






                          share|improve this answer























                            0












                            0








                            0






                            In Java primitive types like int, float, double, boolean are passed to a method by value, if you modify them inside the receiver method they doesn't change into the calling method. But if the property/variable type isn't a primitive, like arrays of primitives or other classes when they are changed inside the method that receive them as parameter they also change in the caller method.
                            But with Kotlin nothing seems to be primitive, so I think all is passed by reference.






                            share|improve this answer












                            In Java primitive types like int, float, double, boolean are passed to a method by value, if you modify them inside the receiver method they doesn't change into the calling method. But if the property/variable type isn't a primitive, like arrays of primitives or other classes when they are changed inside the method that receive them as parameter they also change in the caller method.
                            But with Kotlin nothing seems to be primitive, so I think all is passed by reference.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 at 3:58









                            user2604769

                            1




                            1



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44515031%2fis-kotlin-pass-by-value-or-pass-by-reference%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                Barbados

                                How to read a connectionString WITH PROVIDER in .NET Core?

                                Node.js Script on GitHub Pages or Amazon S3