Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Question about object passing to a method? My VOCAB sucks

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question about object passing to a method? My VOCAB sucks

    In JAVA, When an object is passed to a method, the objectís data is copied into a method parameter.

    If this is false can you tell me why?


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    50
    My Mood
    Fine
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Question about object passing to a method? My VOCAB sucks

    In java, almost every parameter is passed by values, however objects are NOT.
    They are passed by reference, so the data is not copied into a method parameter.

    For more details, check this out: Parameter passing in Java - by reference or by value?

    application context
    Last edited by daniel.j2ee; December 13th, 2011 at 04:03 PM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Question about object passing to a method? My VOCAB sucks

    Quote Originally Posted by daniel.j2ee View Post
    In java, almost every parameter is passed by values, however objects are NOT.
    They are passed by reference, so the data is not copied into a method parameter.

    For more details, check this out: Parameter passing in Java - by reference or by value?
    mmm, kind of. Technically everything is pass by value, but when you "pass an object" it's the objects address which gets passed by value.

    ex.:
    public static void doIt(int[] a)
    {
        a = new int[1];
        a[0] = 5;
    }
     
    public static void doIt2(int[] a)
    {
        a[0] = 4;
    }
     
    public static void main(String[] args)
    {
        int[] val = new int[] {3};
        doIt(val);
        System.out.println(val[0]); // prints out 3
        doIt2(val);
        System.out.println(val[0]); // prints out 4
    }

    If java did pass a true pass by reference, the first print statement would print 5 instead of 3. However, since the memory address is passed by value, doIt() creates a new array and assigns a to point to that new array, leaving the original val reference unchanged. In doIt2(), a is initialized to refer to the same object as val, so making changes to a results in changes to the same object val points to since it's the same object.

    The website you linked to points this distinction out.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about object passing to a method? My VOCAB sucks

    Quote Originally Posted by helloworld922 View Post
    mmm, kind of. Technically everything is pass by value, but when you "pass an object" it's the objects address which gets passed by value.

    ex.:
    public static void doIt(int[] a)
    {
        a = new int[1];
        a[0] = 5;
    }
     
    public static void doIt2(int[] a)
    {
        a[0] = 4;
    }
     
    public static void main(String[] args)
    {
        int[] val = new int[] {3};
        doIt(val);
        System.out.println(val[0]); // prints out 3
        doIt2(val);
        System.out.println(val[0]); // prints out 4
    }

    If java did pass a true pass by reference, the first print statement would print 5 instead of 3. However, since the memory address is passed by value, doIt() creates a new array and assigns a to point to that new array, leaving the original val reference unchanged. In doIt2(), a is initialized to refer to the same object as val, so making changes to a results in changes to the same object val points to since it's the same object.

    The website you linked to points this distinction out.
    you are talking to a 5th grader here. My vocab is that bad :<

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Question about object passing to a method? My VOCAB sucks

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Question about object passing to a method? My VOCAB sucks


  7. #7
    Junior Member
    Join Date
    Jul 2011
    Location
    PUNE/MUMBAI
    Posts
    5
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about object passing to a method? My VOCAB sucks

    Well dear tripline.....if you want to know that concept in-depth then i would recommend you Stanford University's chanel on youtube and in that you have to look for The Programming Methodologies by Mehron Sami.....All you need for that is a fast internet connection.
    Hope that help's you in understanding the concept.

    Regards,
    Ashishkumar Haldar.

Similar Threads

  1. Matcher object .find() method question
    By chronoz13 in forum Java SE APIs
    Replies: 18
    Last Post: September 8th, 2011, 11:18 AM
  2. passing object and storing
    By jack_nutt in forum What's Wrong With My Code?
    Replies: 15
    Last Post: June 30th, 2011, 09:32 PM
  3. passing a string and int array into a static method
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 8th, 2011, 05:35 PM
  4. Passing reference via object
    By Stefan_Lam in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2011, 10:57 AM
  5. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 AM