Thinking in Java Fourth Edition Bruce Eckel Chapter 2 Exercise 3

Exercise 3: Create a class containing a float and use it to demonstrate aliasing during method calls.

Solution:

class Box
{
    float a;
}

public class PassObject2
{
    static void f(Box y)
    {
        y.a = 2.71828f;
    }
    public static void main(String[] args)
    {
        Box x = new Box();
        x.a = 3.1416f;
        System.out.println("1: x.a = " + x.a);
        f(x);  
        System.out.println("2: x.a = " + x.a);
    }
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *