1

I learned that Java's type system follows a broken subtyping rule in that it treats arrays as covariant. I've read online that if a method's argument will be read from and modified, the only type-safe option is invariance which makes sense and we can provide some simple examples of that in Java.

Is Java's patch of this rule by dynamically type checking the type of an object being stored noticeable in terms of performance? I can't imagine that it would be more than one or two additional instructions to check the type of the object. A follow up question is, ignoring any performance differences at runtime, is this equivalent to having a non-broken subtyping rule for arrays? Forgive me if my questions are elementary!

Poptart
  • 331
  • 4
  • 16
  • 1
    If you downvote, can you at least help me out by telling me what I'm doing wrong? Whether you find issue with this question, If this is a duplicate question, or if you are better at googling than I am? – Poptart May 04 '17 at 05:55
  • 1
    Probably down voted because you are not asking a code question or a specific question. Seems like this is more suited for a discussion on https://softwareengineering.stackexchange.com. – tima May 04 '17 at 07:14

2 Answers2

3

I found an article that seems to answer your question:

"Java provides three different ways to find the type of object at runtime: instanceof keyword, getClass() and isInstance() method of java.lang.Class. Out of all three only getClass() is the one which exactly find Type of object while others also return true if Type of object is the super type."

From this it seems that you should be able to write MyObject.getClass() and that will return the objects class. Or you could use MyObject.isInstance(TheObject) and this will return true if MyObject is TheObject. Thirdly you should be able to:

if (MyObject instanceof TheObject) {
//your code
}

Here is the link to the web page

Also here is a link to another post which may help clarify:

Java isInstance vs instanceOf operator

Also here are two more links to other similar questions:

How to determine an object's class (in Java)?

java - How do I check if my object is of type of a given class?

Community
  • 1
  • 1
JFreeman
  • 974
  • 1
  • 10
  • 26
  • Thank you for the response! I'm not writing any code; I'm more curious about what happens behind the scenes every time I insert an object. Is it that Java automatically calls `isInstance()` when I insert an object or do I have to do that myself? – Poptart May 04 '17 at 06:11
  • I unfortunately do not understand your question. `isInstance()` is only a function that allows you to check your object. Why would it be called automatically? – JFreeman May 04 '17 at 06:15
  • Actually not that you mention it i believe Java automatically reads through your code and verifys that you have only assigned actual existing objects. For example if i wrote `int myInt = myString` and `String myString = "hi"` it would break. – JFreeman May 04 '17 at 06:33
1

Performance is always a tricky subject. Depending on the context, such checks may be optimized out completely. For example:

public static void main(String[] args){
    Object[] array = new String[2];
    array[0] = "Hello, World!";//compiler knows this is safe
    System.out.println(array[0]);
    array[1] = new Object();//compiler knows this will throw
}

Here, the compiler has access to the actual type of the array during both assignments, so the run-time checks are not strictly necessary (if the compiler is clever enough, it can optimize them out).

In this example, however, a run-time check is necessary:

public static void main(String[] args){
    Object[] array = Math.random()<.5? new String[2]: new Object[2];
    array[0] = "Hello, World!";//compiler knows this is safe
    System.out.println(array[0]);
    array[1] = new Object();//compiler must check array type
}

Things gets even more complex when you consider the mind-bending just-in-time optimizations that can take place! Although overall, yes, as with many of Java's safety features, there is a necessary performance cost. Whether or not it's noticeable will depend on your use-case.

As for the equivalence question: No, this is not the same as having invariant arrays. Invariant arrays would make Object[] array = new String[2]; a compile-time error.

Ryan Hilbert
  • 1,805
  • 1
  • 18
  • 31