Tuesday, October 2, 2007

Is java pass by value or reference?

Java is always pass-by-value.

Consider the example(passing primitives)
int x = 0;
passingPrimitives(x);
System.out.println(x);
............
............

void passingPrimitives(int y)
{
y = 10;
}


After the method call the value of x is 0 and NOT 10. So primitives are passed by value

Another example (passing objects)

Object x=null;
passingObjects(x);
System.out.println(x);
............................
...........................

void passingObjects(Object y)
{
y = "Am i now Y?"
}

The value of x after the method call is null. A clear indication of objects are passed by value too in java.

First Post

Well....Here is my first post. The blog will contain some fundamental FAQ's on java and J2EE stack. I will also post "wiered" issues that i have had to face at my work place.