我已经反编译了一个使用新的Java 7字符串切换功能的非常简单的类.
班级:
public class StringSwitch {
public static void main(String[] args) {
final String color = "red";
switch (color) {
case "red":
System.out.println("IS RED!");
break;
case "black":
System.out.println("IS BLACK");
break;
case "blue":
System.out.println("IS BLUE");
break;
case "green":
System.out.println("IS GREEN");
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
针对此类运行Java 7"javap",会生成一组有趣的指令(完整的反汇编代码可在此处获得):
public static void main(java.lang.String[]);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=4, args_size=1
...
12: lookupswitch { // 4
112785: 56
3027034: 84
93818879: 70
98619139: 98
default: 109
}
56: aload_2
57: ldc …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Java 7的 ThreadLocalRandom,并发现它在多个线程中生成完全相同的随机数.
这是我的代码,我创建了5个线程,每个线程打印出5个随机数:
//5 threads
for(int i = 0; i < 5 ; i++) {
final Thread thread = new Thread() {
@Override
public void run() {
System.out.print(Thread.currentThread().getName()+":");
//each thread prints 5 random numbers
for(int j = 0 ; j < 5; j++) {
final int random = ThreadLocalRandom.current().nextInt(1,100);
System.out.print(random + ",");
}
System.out.println();
}
};
thread.start();
thread.join();
}
Run Code Online (Sandbox Code Playgroud)
输出:
Thread-0:1,93,45,75,29,
Thread-1:1,93,45,75,29,
Thread-2:1,93,45,75,29,
Thread-3:1,93,45,75,29,
Thread-4:1,93,45,75,29,
Run Code Online (Sandbox Code Playgroud)
为什么我为每个线程和每次执行程序获得相同的随机数?
是否存在JDK的界面,如下所示:
public interface Callback<T> {
public void process(T t);
}
Run Code Online (Sandbox Code Playgroud)
要求是实现运行代码的回调,但不返回任何内容.
我可以编写自己的(通过简单地使用这里的示例代码),但我想使用现有的轮子,如果存在,而不是重新发明一个.
好吧,我通过相关问题,我阅读了JDK 1.7的源代码,但我找不到答案.
在这个问题中,我想完全忽略fillInStackTrace.
从JDK 1.4 initCause()开始,添加了方法.例如,当您使用核心反射来调用该方法时,您会收到InvocationTargetException,其中包含具有目标异常的原因.
当我看到这个功能时,我也开始在这样的场景中使用它
try {
//contains some code that can throw new IOException();
}
catch(IOException e){
throw new RuntimeException(e);
}
Run Code Online (Sandbox Code Playgroud)
所以,我抓住了一个异常,我还没准备好在这里处理它并且我重新抛出新的异常,其中我有原始异常作为原因.在一些scenarious而不是RuntimeException,但我的自定义异常被使用,所以有时我也调用e.getCause(),以便在外部块中正确处理此异常.
这是JDK 1.7之前的情况.我应该何时何地使用addSuppressed()?我应该将上面的代码更改为
try {
//contains some code that can throw new IOException();
}
catch(IOException e){
RuntimeException re= new RuntimeException(e.getMessage());
re.addSuppressed(e);
throw re;
}
Run Code Online (Sandbox Code Playgroud)
作为奖励问题,为什么不addSuppressed()返回Throwable作为initCause()确实允许throw (RuntimeException)new RuntimeException().initCause(e);?例如,为什么我不能这样做?:
try {
//contains some code that can throw new IOException();
}
catch(IOException e){
throw …Run Code Online (Sandbox Code Playgroud) 我有一个名为MatchingLine的课程
public class MatchingLine implements Comparable
{
private String matchingLine;
private int numberOfMatches;
// constructor...
// getters and setters...
// interface method implementation...
}
Run Code Online (Sandbox Code Playgroud)
我在ArrayList中使用此类如下 -
ArrayList<MatchingLine> matchingLines = new ArrayList<MatchingLine>();
Run Code Online (Sandbox Code Playgroud)
但是,Netbeans IDE在此声明旁边说了一句话,并说,
redundant type arguments in new expression (use diamond operator instead)
Run Code Online (Sandbox Code Playgroud)
它表明我使用 -
ArrayList<MatchingLine> matchingLines = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
我一直认为前一种风格是惯例?后一种风格是惯例吗?
我已经阅读了"Groovy 2.0中的新功能",我对何时使用@CompileStatic感到有些困惑.文章提到@CompileStatic为没有能够利用Java7的调用动态部分的开发人员添加了注释.
因此,如果无法在JDK 7上运行,那么寻求性能改进的开发人员不会看到Groovy 2.0中的太多变化.幸运的是,Groovy开发团队认为这些开发人员可以通过允许类型检查来获得有趣的性能提升等优势代码要静态编译.
我的问题是,如果我使用JDK 7并按照说明添加--indy标志,我是否需要添加@CompileStatic才能看到性能提升? 这个博客暗示我会,但我不确定他是否正确编译,因为他在Eclipse中做到了.
更新:以下是运行Fibonacci代码的不同排列时的统计信息.
> groovy --indy FibBoth.groovy
..........Fib (non-static indy): 1994.465
..........Fib (static indy): 529.197
> groovy FibBoth.groovy
..........Fib (non-static): 1212.788
..........Fib (static): 525.671
Run Code Online (Sandbox Code Playgroud)
注意:现在我明白这些功能是独立的,这个问题似乎有点混乱.由于问题的基础是围绕着使我认为两个特征相关的注释的混淆,我认为不改变问题措辞并允许解释差异的接受答案是有道理的.
ArrayList的以下两个声明有什么区别?
ArrayList<Integer> nunbers = new ArrayList<Integer>();
Run Code Online (Sandbox Code Playgroud)
VS
ArrayList<Integer> nunbers = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
他们中的一个比另一个更受欢迎吗?
我在Android Studio中创建了一个新项目,并添加了Google地图活动.
我收到这些警告:
warning: com/google/android/gms/maps/GoogleMap.class(com/google/android/gms/maps:GoogleMap.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: com/google/android/gms/maps/SupportMapFragment.class(com/google/android/gms/maps:SupportMapFragment.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: com/google/android/gms/maps/model/LatLng.class(com/google/android/gms/maps/model:LatLng.class): major version 51 is newer than 50, the highest major version supported by this compiler.
It is recommended that the compiler be upgraded.
warning: com/google/android/gms/maps/model/MarkerOptions.class(com/google/android/gms/maps/model:MarkerOptions.class): …Run Code Online (Sandbox Code Playgroud) 我已经使用旧的,过时java.io.File.listFiles()的了.
表现不太好.它是:
File为每个条目创建一个新对象.有哪些替代方案?
Files.walkFileTree访问同一级别的文件/目录的顺序是什么?
它似乎没有按大小,上次修改时间或名称的顺序访问它们.我在API文档中也找不到任何内容.
也许该preVisitDirectory方法可用于指定访问顺序,但默认行为是什么?
java-7 ×10
java ×9
android ×1
arraylist ×1
coding-style ×1
decompiling ×1
exception ×1
file-io ×1
filesystems ×1
generics ×1
groovy ×1
groovy-2 ×1
interface ×1
jvm ×1
list ×1
netbeans-7 ×1
random ×1