is java support call by reference and call by value, if
supports please explain?

Answers were Sorted based on User's Feedback



is java support call by reference and call by value, if supports please explain?..

Answer / deepa

java Supports Call by value as any C/C++ languages do.
It spports Call By Reference through objects,Since Java
Does not support Pointers.

Is This Answer Correct ?    29 Yes 9 No

is java support call by reference and call by value, if supports please explain?..

Answer / sreevani

Java doesn't pass method arguments by reference; it passes
them by value.


Take the badSwap() method for example:

public void badSwap(int var1, int var2)
{
int temp = var1;
var1 = var2;
var2 = temp;
}


When badSwap() returns, the variables passed as arguments
will still hold their original values. The method will also
fail if we change the arguments type from int to Object,
since Java passes object references by value as well.
Now, here is where it gets tricky:

public void tricky(Point arg1, Point arg2)
{
arg1.x = 100;
arg1.y = 100;

Point temp = arg1;
arg1 = arg2;
arg2 = temp;
}

public static void main(String [] args)
{
Point pnt1 = new Point(0,0);
Point pnt2 = new Point(0,0);
System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
System.out.println(" ");
tricky(pnt1,pnt2);
System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
}

If we execute this main() method, we see the following
output:

X: 0 Y: 0
X: 0 Y: 0

X: 100 Y: 100
X: 0 Y: 0

The method successfully alters the value of pnt1, even
though it is passed by value; however, a swap of pnt1 and
pnt2 fails! This is the major source of confusion. In the
main() method, pnt1 and pnt2 are nothing more than object
references. When you pass pnt1 and pnt2 to the tricky()
method, Java passes the references by value just like any
other parameter. This means the references passed to the
method are actually copies of the original references.

Java copies and passes the reference by value, not the
object. Thus, method manipulation will alter the objects,
since the references point to the original objects. But
since the references are copies, swaps will fail. As Figure
2 illustrates, the method references swap, but not the
original references. Unfortunately, after a method call,
you are left with only the unswapped original references.
For a swap to succeed outside of the method call, we need
to swap the original references, not the copies.

Is This Answer Correct ?    26 Yes 9 No

is java support call by reference and call by value, if supports please explain?..

Answer / ravikiran

java supports call by value because there is no concept of
pointers.

Is This Answer Correct ?    18 Yes 10 No

is java support call by reference and call by value, if supports please explain?..

Answer / renjit mathew

ya java support both.In java when u pass a single type to a
method it passes by value.


Call By Reference:-In java objects are passed by
reference.when you create a variable of a class type,you are
only creating a reference to an object.when you pass this
object to a method the parameter that receives it will refer
to the same object as that refered by the argument

Is This Answer Correct ?    9 Yes 10 No

is java support call by reference and call by value, if supports please explain?..

Answer / saravanan

yes java support both of these valu

Is This Answer Correct ?    3 Yes 7 No

Post New Answer

More Core Java Interview Questions

what are synchronized methods and synchronized statements? : Java thread

0 Answers  


To set the position and size of a component, which methods are used?

3 Answers  


What is jdbc api?

0 Answers  


What is the major drawback of internal iteration over external iteration?

0 Answers  


Is simpledateformat safe to use in the multithreaded program?

0 Answers  






What are namespaces in java?

0 Answers  


What is a "pure virtual" member function?

0 Answers   Amazon,


Which collection does not allow duplicates in java?

0 Answers  


there are N number of matchboxes numbered 1...N.each matchbox contain various number of stick.Two player can alternatevely pick some amount of stick from the higest stick containing box . The player is condidered win if there is no stick after his move.Find the final move so that the move player win. Note:In case the number of stick is equal ,pick the stick from the higest numbered box.

0 Answers   Manhattan,


What is qms certification?

0 Answers  


explain about method overloading and method overriding with difficult examples

4 Answers  


What is parsing and its types?

0 Answers  


Categories