2

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!

2
  • 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? Commented May 4, 2017 at 5: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 softwareengineering.stackexchange.com. Commented May 4, 2017 at 7:14

2 Answers 2

4

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?

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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?
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.