jls

Java casting: is the compiler wrong, or is the language spec wrong, or am I wrong?

这一生的挚爱 提交于 2019-11-30 06:54:37
问题 I have been reading the Java Language Spec, 3rd edition, and have found what I think is a discrepancy between the spec and the javac compiler implementation. The same discrepancies exist in the Eclipse compiler. Section 15.16 talks about cast expressions. It says that it should be a compile time error if the argument type cannot be converted to the cast type via casting conversion (section 5.5): It is a compile-time error if the compile-time type of the operand may never be cast to the type

Java 8 Consumer/Function Lambda Ambiguity

大兔子大兔子 提交于 2019-11-30 06:33:21
I have an overloaded method that takes a Consumer and a Function object respectively and returns a generic type that matches the corresponding Consumer/Function. I thought this would be fine, but when I try to call either method with a lambda expression I get an error indicating the reference to the method is ambiguous. Based on my reading of JLS §15.12.2.1. Identify Potentially Applicable Methods: it seems like the compiler should know that my lambda with a void block matches the Consumer method and my lambda with a return type matches the Function method. I put together the following sample

Is “public static final” redundant for a constant in a Java interface?

坚强是说给别人听的谎言 提交于 2019-11-30 05:36:25
This code: interface Config { int MAX_CONN = 20; } compiled and worked as I expected. It looks like this is the same as: interface Config { public static final int MAX_CONN = 20; } Is "public static final" redundant for a constant in a Java interface? Is this true for Java 1.1, 1.2, 1.3, 1.4,..., 1.8 or did it change in a Java release? NINCOMPOOP Variables declared in Interface are implicitly public static final . This is what JLS 9.3 says : Every field declaration in the body of an interface is implicitly public, static, and final . It is permitted to redundantly specify any or all of these

changing final variables through reflection, why difference between static and non-static final variable

ⅰ亾dé卋堺 提交于 2019-11-29 14:28:28
问题 Please refer to the below code. When I run the code, I am able to change the value of a final non-static variable. But if I try to change the value of a final static variable then it throws java.lang.IllegalAccessException . My question is why doesn't it throw an exception in case of non-static final variable also or vice versa. Why the difference? import java.lang.reflect.Field; import java.util.Random; public class FinalReflection { final static int stmark = computeRandom(); final int

Java: overloaded method resolution and varargs — confusing example

ⅰ亾dé卋堺 提交于 2019-11-29 12:15:49
问题 Just when I thought I understood JLS15.12 as it applied to varargs, here's this example: package com.example.test.reflect; public class MethodResolutionTest2 { public int compute(Object obj1, Object obj2) { return 42; } public int compute(String s, Object... objects) { return 43; } public static void main(String[] args) { MethodResolutionTest2 mrt2 = new MethodResolutionTest2(); System.out.println(mrt2.compute("hi", mrt2)); System.out.println(mrt2.compute("hi", new Object[]{mrt2})); System

Execution order of of static blocks in an Enum type w.r.t to constructor

ぃ、小莉子 提交于 2019-11-29 09:26:28
This is from Effective Java : // Implementing a fromString method on an enum type private static final Map<String, Operation> stringToEnum = new HashMap<String, Operation>(); static { // Initialize map from constant name to enum constant for (Operation op : values()) stringToEnum.put(op.toString(), op); } // Returns Operation for string, or null if string is invalid public static Operation fromString(String symbol) { return stringToEnum.get(symbol); } Note that the Operation constants are put into the stringToEnum map from a static block that runs after the constants have been created. Trying

Java 8 Consumer/Function Lambda Ambiguity

北城以北 提交于 2019-11-29 05:19:16
问题 I have an overloaded method that takes a Consumer and a Function object respectively and returns a generic type that matches the corresponding Consumer/Function. I thought this would be fine, but when I try to call either method with a lambda expression I get an error indicating the reference to the method is ambiguous. Based on my reading of JLS §15.12.2.1. Identify Potentially Applicable Methods: it seems like the compiler should know that my lambda with a void block matches the Consumer

Java in operator

无人久伴 提交于 2019-11-28 20:11:54
For the one millionth time, I would have liked to use an IN operator in Java, similar to the IN operator in SQL. It could just be implemented as compiler syntactic sugar. So this if (value in (a, b, c)) { } else if (value in (d, e)) { } ...would really be awesome. In fact, the above is the same as the rather verbose (and not adapted for primitives) construct here: if (Arrays.asList(a, b, c).contains(value)) { } else if (Arrays.asList(d, e).contains(value)) { } Or like this for int , long and similar types: switch (value) { case a: case b: case c: // .. break; case d: case e: // .. break; } Or

About reference to object before object's constructor is finished

核能气质少年 提交于 2019-11-28 18:54:38
Every one of you know about this feature of JMM , that sometimes reference to object could receive value before constructor of this object is finished. In JLS7, p. 17.5 final Field Semantics we can also read: The usage model for final fields is a simple one: Set the final fields for an object in that object's constructor; and do not write a reference to the object being constructed in a place where another thread can see it before the object's constructor is finished . If this is followed, then when the object is seen by another thread, that thread will always see the correctly constructed

Most specific method with matches of both fixed/variable arity (varargs)

◇◆丶佛笑我妖孽 提交于 2019-11-28 12:41:09
In section 15.12.2.5 of the Java Language Specification , it talks about how to choose the most specific method in both cases of methods with fixed arity and methods of variable arity (i.e. varargs ). What I can't find in the JLS is anything about deciding between two methods where one is of fixed arity and one of variable arity however. For example: public interface SomeApi { public String getSomething(String arg); // method 1 public String getSomething(String ... args); // method 2 } Compiles just fine as one would expect ( for reasons outlined by Yoni below ). This calling code compiles