jls

Why is return-type covariance enforced for hidden static methods?

偶尔善良 提交于 2019-12-03 07:26:13
问题 This code won't compile because of the String return type of the staticMethod in Child . class Parent { static void staticMethod() { } } class Child extends Parent { static String staticMethod() { return null; } } I know that JLS 8 in §8.4.8.3, "Requirements in Overriding and Hiding" says: If a method declaration d1 with return type R1 overrides or hides the declaration of another method d2 with return type R2, then d1 must be return-type-substitutable (§8.4.5) for d2, or a compile-time error

Out-of-order writes for Double-checked locking

蹲街弑〆低调 提交于 2019-12-03 03:50:45
In the examples mentioned for Out-of-order writes for double-checked locking scenarios (ref: IBM article & Wikipedia Article ) I could not understand the simple reason of why Thread1 would come of out synchronized block before the constructor is fully initialized. As per my understanding, creating "new" and the calling constructor should execute in-sequence and the synchronized lock should not be release till all the work in not completed. Please let me know what I am missing here. The constructor can have completed - but that doesn't mean that all the writes involved within that constructor

Ternary operator casts integer

匿名 (未验证) 提交于 2019-12-03 02:35:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Please have a look into the below code int a =10; int b =20; System.out.println((a>b)?'a':65);//A System.out.println((a>b)?a:65);//65 System.out.println((a>b)?"a":65);//65 Can somebody explain me why it is displaying "A" if I made variable 'a' as a character? And it should display 65 if I made "a" as a string? 回答1: This behavior is documented in the JLS - 15.25. Conditional Operator ? : : If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression ( §15.28 ) of type int whose value is

Why is return-type covariance enforced for hidden static methods?

故事扮演 提交于 2019-12-02 20:59:24
This code won't compile because of the String return type of the staticMethod in Child . class Parent { static void staticMethod() { } } class Child extends Parent { static String staticMethod() { return null; } } I know that JLS 8 in §8.4.8.3, "Requirements in Overriding and Hiding" says: If a method declaration d1 with return type R1 overrides or hides the declaration of another method d2 with return type R2, then d1 must be return-type-substitutable (§8.4.5) for d2, or a compile-time error occurs. My question is what has been the motivation for this compile-time checking in the specific

How does the Java compiler choose the runtime type for a parameterized type with multiple bounds?

隐身守侯 提交于 2019-12-02 20:23:51
I would like to understand better what happens when the Java compiler encounters a call to a method like the one below. <T extends AutoCloseable & Cloneable> void printType(T... args) { System.out.println(args.getClass().getComponentType().getSimpleName()); } // printType() prints "AutoCloseable" It is clear to me that there is no type <T extends AutoCloseable & Cloneable> at runtime, so the compiler makes the least wrong thing it can do and creates an array with the type of one of the two bounding interfaces, discarding the other one. Anyway, if the order of the interfaces is switched, the

Why is x == (x = y) not the same as (x = y) == x?

淺唱寂寞╮ 提交于 2019-12-02 15:44:51
Consider the following example: class Quirky { public static void main(String[] args) { int x = 1; int y = 3; System.out.println(x == (x = y)); // false x = 1; // reset System.out.println((x = y) == x); // true } } I'm not sure if there is an item in the Java Language Specification that dictates loading the previous value of a variable for comparison with the right side ( x = y ) which, by the order implied by brackets, should be calculated first. Why does the first expression evaluate to false , but the second evaluate to true ? I would have expected (x = y) to be evaluated first, and then it

Why can't a class extend an enum?

别来无恙 提交于 2019-12-01 15:14:19
I am wondering why in the Java language a class cannot extend an enum . I'm not talking about an enum extending an enum (which can't be done, since java doesn't have multiple inheritance, and that enum s implicitly extend java.lang.Enum ), but a class that extends an enum in order to only add extra methods, not extra enumeration values. Something like: enum MyEnum { ASD(5), QWE(3), ZXC(7); private int number; private asd(int number) { this.number=number; } public int myMethod() { return this.number; } } class MyClass extends MyEnum { public int anotherMethod() { return this.myMethod()+1; } }

Which Java Errors and Exceptions may (not) be thrown by “empty statements”?

安稳与你 提交于 2019-12-01 11:23:38
Which subclass(es) of java.lang.Throwable may be thrown by an empty statement? By the phrase "an empty statement", I'm referring to the "nothing", the "semi-colon", and the "semi-colons": // .... A(); B(); C(); try { // nothing } catch (java.lang.Throwable e) { // which Throwable subclass might we see? } D(); E(); F(); try { ; // semi-colon } catch (java.lang.Throwable e) { // which Throwable subclass might we see? } G(); H(); I(); try { ; ; ;; ;;;;; ; ; ;;; ;; ;; ;; ;; ; ;; ; ;; // ... semi-colons } catch (java.lang.Throwable e) { // which Throwable subclass might we see? } J(); K(); L(); //

Restrictions on Java intersection types with interfaces classes and type variables

☆樱花仙子☆ 提交于 2019-12-01 08:35:30
Today I tried to write a class with a generic method that uses intersection types and got confused by the different error messages depending on the intersected types. Let's assume that we have an interface and a class and define generic methods in a generic interface: class ClassType { } interface InterfaceType { } interface I<T> { public <X extends InterfaceType & InterfaceType> void foo(); public <X extends ClassType & ClassType> void foo1(); public <X extends ClassType & InterfaceType> void foo2(); public <X extends InterfaceType & ClassType> void foo3(); public <X extends T & ClassType>

Java: compile-time resolution and “most specific method” as it applies to variable arity

旧街凉风 提交于 2019-12-01 08:22:40
Could someone help me understand section 15.12.2.5 of the JLS re: most specific method ? (bludgeoned cut&paste from JLS follows) In addition, one variable arity member method named m is more specific than another variable arity member method of the same name if either: One member method has n parameters and the other has k parameters, where n >= k. The types of the parameters of the first member method are T1, . . . , Tn-1 , Tn[], the types of the parameters of the other method are U1, . . . , Uk-1, Uk[]. If the second method is generic then let R1 ... Rp p1, be its formal type parameters, let