Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java dynamic, static casting

import javax.swing.*;

public class Toast {
    static Object[] objects = { new JButton(),
        new String("Example"), new Object() };

    public static void main(String[] args) {
        System.out.println( new Count(objects) );
        for (Object o : objects)
            System.out.println(o);
    }
}


class Count {
    int b, s, o;
    public Count(Object[] objects) {
        for (int i=0; i<objects.length; i++)
            count(objects[i]);
    }
    public void count(JButton x) { b++; }
    public void count(String x) { s++; }
    public void count(Object x) { o++; }
    public String toString() {
        return b + " : " + s + " : " + o + "\n";
    }
}

Above is a piece of code that appears in some form or the other in past exam papers for one of my upcoming tests. The idea of the question is to gauge if you fully understand polymorphism, dynamic and static casting. Basic ideas of OO.

I would like to put out what I think is correct and if people would be able to correct me or add points that would be greatly appreciated.

From what I can see in the above code:

  • Items are upcast to objects in the object array as every class in java technically inherits from the object class. This is why when count is run it will say there are 3 objects not 1 Jbutton, 1 string and 1 object.

  • When the enhanced for loop is run the toString of that object type e.g. Example from the string and memory address of the object (not sure what the JButton will print). As this is done at runtime this is known as dynamic casting.

I cannot see any other points that would be relevant to the above bit of code.

like image 858
Softey Avatar asked Jan 18 '26 06:01

Softey


2 Answers

The idea behind static cast and dynamic cast is related to the moment a type decision needs to be made. If it needs to be made by the compiler then it's static cast. If the compiler postpones the decision to runtime then it's dynamic cast.

So, your first observation is incorrect. The upcast does not explain the count. Objects do not loose they type but the compiler needs to perform a static cast to decide which method to invoke and it chooses count(Object). There is no dynamic dispatch in java which means that the method called is always decided at compile time.

You second observation is also incorrect. What is in use is polymorphism. In Java, methods are always invoked for the type of the instance and not for the type in the code. Also, there is no dynamic casting here. The compiler can verify all the types. It's just that method invocation is always virtual but that's not a cast.

Actually in this example, I don't see a single case of dynamic casting. The compiler can verify all types. You normally only see dynamic casting when down casting and there is no case of that.

like image 93
m4ktub Avatar answered Jan 19 '26 20:01

m4ktub


Here's what I would take away:

  1. The compiler implicitly upcasts when performing assignments. This includes assigning to array elements during initialization.

  2. The compiler and JVM do not implicitly downcast when selecting method overloads. The static type of the objects array is Object[], so the count(Object) method will always be called.

  3. The JVM does implicitly "downcast" (in a sense) when invoking a virtual method. The println loop will always invoke the toString method of the actual object instance rather than always invoking Object.toString.

like image 35
Brett Kail Avatar answered Jan 19 '26 19:01

Brett Kail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!