jls

Are there any guarantees in JLS about order of execution static initialization blocks?

左心房为你撑大大i 提交于 2019-11-26 22:10:20
问题 I wonder if it's reliable to use a construction like: private static final Map<String, String> engMessages; private static final Map<String, String> rusMessages; static { engMessages = new HashMap<String, String> () {{ put ("msgname", "value"); }}; rusMessages = new HashMap<String, String> () {{ put ("msgname", "значение"); }}; } private static Map<String, String> msgSource; static { msgSource = engMessages; } public static String msg (String msgName) { return msgSource.get (msgName); } Is

Why doesn't a Java constant divided by zero produce compile time error? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-11-26 20:57:19
问题 Possible Duplicate: Is 1/0 a legal Java expression? Why does this code compile? class Compiles { public final static int A = 7/0; public final static int B = 10*3; public static void main(String[] args) {} } If I take a look in the compiled class file, I can see that B has been evaluated to 30, and that A still is 7/0. As far as I understand the JSL an expression where you divide by zero is not a constant. Ref: JLS 15.28 My above statement is due to this line: A compile-time constant

Return value of assignment operator in concurrent code

只谈情不闲聊 提交于 2019-11-26 19:57:15
问题 Given the following class: class Foo { public volatile int number; public int method1() { int ret = number = 1; return ret; } public int method2() { int ret = number = 2; return ret; } } and given multiple threads calling method1() and method2() concurrently on the same Foo instance, can a call to method1() ever return anything other than 1? 回答1: The JLS 15.26 specifies: There are 12 assignment operators; all are syntactically right-associative (they group right-to-left). Thus, a=b=c means a=

Case sensitivity of Java class names

岁酱吖の 提交于 2019-11-26 19:21:45
问题 If one writes two public Java classes with the same case-insensitive name in different directories then both classes are not usable at runtime. (I tested this on Windows, Mac and Linux with several versions of the HotSpot JVM. I would not be surprised if there other JVMs where they are usable simultaneously.) For example, if I create a class named a and one named A like so: // lowercase/src/testcase/a.java package testcase; public class a { public static String myCase() { return "lower"; } }

Testing initialization safety of final fields

不羁的心 提交于 2019-11-26 17:36:13
问题 I am trying to simply test out the initialization safety of final fields as guaranteed by the JLS. It is for a paper I'm writing. However, I am unable to get it to 'fail' based on my current code. Can someone tell me what I'm doing wrong, or if this is just something I have to run over and over again and then see a failure with some unlucky timing? Here is my code: public class TestClass { final int x; int y; static TestClass f; public TestClass() { x = 3; y = 4; } static void writer() {

Anonymous-Inner classes showing unwanted modifier

别等时光非礼了梦想. 提交于 2019-11-26 16:57:59
问题 To my understanding, the following code should have printed true . However, when I ran this code it is printing false . From Java docs of Anonymous Classes 15.9.5. : An anonymous class is always implicitly final public class Test { public static void main(String args[]) { Object o = new Object() { }; System.out.println("Annonymous class is final: " + Modifier.isFinal(o.getClass().getModifiers())); } } Can some one please help me understand this behavior. 回答1: Note that the wording in the JLS

Is a write to a volatile a memory-barrier in Java

末鹿安然 提交于 2019-11-26 16:24:15
问题 I recently heard in a talk that a write to a volatile triggers a memory-barrier for each variable that the thread has written to. Is that really correct? From the JLS, it seems that only the variable concerned gets flushed out, but not others. Does anybody know what is actually correct? Can one point me a concrete location in the JLS? 回答1: Yes, it will initiate a barrier. You can read more here. There are 4 types, LoadLoad LoadStore StoreStore StoreLoad. As far as your question From the JLS,

Order of execution of parameters guarantees in Java?

拟墨画扇 提交于 2019-11-26 16:20:26
问题 Given the following function call in C : fooFunc( barFunc(), bazFunc() ); The order of execution of barFunc and BazFunc is not specified, so barFunc() may be called before bazFunc() or bazFunc() before barFunc() in C . Does Java specify an order of execution of function argument expressions or like C is that unspecified? 回答1: From the Java Language Specification (on Expressions): 15.7.4 Argument Lists are Evaluated Left-to-Right In a method or constructor invocation or class instance creation

Lambda expression and method overloading doubts

五迷三道 提交于 2019-11-26 16:14:56
OK, so method overloading is-a-bad-thing™. Now that this has been settled, let's assume I actually want to overload a method like this: static void run(Consumer<Integer> consumer) { System.out.println("consumer"); } static void run(Function<Integer, Integer> function) { System.out.println("function"); } In Java 7, I could call them easily with non-ambiguous anonymous classes as arguments: run(new Consumer<Integer>() { public void accept(Integer integer) {} }); run(new Function<Integer, Integer>() { public Integer apply(Integer o) { return 1; } }); Now in Java 8, I'd like to call those methods

If you overwrite a field in a subclass of a class, the subclass has two fields with the same name(and different type)?

好久不见. 提交于 2019-11-26 14:28:36
I have 3 classes: public class Alpha { public Number number; } public class Beta extends Alpha { public String number; } public class Gama extends Beta { public int number; } Why does the following code compile? And, why does the test pass without any runtime errors? @Test public void test() { final Beta a = new Gama(); a.number = "its a string"; ((Alpha) a).number = 13; ((Gama) a).number = 42; assertEquals("its a string", a.number); assertEquals(13, ((Alpha) a).number); assertEquals(42, ((Gama) a).number); } Member variables cannot be overridden like methods. The number variables in your