What is the output of the following Java program? Explain in terms of how parame
ID: 3764556 • Letter: W
Question
What is the output of the following Java program? Explain in terms of how parameters are passed in Java.
import java.awt.*;
class PointParameters
{
public static void main(String [] args)
{
int x = 1, y = 1;
Point p = new Point(x, y), q = new Point(x, y);
doubleScale(x, y, p, q);
System.out.println( "(x,y) = " + new Point(x, y) +
" p = " + p + " q = " + q);
}
private static void doubleScale(int x, int y, Point p, Point q)
{
x *= 2;
y *= 2;
p.x *= 2;
p.y *= 2;
q = new Point(x, y);
}
}
Suppose a similar program were written in C# in which all the parameters were ref parameters. What would the output of that program be?
Explanation / Answer
The output from doublescale does the following impact:
x =1
y = 1
p.x=2
p.y=2
q = will remain same as initiated in main function.
The values of x and Y remains unchanged siince they are immutable - constant like values.
The values in P get changed since they are modified in doublescale class, P was not created/nullfied but it was modified.
In the case of q, was totally created - that is existing relation (memory addressing) to q from main was removed and a new one is created, hence q value in main function is not effected.
IF the program is written c# - to get the same result below approach is required.
x - Byval
y - Byval
p - Byref
q - Byval
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.