jls

Final fields initialization order

孤人 提交于 2019-12-17 18:22:15
问题 Here is some code that calls static method A.f() on class that is not initialized yet. Can someone explain behavior of this code in terms of JLS? class A { final static Object b = new B(); final static int S1 = 1; final static Integer S2 = 2; static void f() { System.out.println(S1); System.out.println(S2); } } class B { static { A.f(); } } public class App { public static void main( String[] args ) { A.f(); } } Output: 1 null 1 2 回答1: A.f() in App.main() triggers initialization of class A .

Is it a good practise to remove a function instead of making it stay there with a deprecated annotation [closed]

落花浮王杯 提交于 2019-12-13 09:08:21
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . So i had this long argument with some guys and they kept fighting over their self made code conventions. According to which , they said if there is a method that you while coding the stuff you thought it should be deprecated.You simply remove it,change everywhere it has been

Why does this program compile with Java 7 but not Java 8? [duplicate]

孤人 提交于 2019-12-12 10:57:02
问题 This question already has answers here : Generics compilation error with ternary operator in Java 8, but not in Java 7 (3 answers) Closed 4 years ago . Consider this program: public class xx<T> { <T> Iterable<T> createIterable(Class<T> cls) { return null; } Iterable<? extends Number> createNumberIterable(boolean floatingPoint) { return this.createIterable(floatingPoint ? Integer.class : Float.class); } } Under Java 7 it compiles: $ java -version java version "1.7.0_45" Java(TM) SE Runtime

In Java, can a method/constructor declaration appear inside another method/constructor declaration?

邮差的信 提交于 2019-12-11 10:09:18
问题 In Java, can a method/constructor declaration appear inside another method/constructor declaration? Example: void A() { int B() { } } I think not, but I'd love to be reassured. 回答1: No . it is not compilable. 回答2: No this is not possible. For reference: http://download.oracle.com/javase/tutorial/java/javaOO/methods.html 回答3: Not directly, but you can have a method in a class in a method: class A { void b() { class C { void d() { } } } } 回答4: This is not possible in java. However this can

Where does the JLS specify that the result of an addition is int if its operands are of smaller type?

孤者浪人 提交于 2019-12-11 03:43:23
问题 With reference to Why i am getting type mismatch: cannot convert from int to byte, I tried a quick search in the JLS to find where is it mentioned that the result of an addition between byte operands is automatically widened to int . The best I found was this tutorial, but I didnt' find anything in the JLS. The Conversions chapter does not mention anything about the addition operator, at least I couldn't find something. The Additive Operators paragraph does not mention automatic widening of

Bitshift operators description in Java language specification [closed]

不羁岁月 提交于 2019-12-10 22:57:17
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 2 years ago . At specified in JLS8 at §JLS-15.19 If the promoted type of the left-hand operand is int, then only the five lowest-order bits of the right-hand operand are used as the shift distance . It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask

What's the definition of “during the operation of the Java Virtual Machine”?

风流意气都作罢 提交于 2019-12-10 21:55:29
问题 From JVMS chapter 6.3: [...] any of the VirtualMachineError subclasses defined below [ InternalError , OutOfMemoryError , StackOverflowError , UnknownError ] may be thrown at any time during the operation of the Java Virtual Machine How does the JVMS define the phrase "at any time during the operation of the Java Virtual Machine"? How do current JVMs interpret that phrase? Specifically, does it mean that the four errors, java.lang.InternalError, java.lang.OutOfMemoryError, java.lang

Java language specification on wildcards

流过昼夜 提交于 2019-12-10 19:31:22
问题 I am going through this link (Chapter 4. Types, Values, and Variables) and did not understand below point: The relationship of wildcards to established type theory is an interesting one, which we briefly allude to here. Wildcards are a restricted form of existential types. Given a generic type declaration G<T extends B>, G<?> is roughly analogous to Some X <: B. G<X> . I appreciate if you provide good example to understand above point clearly. Thanks in advance. 回答1: The wording and

What operations may (not) throw StackOverflowError?

筅森魡賤 提交于 2019-12-10 16:49:06
问题 When will a StackOverError be thrown? Or rather, when will it not be thrown? For example , if we use the primitive operators + , += , - , -= , == < , > , / , % , etc: try { // operations +, +=, -, -=, == <, >, /, %, etc } catch (java.lang.StackOverflowError e) { // will never occur? } Is there any guarantee that StackOverflowError will not be thrown? 回答1: is it true that code which do not call any functions will never throw a java.lang.StackOverflowError? A StackOverflowError is-a

Does JVM loads all used classes when loading a particular class?

梦想与她 提交于 2019-12-09 23:37:48
问题 When JVM loads a class A, does it load all of the classes used within A? And I'm wondering if import declarations are matter somehow to the loading process? The link to JLS would be appreciated. 回答1: Import and class loading are unrelated. The former just saves typing: it allows you to use the short class name rather than the fully-resolved class name in your code. Classes are loaded by the JVM when they're used for the first time. 回答2: import merely helps the programmer. When the class file