Java Language Specification: Meaning of Conventions | S |

I go through JLS 7 to understand the type cast section 5.5.1 .

It says: given the reference compilation time type S (source) and the compilation time reference type T (target), a cast conversion exists from S to T if compile-time errors do not occur due to the following rules. If S is a class type:

  • If T is a class type, then either | S | <: | T | or | T | <: | S | . Otherwise, a compilation time error occurs.

They made it clear that S and T are two types in section 4.10 , then

  • S:> T indicates that S is a supertype of T
  • S> T indicates that S is a proper super type of T, from which it follows S:> T and S! = T.

I can not find the meaning | S | . Please help me understand what this means | S | ? Does this mean the number and types of properties or something else. I tried to find it in JLS , but could not find it. Thanks in advance.

+6
source share
1 answer

I cannot provide a better or less formal explanation of what the document is for type erasure. In your case (Class casting) "If T is a class type, then either | S | <: | T | or | T | <: | S |. Otherwise, a compile-time error occurs." means that after the type is erased, the cast class is legal if the arguments of the general type are in the "subclass class relation". A simple example:

static class Bar {} static class FooBar extends Bar {} public static void main(String[] args) { List<FooBar> foobarList = (List<FooBar>) newList(Bar.class); List<Bar> barList = (List<Bar>) newList(FooBar.class); System.out.println("No cast class exception :)"); } private static<T> List<?> newList(Class<T> clazz) { return new ArrayList<T>(); } 
+2
source

Source: https://habr.com/ru/post/947355/


All Articles