jls

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

半腔热情 提交于 2020-01-11 14:49:48
问题 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

What does “qualified this” construct mean in java?

早过忘川 提交于 2020-01-09 07:11:28
问题 In Effective Java inside the item "Item 22: Favor static member classes over nonstatic" Josh Bloch says: Each instance of a nonstatic member class is implicitly associated with an enclosing instance of its containing class. Within instance methods of a nonstatic member class, you can invoke methods on the enclosing instance or obtain a reference to the enclosing instance using the qualified this construct. What does he mean by Qualified This Construct ? 回答1: Without the qualifier, x() would

Why does Java bind variables at compile time?

这一生的挚爱 提交于 2020-01-09 06:05:54
问题 Consider the following example code class MyClass { public String var = "base"; public void printVar() { System.out.println(var); } } class MyDerivedClass extends MyClass { public String var = "derived"; public void printVar() { System.out.println(var); } } public class Binding { public static void main(String[] args) { MyClass base = new MyClass(); MyClass derived = new MyDerivedClass(); System.out.println(base.var); System.out.println(derived.var); base.printVar(); derived.printVar(); } }

Why does Java bind variables at compile time?

冷暖自知 提交于 2020-01-09 06:05:29
问题 Consider the following example code class MyClass { public String var = "base"; public void printVar() { System.out.println(var); } } class MyDerivedClass extends MyClass { public String var = "derived"; public void printVar() { System.out.println(var); } } public class Binding { public static void main(String[] args) { MyClass base = new MyClass(); MyClass derived = new MyDerivedClass(); System.out.println(base.var); System.out.println(derived.var); base.printVar(); derived.printVar(); } }

Is there a tool to determine whether a program is “correctly synchronized” as defined in JLS?

流过昼夜 提交于 2020-01-04 04:19:32
问题 The Java Language Specification 7 (JLS7-17.4.5) defines a "correctly synchronized" program like this: "A program is correctly synchronized if and only if all sequentially consistent executions are free of data races". JLS7-17.4.5 also states that: Without correct synchronization, very strange, confusing and counterintuitive behaviors are possible. So, from a programmer's point of view, it would be very useful to have a tool to determine whether a program is "correctly synchronized" according

Which part of JLS said anonymous classes cannot have public/protected/private member classes

大兔子大兔子 提交于 2020-01-01 08:29:57
问题 Consider this piece of code: public class TopLevelClass { Cloneable c = new Cloneable() { private int privateField; private void privateMethod() {}; }; } There is an anonymous class that has a private member field and a private member method. It has been successfully compiled. Then consider this one: public class TopLevelClass { Cloneable c = new Cloneable() { private class PrivateInnerClass {} }; } There is an anonymous class that has a private member class. However... javac said: error:

When can I use “==” operator?

99封情书 提交于 2019-12-30 13:39:02
问题 I have found quote from jls: The equality operators may be used to compare two operands that are convertible (§5.1.8) to numeric type, or two operands of type boolean or Boolean, or two operands that are each of either reference type or the null type . All other cases result in a compile-time error. But this code String str= ""; Number num = 1; System.out.println(str == num); every operand is reference! said that it is incompatible types. Where did in jls say that these types should be

When can I use “==” operator?

天涯浪子 提交于 2019-12-30 13:36:12
问题 I have found quote from jls: The equality operators may be used to compare two operands that are convertible (§5.1.8) to numeric type, or two operands of type boolean or Boolean, or two operands that are each of either reference type or the null type . All other cases result in a compile-time error. But this code String str= ""; Number num = 1; System.out.println(str == num); every operand is reference! said that it is incompatible types. Where did in jls say that these types should be

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

 ̄綄美尐妖づ 提交于 2019-12-30 10:19:29
问题 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, . . . ,

What are the formal conditions for a wildcard parameter in a Java generic type to be within its bounds?

╄→гoц情女王★ 提交于 2019-12-30 06:10:07
问题 With parameterized types in Java, how do the rules that check if a parameter is within its bound work exactly for wildcards? Given a class like this: class Foo<T extends Number> {} Experimenting with what the compiler accepts learns that: A ? extends wildcard using an unrelated interface type is allowed: Foo<? extends Runnable> is valid A ? extends wildcard using an unrelated class type is not allowed: Foo<? extends Thread> is invalid. That makes sense because no type can be a subtype of both