Results 1 to 7 of 7
  1. #1
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Formal and actual parameters

    In the following code:
    Java Code:
    public static void f() {
    int n = 5;
    p(n, 2 * n);
    }
    
    public static void p(int a, int b) {
    int x = 1;
    q(x, a + b);
    }
    
    public static void q(int x, int y) {
    int z = x + y;
    x = 0;
    ...
    }
    When we write x = 0; that refers to the formal parameter int x and hence it's the formal parameter that changes value. Could someone explain why isn't the value of the actual parameter also changing?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Formal and actual parameters

    Actually, I refer to them as arguments. But that's a semantic issue. In any event, Java passes arguments by value and not by name (or reference). So changing x in the example does not change the value passed from the main program.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Formal and actual parameters

    Quote Originally Posted by jim829 View Post
    Actually, I refer to them as arguments. But that's a semantic issue. In any event, Java passes arguments by value and not by name (or reference). So changing x in the example does not change the value passed from the main program.
    But why isn't x given the value 0?

  4. #4
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Formal and actual parameters

    Changing x only changes the value in that given constructor/method. The value of x from the place that you called the method from will not be affected because as Jim said, Java passes parameters by value, not by name.

    For example this code will print out 2 and then 0 because Java only changes the value inside the method, but not the actual variable that was passed into the method.

    Java Code:
    public class Main {
    
    	static int x = 0;
    	
    	public static void main(String[] args) {
    		print(x);
    		System.out.println(x);
    	}
    	
    	public static void print(int x) {
    		x = 2;
    		System.out.println(x);
    	}
    	
    }

  5. #5
    Stud1 is offline Member
    Join Date
    Jan 2014
    Posts
    44
    Rep Power
    0

    Default Re: Formal and actual parameters

    Quote Originally Posted by pj6444 View Post
    Changing x only changes the value in that given constructor/method. The value of x from the place that you called the method from will not be affected because as Jim said, Java passes parameters by value, not by name.

    For example this code will print out 2 and then 0 because Java only changes the value inside the method, but not the actual variable that was passed into the method.

    Java Code:
    public class Main {
    
    	static int x = 0;
    	
    	public static void main(String[] args) {
    		print(x);
    		System.out.println(x);
    	}
    	
    	public static void print(int x) {
    		x = 2;
    		System.out.println(x);
    	}
    	
    }
    I wonder if you could please elaborate your example. I have some questions that I need to know to understand.

    1. How many parameters are involved in a class, for example in the code example above? Are there different parameters for each method aswell as outside the methods?

    In the above code example I see that the formal parameter of method print() is given the value x. Does this method even have an actual parameter?

    In the main() method, what is printed, is that even a parameter at all?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Formal and actual parameters

    You need to check out the Oracle tutorials in my signature. Instance fields (variables) and local variables are all explained there very well. Here is a link to that section but the entire tutorial should be studied.

    Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Formal and actual parameters

    Quote Originally Posted by Stud1 View Post
    I wonder if you could please elaborate your example. I have some questions that I need to know to understand.

    1. How many parameters are involved in a class, for example in the code example above? Are there different parameters for each method aswell as outside the methods?

    In the above code example I see that the formal parameter of method print() is given the value x. Does this method even have an actual parameter?

    In the main() method, what is printed, is that even a parameter at all?
    Parameters are what is passed into a method/constructor, not regular variables. Yes that method does have an actual parameter. That parameter is an integer. In the main method, that is not a parameter. It is the static int x that I initialized at the top. You can pass parameters to the main method by running it through command prompt and passing Strings to the String[] args array.

Similar Threads

  1. formal specifications in eclipse
    By superhaNds in forum Eclipse
    Replies: 1
    Last Post: 12-18-2013, 09:09 PM
  2. Help with formal parameter, ArrayList
    By ezkatz19 in forum New To Java
    Replies: 2
    Last Post: 01-27-2012, 07:05 AM
  3. Formal introduction
    By sbliss in forum Introductions
    Replies: 3
    Last Post: 03-21-2010, 09:40 PM

Posting Permissions

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