Results 1 to 13 of 13

Thread: Arguments

  1. #1
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default Arguments

    Hi all,

    Am new to java. I wanted to know why do we need to pass arrays as arguments or simply arguments to the method?? I mean what is the purpose of that?

    Thank you.

  2. #2
    jayachandra is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Smile

    For Ex: public void m1(int a , int b){
    }

    as shown above m1 method is there. Suppose if you want to do any operation inside the method (for ex: add, mul, sub ) then you need to pass a values into a variable a and b . Then you can do what ever openration you want. Then you can use the same method out side the class whne ever you required just by importing that class.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by maya700 View Post
    Hi all,

    Am new to java. I wanted to know why do we need to pass arrays as arguments or simply arguments to the method?? I mean what is the purpose of that?

    Thank you.
    I don't understand your question; can you please elaborate a bit? Things are passed as parameters/arguments to make them visible to the other method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    I have a feeling you are confused because you see an item declared in some class, for example:
    Java Code:
    public class Test{
        int a;
        
        public void SomeMethod(){
            someOtherMethod(a);
        }
    
        public void SomeOtherMethod(int num){
             //do something with num (a)
        }
    In this case, you might be asking: "Why pass 'a' as a parameter?". Well, this is only one case. What if some method in some other class wanted to use 'SomeOtherMethod()'. It would not have access to 'a' and therefor for 'SomeOtherMethod()' to work, you would need to supply a value as a parameter.

    In the case of an array, passing an array as a parameter might allow you to pass millions of items into a method instead of passing them in one at a time.

  5. #5
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default

    so u mean to say that we pass parameters to methods so that it can be used by method that is present in a different class?

    Thanks alll.

  6. #6
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default

    And also when Mr. JosAH says that "Things are passed as parameters/arguments to make them visible to the other method" then why can we make the same method public? why complicate and confuse the whole thing by passing parameters??

    Sorry my doubt might sound stupid.

    Thanks all for ur replies.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by maya700 View Post
    And also when Mr. JosAH says that "Things are passed as parameters/arguments to make them visible to the other method" then why can we make the same method public? why complicate and confuse the whole thing by passing parameters??

    Sorry my doubt might sound stupid.

    Thanks all for ur replies.
    If a method isn't public it can't be used by anything else in the world; that has nothing to do with the parameters of the method. Try to write methods without parameters and see for yourself. You'll hit a brick wall before you know it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    syst3m.tr0jan is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    actually we are not obliged to do passing of variables to functions.. we use functions for readability purposes of our codes. and since we use functions, and need variables which are already declared in the main of the class, we pass them these variables as parameters of the function.

    but if you can trace your code from "cover to cover", well there is no need to create functions.. but then functions are very useful in understanding a multi-threaded application development..

    cheers..

  9. #9
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default Thx

    Thanks once again. Will try and get back.

    Thank you all.

  10. #10
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default Hii

    Quote Originally Posted by JosAH View Post
    If a method isn't public it can't be used by anything else in the world; that has nothing to do with the parameters of the method. Try to write methods without parameters and see for yourself. You'll hit a brick wall before you know it.

    kind regards,

    Jos
    so you mean to say that even if i make a method private, just by passing parameters to that method i may be able to access it from outside?? also i typed a simple code with parameters and without parameters. Here is the code below

    //With parameters

    class Arrayi

    {

    private void init(String[] s)

    {

    s[0]="maya";

    s[1]="is";

    s[2]="good";

    }

    private void disp(String[] g)

    {

    System.out.println(g[1]);

    System.out.println(g[0]);

    System.out.println(g[2]);

    }

    }

    public class Tearra

    {

    public static void main(String[] args)

    {

    Arrayi a= new Arrayi();

    String[] f = new String[3];

    a.init(f);
    a.disp(f);

    }

    }
    ***which throws an error saying init and disp has private access but when i make same method publics its working

    //without parameter

    class Arram

    {

    String[] s=new String[2];

    public void init()

    {

    //s=String[2];

    s[0]="maya";

    s[1]="good";

    disp();

    }

    public void disp()

    {

    System.out.println(s[0]);

    System.out.println(s[1]);

    }

    }
    public class Tearram

    {

    public static void main(String[] args)

    {

    Arram a = new Arram();

    a.init();
    }

    }
    so in this code if i make this two methods private, its gonna throw same error like the above one. so whats the big deal in providing the parameters??

    Sorry for trouble guys.

    Thanks once again for all the pain.

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    so you mean to say that even if i make a method private, just by passing parameters to that method i may be able to access it from outside??
    No.
    You would experience something called method Overloading. This feature of java allows multiple methods (public or private) to share the same name, but be differentiated by the number and type of parameters. For example:
    Java Code:
    public class SomeClass{
    //...
    private void print(){
        System.out.println("Hello, World!);
    }
    
    public void print(String pluralNoun){
        System.out.println("I like "+pluralNoun);
    }
    //...
    If from outside this class you were to call these methods, you might have something like this:
    Java Code:
    //...
    SomeClass a = new SomeClass();
    a.print();  //this would fail, print() is not public in SomeClass
    a.print("Grapes"); //this would work, print with a string parameter is public
    a.print("Grapes", "Dogs"); //this would fail. there is no public method that takes two strings
    a.print(5); //This would fail, there is no method that prints an integer value
    See the difference?

  12. #12
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    P.S. Also, wrap your code in [code][/code] tags so we can read it! :D

  13. #13
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 09-23-2010, 01:07 AM
  2. Default Arguments
    By Mark Robert in forum New To Java
    Replies: 6
    Last Post: 08-17-2010, 03:56 PM
  3. Passign arguments
    By ninjalord918 in forum AWT / Swing
    Replies: 4
    Last Post: 08-04-2010, 12:11 AM
  4. passing arguments
    By mac in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 12:30 AM
  5. repetition of 'arguments'(?)
    By Igor in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 11:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •