View RSS Feed

Java Object

Passing Primitive Data Type Arguments

Rate this Entry
by , 04-26-2012 at 05:21 PM (2124 Views)
Primitive arguments for example, a double or an int, get passed by value into methods. This shows that changes to parameter values are present just in the methodís scope. When methods are returned it means that parameters have been gone & all alterations have been lost. For example:
Java Code:
public class PassPrimitiveByValue {

    public static void main(String[] args) {
           
        int x = 3;
           
        // invoke passMethod() with 
        // x as argument
        passMethod(x);
           
        // print x to see if its 
        // value has changed
        System.out.println("After invoking passMethod, x = " + x);
           
    }
        
    // change parameter in passMethod()
    public static void passMethod(int p) {
        p = 10;
    }
}
Output will be as following when this program is being run:

After invoking passMethod, x = 3

Submit "Passing Primitive Data Type Arguments" to Facebook Submit "Passing Primitive Data Type Arguments" to Digg Submit "Passing Primitive Data Type Arguments" to del.icio.us Submit "Passing Primitive Data Type Arguments" to StumbleUpon Submit "Passing Primitive Data Type Arguments" to Google

Comments