jls

Is there a difference when specifying upper bounds for wildcards explicitly?

醉酒当歌 提交于 2021-02-17 08:55:34
问题 Suppose I have a generic class Generic<A extends BaseType> . Is there a notable difference, as far as the Java Language Specification is concerned, between the following two type declarations? Generic<?> Generic<? extends BaseType> What about nested wildcards? List<Generic<?>> List<Generic<? extends BaseType>> Thinking about this, I would assume these to be equivalent. Generic specifies that the type parameter A has BaseType for an upper bound. Thus, the wildcard should always be

Does the JLS require inlining of final String constants?

别等时光非礼了梦想. 提交于 2021-02-16 09:57:11
问题 I ran into an issue while manipulating some bytecode, where a certain final String constant was not inlined by the java compiler (Java 8), see the example below: public class MyTest { private static final String ENABLED = "Y"; private static final String DISABLED = "N"; private static boolean isEnabled(String key) { return key.equals("A"); } private static String getString(String key, String value) { return key + value; } public static void main(String[] args) throws Exception { String flag =

Java final fields: is “taint” behavior possible with the current JLS

左心房为你撑大大i 提交于 2021-02-07 05:27:28
问题 I'm currently trying to understand this JLS section on final fields. To understand the text in the JLS better I'm also reading The Java Memory Model by Jeremy Manson (one of creators of the JMM). The paper contains the example that got me interested: if an object o with final fields is made visible to another thread t twice: first "improperly" before o 's constructor finishes next "properly" after o 's constructor finishes then t can see semi-constructed o even when it is accessed only via a

How do default and static methods work in java 8 interfaces?

六眼飞鱼酱① 提交于 2021-02-04 19:13:33
问题 I have been trying to get my head around on how actually do the default and static methods work in java 8? consider the following interface: public interface Car { default void drive() { System.out.println("Default Driving"); } static int getWheelCount(){ return wheelCount; } int wheelCount = 7; } and the following implementation: public class Benz implements Car { } Now if I go to my main method and write: public static void main(String[] args){ Car car = new Benz(); car.drive(); System.out

How the intents processed in a Text block(Java 13)

冷暖自知 提交于 2020-03-18 09:29:29
问题 I just tried the new text block feature in Java 13 and encountered a small issue. I have read this article from Jaxcenter. The closing triple quotation marks will affect the format. String query = """ select firstName, lastName, email from User where id= ? """; System.out.println("SQL or JPL like query string :\n" + query); This above format works well. To align with the the closing delimiter ("""), the multiline string left spaces before every lines. But when I tried to compare the following

Java中的内部类实例初始化匿名函数(JLS7 8.6:Instance Initializers)

放肆的年华 提交于 2020-03-02 15:24:25
偶尔发现了一段好玩的java代码,关于Java内部类的,觉得很有意思,但是想不明白这段代码是java语法的特性哪?还是别的什么。最后发现还是对java了解的不够深啊。先上代码: import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<String> lst = new ArrayList<String>() { @Override public boolean add(String paramE) { return super.add("LST-" + paramE); } { add("a"); add("45"); add("a"); add("asdfs"); } }; for (String str : lst) { System.out.println(str); } } } 最后多谢@brayden的解答,查看了JLS定义中的Instance Initializers,了解到这个是Java语法标准。顺便我反编译了一下,生成的代码,下面这段匿名方法最终会编译到这个内部类的构造函数中。 { add("a"); add("45"); add("a"); add("asdfs"); }

hdu5306(线段树+区间取最值)jls线段树

我只是一个虾纸丫 提交于 2020-02-08 00:25:56
jls论文: 根据上面论文我们就可以做这题了。 终于学会了jls线段树。 手敲了下,看懂理论1h,写+bug就花了不到1h。 果然线段树我还是比较熟练的 马上搞下camp树套jls树。。。 #include<bits/stdc++.h> using namespace std; #define ls o*2 #define rs o*2+1 typedef long long ll; const int M=1e6+7; ll mx[M<<2];//最大值 ll se[M<<2];//次大值 ll nm[M<<2];//最大值个数 ll sm[M<<2];//区间和 ll lz[M<<2];//懒标记,即该区间要与lzo 取min ll a[M]; void pu(int o,int l,int r)//合并左右子树 { sm[o]=sm[ls]+sm[rs]; mx[o]=max(mx[ls],mx[rs]); se[o]=max(se[ls],se[rs]); nm[o]=0; if(mx[ls]!=mx[rs]) se[o]=max(se[o],min(mx[ls],mx[rs])); if(mx[o]==mx[ls]) nm[o]+=nm[ls]; if(mx[o]==mx[rs]) nm[o]+=nm[rs]; } void bd(int o,int l,int r) {

How does the JLS specify the terms “abstract method”, “concrete method” and “default method”?

为君一笑 提交于 2020-02-03 05:48:10
问题 I have seen "divergent" definitions of the terms abstract method , concrete method and default method in some StackOverflow answers. What are the real definitions, as given by the Java Language Specification? Please include the relevant supporting JLS references in your answer. 回答1: According to the JLS 8.4.3.1: "An abstract method declaration introduces the method as a member, providing its signature (§8.4.2), result (§8.4.5), and throws clause if any (§8.4.6), but does not provide an

“variable xxx might not have been initialized” when calling static method that returns variable of the same type and the same name of the type itself

巧了我就是萌 提交于 2020-01-30 05:50:10
问题 Why does it fail with the error shown below? I'm not sure where in the JLS to look for the restriction to do something like this. public class A { static A foo() { return null; } public static void main(String[] args) { A A = A.foo(); } } Error at compile time A.java:14: error: variable A might not have been initialized A A = A.foo(); ^ 1 error 回答1: The variable hides the class of the same name. That's part of why there are naming conventions. As Patricia notes in the comments, this is

Why do interfaces extend Object, according to the class file format?

旧时模样 提交于 2020-01-12 14:13:10
问题 Why does the JVM specification state that interfaces must have a super_class of java/lang/Object , even though interfaces do not extend java/lang/Object ? I'm specifically referring to §4.1 of the JVM spec, where it says: For an interface, the value of the super_class item must always be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object. yet in §9.2 of the JLS, it says that interfaces do not