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 20 of 20

Thread: passing arguments

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default passing arguments

    Hello,
    below is my code
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package myproject;
     
    /**
     *
     * @author User
     */
    public class Helloworld {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
    {
        int[] a = new int[5];
        int[] b = new int[5];
        a=doIt(a);
        f(b);
     
        System.out.println("ARRAY a");
        for(int i = 0; i < a.length; ++i)
            System.out.println(a[i]);
     
        System.out.println("ARRAY b");
        for(int i = 0; i < b.length; ++i)
            System.out.println(b[i]);
    }
     
    public static int[] doIt(int[] var)
    {
        var = new int[15];
        for(int i = 0; i < var.length; i++)
        {
            var[i] = i;
        }
        return var;
    }
     
    public static void f(int[] var)
    {
        var = new int[15];
        for(int i = 0; i < var.length; i++)
        {
            var[i] = i;
        }
    }
     
    }
    and the output

    ARRAY a
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    ARRAY b
    0
    0
    0
    0
    0

    This means that function f does not modify array b at all.Am i right?I need a reference to array b in order to have function f does the same job as function doIt,right?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: passing arguments

    Quote Originally Posted by Samaras View Post
    Hello,
    below is my code
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package myproject;
     
    /**
     *
     * @author User
     */
    public class Helloworld {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
    {
        int[] a = new int[5];
        int[] b = new int[5];
        a=doIt(a);
        f(b);
     
        System.out.println("ARRAY a");
        for(int i = 0; i < a.length; ++i)
            System.out.println(a[i]);
     
        System.out.println("ARRAY b");
        for(int i = 0; i < b.length; ++i)
            System.out.println(b[i]);
    }
     
    public static int[] doIt(int[] var)
    {
        var = new int[15];
        for(int i = 0; i < var.length; i++)
        {
            var[i] = i;
        }
        return var;
    }
     
    public static void f(int[] var)
    {
        var = new int[15];
        for(int i = 0; i < var.length; i++)
        {
            var[i] = i;
        }
    }
     
    }
    and the output

    ARRAY a
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    ARRAY b
    0
    0
    0
    0
    0

    This means that function f does not modify array b at all.Am i right?I need a reference to array b in order to have function f does the same job as function doIt,right?
    Correct. However, you have not compared apples to apples.

    Function doit(int[] var) does not modify array a either. Your difference lies in your calls.
        a=doIt(a);
        f(b);
    The first line, your variable a is going to be assigned the return value of function doit. Passing the variable a into the function was a useless effort. The first thing you did in your function was ignore that array and create a new one in var. var is then modified to the i values and sent back to be stored in a.
    The second line sends in b to function f, and again is immediately discarded. var is assigned a new array, modifies it, and then discards it as well, in a sense having done nothing.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: passing arguments

    ..an addon to my previous post. When you passed a to doit, var your original array with five zeros in it becomes lost. So it is important to realize you did not modify a in the sense that the loop in doit modified a. a was changed by storing the return value of the method call.

    Just so nothing is left out, you get the zeros because int arrays are defaulted to hold zeros when defined, (when you actually call new).

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: passing arguments

    Quote Originally Posted by jps View Post
    The second line sends in b to function f, and again is immediately discarded. var is assigned a new array, modifies it, and then discards it as well, in a sense having done nothing.
    I have this in my mind.Tell me if it is wrong,please.The array b and array var are equivalent at the body of f.
    But when i write var = new int[15]; then the system allocates some new memory cells and assigns them to array var.
    If i remove this line,then everything is as expected.Why if i remove that line var is going to update b and then be discarded?

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: passing arguments

    Quote Originally Posted by jps View Post
    ..an addon to my previous post. When you passed a to doit, var your original array with five zeros in it becomes lost. So it is important to realize you did not modify a in the sense that the loop in doit modified a. a was changed by storing the return value of the method call.

    Just so nothing is left out, you get the zeros because int arrays are defaulted to hold zeros when defined, (when you actually call new).
    I thought of it too.agree

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: passing arguments

    My advice is to comment the line in question, run it, and see what happens. Post back if you have questions after a little trial and discovery.

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: passing arguments

    Quote Originally Posted by jps View Post
    My advice is to comment the line in question, run it, and see what happens. Post back if you have questions after a little trial and discovery.
    What line are you refering to?

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: passing arguments

    Quote Originally Posted by Samaras View Post
    I have this in my mind.Tell me if it is wrong,please.The array b and array var are equivalent at the body of f.
    But when i write var = new int[15]; then the system allocates some new memory cells and assigns them to array var.
    If i remove this line,then everything is as expected.Why if i remove that line var is going to update b and then be discarded?
    Quote Originally Posted by jps View Post
    ..an addon to my previous post. When you passed a to doit, var your original array with five zeros in it becomes lost. So it is important to realize you did not modify a in the sense that the loop in doit modified a. a was changed by storing the return value of the method call.

    Just so nothing is left out, you get the zeros because int arrays are defaulted to hold zeros when defined, (when you actually call new).
    Don't just try in one method, comment the lines with new in both doit and f, and run it.

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: passing arguments

    Quote Originally Posted by Samaras View Post
    What line are you refering to?
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package myproject;
     
    /**
     *
     * @author User
     */
    public class Helloworld {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)
    {
        int[] a = new int[5];
        int[] b = new int[5];
        a=doIt(a);
        f(b);
     
        System.out.println("ARRAY a");
        for(int i = 0; i < a.length; ++i)
            System.out.println(a[i]);
     
        System.out.println("ARRAY b");
        for(int i = 0; i < b.length; ++i)
            System.out.println(b[i]);
    }
     
    public static int[] doIt(int[] var)
    {
        var = new int[15];//This line
        for(int i = 0; i < var.length; i++)
        {
            var[i] = i;
        }
        return var;
    }
     
    public static void f(int[] var)
    {
        var = new int[15];//and this line
        for(int i = 0; i < var.length; i++)
        {
            var[i] = i;
        }
    }
     
    }

  10. #10
    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: passing arguments

    The issue has to deal with how Java passes parameters. In Java, all values are pass by value. When you pass a primitive type (int, char, boolean, etc.), their direct value is passed.

    For example:

    public static void method1(int var)
    {
        var = 3;
    }
     
    public static void main(String[] args)
    {
        int a = 1;
        method1(a);
    }

    As expected, a's value is always 1, even though method1 "appears" to override it's value with 3.

    This can be explained by examining a simplification of the memory model.

    The variable a is stored in a certain memory location, for example address 1000. At this address the value 1 is written. When method1 gets called, a new variable var is created at a different memory location, say address 2000. The value stored at 1000 (1) is copied to address 2000. Now any modifications made to the variable var will effect address 2000, but all changes will not be copied or made apparent at address 1000, which is where the variable a resides.

    Now what happens when you pass an array or an object? Again, the parameters are passed by value. However, this time the value is the address of the object. An analogy is this:

    Say you have a variable john. The variable has a piece of paper which points to the address of jane's house. The house is the physical object (whether it be an array, object, etc.).

    Now say you call a method which has a parameter steve. When the method is called, john gives steve a copy of jane's house. Now steve can go to jane's house and make any modifications, such as paint the walls blue. Now when john goes to the same house he'll find a house with blue walls.

    However, suppose that we have another method in which john gives jane's address to scott. In this method, scott decides he wants to build a new house and paint that house's walls yellow. He can't build a house at jane's address because there's already a house there, so he needs a new address. After painting the new house's walls yellow, john goes to visit jane's house. Jane's house obviously doesn't have yellow walls because scott never painted jane's house.

  11. #11
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: passing arguments

    ok,i 'll do so and i will post back tomorrow if i have problems(that is the chance though )

  12. #12
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: passing arguments

    Ok everything is clear now.However a question that just arrived is this : Can i pass an argument as a reference?
    exampe
    public static void bla(int c)
    {
             c=45;
    }
    public static void main(String[] args)
    {
         int c=4;
         bla(c);
    }
    Actually no effect is done by function bla.As @helloworld922 stated the variable is passed by value.All this are very clear to me because i know C/C++ quite well.So i tried to pass it as a reference like in C/C++ but the compiler won't let me do it.So how can i have the same results in JAVA as the results that i would have if passed as a ref in C/C++?In other words how can i make function bla set c to 45?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: passing arguments

    how can i make function bla set c to 45?
    c=bla();
    ...
    int bla() { return 45;}
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: passing arguments

    Well that's ok for one variable.For more variables?For three for example?Without using an array.Is that possible?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: passing arguments

    A method can only return one variable/value.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: passing arguments

    Quote Originally Posted by Norm View Post
    A method can only return one variable/value.
    I agree.But in C for example you can write
    /*C code*/
    int f(int* a,int* b)
    { 
          a=1; b=5;
    }
     
    int main(void)
    {
        int a=0;
        int b=9;
        f(&a,&b);
        printf("a= %d ,b= %d\n",a,b);
    }

    and the output would be

    a=1 b=5

    So can i get the same job done in JAVA without(of course) using an array?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: passing arguments

    This has already been discussed. Java passes by value.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: passing arguments

    Quote Originally Posted by Norm View Post
    This has already been discussed. Java passes by value.
    Yes this was stated,but i thought we could make some adjustments.The answer is no..Thank you for your patience

  19. #19
    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: passing arguments

    In short, no. There are "cheater" methods, though which achieve some of what passing as a pointer offers. The simplest is to pass single element arrays as a pseudo-pointer. However, this method really isn't that great. The best solution is to design your programs to not use this and focus on object-oriented design philosophies.

    // workable, but not really recommended pseudo-pointer
    public static void method1(int[] a, int[] b)
    {
        a[0] = 5;
        b[0] = 3;
    }
     
    public static void main(String[] args)
    {
        int[] var1 = new int[1];
        int[] var2 = new int[1];
        method1(var1, var2);
        System.out.println(var1[0] + " " + var2[0]);
    }

  20. #20
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: passing arguments

    I got the point.Thank you very much for your answers

Similar Threads

  1. Method is not applicable for the arguments
    By jbodary in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 23rd, 2012, 07:05 AM
  2. execute bat file with arguments
    By kafka82 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 6th, 2011, 01:20 PM
  3. Tiny problem with arguments.
    By Melawe in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 15th, 2011, 02:26 PM
  4. Problem with inheritence? arguments not applicable
    By rbk in forum Object Oriented Programming
    Replies: 2
    Last Post: March 29th, 2011, 09:37 AM
  5. command line arguments
    By rizla in forum Member Introductions
    Replies: 3
    Last Post: December 12th, 2010, 11:14 PM