Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Why doesn't the method swap1 swap the radiuses even though the reference of the objects were passed? If it doesn't work out why is swap1
Why doesn't the method swap1 swap the radiuses even though the reference of the objects were passed? If it doesn't work out why is swap1 not a compile error?
public class Test { public static void main(String[] args) { Circle circle1 = new Circle(1); Circle circle2 = new Circle(2); swap1(circle1, circle2); System.out.println("After swap1: circle1 = " + circle1.radius + " circle2 = " + circle2.radius); swap2(circle1, circle2); System.out.println("After swap2: circle1 = " + circle1.radius + " circle2 = " + circle2.radius); } public static void swap1(Circle x, Circle y) { Circle temp = x; x = y; y = temp; } public static void swap2(Circle x, Circle y) { double temp = x.radius; x.radius = y.radius; y.radius = temp; } } class Circle { double radius; Circle(double newRadius) { radius = newRadius; } }
After swap1: circle1 = 1.0 circle2 = 2.0 After swap2: circle1 = 2.0 circle2 = 1.0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started