例如:
int anInt = null;
Run Code Online (Sandbox Code Playgroud)
在编译时失败但是
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println("" + getSomeVal());
}
}
public static int getSomeVal() {
return new Random().nextBoolean() ? 1 : null;
}
Run Code Online (Sandbox Code Playgroud)
在运行时(通常)失败.试图返回刚才null也会导致编译错误,所以我假设有多条路径导致编译器推断null可能是自动装箱int?为什么javac能够以相同的错误编译这两种情况?
Scala-lang参考5.5.1和6.6.1给我的印象是默认参数能够引用先前评估的参数:
class Test(val first: String, val second: String = first)
Run Code Online (Sandbox Code Playgroud)
但从实验来看,似乎唯一的方法就是使用表格:
class Test(val first: String)(val second: String = first)
Run Code Online (Sandbox Code Playgroud)
然后定义辅助构造函数或创建伴随类,以避免在创建时指定第二组括号.我真的不明白第二个构造函数是如何工作的,它看起来像一个curried函数所以我可能会猜测有必要first独立评估second,这是正确的吗?这个形式是必要的还是有一些合成糖可以用来调整第一个构造函数来做我想要的?
constructor scala operator-precedence default-parameters default-arguments
在java中8 java.util.HashMap中我注意到一个变化来自:
static int hash(int h) {
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
Run Code Online (Sandbox Code Playgroud)
到:
static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
Run Code Online (Sandbox Code Playgroud)
从代码中可以看出,新函数是XOR低16位的简单函数,高16位保持高16位不变,而前一实现中的几个不同的位移相反,并且从评论中看,这在分配时效果较差散列函数的结果,在低位到较大位的冲突很多,但通过减少操作来节省CPU周期.
我在发行说明中看到的唯一一件事就是从链接列表到平衡树的变化以存储碰撞键(我认为这可能会改变计算好哈希的时间量),我特别感兴趣的是看到如果此更改对大型哈希映射有任何预期的性能影响.是否有关于此更改的任何信息,或者是否具有更好的哈希函数知识的任何人都知道此更改的含义可能是什么(如果有的话,我可能只是误解了代码)以及是否需要生成哈希迁移到Java 8时,以不同的方式编码以保持性能?
如果我在多线程环境中有一个不同步的java集合,并且我不想强制集合的读者同步[1],那么我是一个同步编写器并使用引用赋值的原子性可行的解决方案吗?就像是:
private Collection global = new HashSet(); // start threading after this
void allUpdatesGoThroughHere(Object exampleOperand) {
// My hypothesis is that this prevents operations in the block being re-ordered
synchronized(global) {
Collection copy = new HashSet(global);
copy.remove(exampleOperand);
// Given my hypothesis, we should have a fully constructed object here. So a
// reader will either get the old or the new Collection, but never an
// inconsistent one.
global = copy;
}
}
// Do multithreaded reads here. …Run Code Online (Sandbox Code Playgroud) 我正在搜索由OpenGrok索引的代码库,-a选项已启用,允许搜索词的第一个字符为通配符.我想找到所有出现的带有foo一些字符串参数的方法(foo("")在字符串中有一个或多个字符),并且该方法在变量的5个字内bar.
随着标准标记化器提供的单词分解,我可以使用它+full:"foo bar"~5来使所有foo(附近bar,但排除无参数方法是一个问题,因为("索引为两个空白字符,所以例如foo("jar") 似乎匹配foo AND \ \ AND jar AND \ \".该语法看起来并不像它支持这种组合(即使没有通配符的字符串,而不是搜索"jar")与""~5邻近搜索.有关如何实现全部或部分内容的任何想法,因为目前不能更改令牌器吗?
当WatchService监视的目录被删除时,其父目录不会立即反映其文件的listFiles方法中的删除,并且无法删除.在明确停止整个服务之前,父母的后果似乎是:
为了演示,这个测试代码:
import java.io.*;
import java.nio.file.*;
class DirectoryTester {
static WatchService watcher;
static {
try{watcher = FileSystems.getDefault().newWatchService();}
catch (IOException e) {e.printStackTrace();}
}
public static void main(String[] args) throws IOException {
String SEPARATE = System.getProperty("file.separator");
String testDirName = System.getProperty("user.dir") + SEPARATE + "testDir";
String subDirName = testDirName + SEPARATE + "subDir";
String fileName = subDirName + SEPARATE +"aFile";
create(fileName);
Paths.get(subDirName).register(watcher, StandardWatchEventKinds.ENTRY_DELETE);
delete(new File(testDirName));
}
static void create(String …Run Code Online (Sandbox Code Playgroud) 我想跳转到Any的源代码,看看有些方法是如何实现的,但是我看到它没有包含在scala-library-src中,也没有AnyRef(值类型和Nothing都是).我很好奇,Scala对象基类型是如何实现的?
当我编译:
public static final boolean FOO = false;
public static final void fooTest() {
if (FOO) {
System.out.println("gg");
}
}
Run Code Online (Sandbox Code Playgroud)
我得到一个空方法fooTest() {}.但是当我编译时:
static boolean isBar = false;
public static final boolean BAR = isBar;
public static final void fooTest() {
if (BAR) {
System.out.println("gg");
}
}
Run Code Online (Sandbox Code Playgroud)
if语句包含在已编译的类文件中.这是否意味着java中有两种不同的"类型"静态final,或者这仅仅是编译器优化?
Java OpenGL GL接口包含大约2000个方法,出于调试目的,我想在执行一些日志记录时包装一个实例并委托对它进行调用.在每种情况下,日志代码都可以推送到相同的方法,因此写出方法实现的任务看起来可以自动化.我想要做的一个例子:
import javax.media.opengl.GL;
public class GLErrorLogger implements GL {
private final GL backing;
public GLErrorLogger(GL delegateToMe) {
backing = delegateToMe;
}
private void checkErrorCode() {
// Log frame and thread details depending on gl state
}
/**
* Example of a method
*/
@Override
public int glGenLists(int arg0) {
checkErrorCode();
int retVal = backing.glGenLists(arg0);
checkErrorCode();
return retVal;
}
// rest of methods here...
}
Run Code Online (Sandbox Code Playgroud)
换句话说,将方法名称和参数(减去它们的类型)复制到后备对象上的调用中,使用对日志记录方法的调用进行环绕,如果有返回类型,则将结果分配给此类型的变量并将其返回在方法的最后.
我看了创建一个单独的eclipse代码模板来自动生成方法,但是没有一种直接明显的方法在返回类型上进行模式匹配.任何人都可以建议一种方法在Eclipse或其任何代码生成工具中执行此操作,以节省我拔出正则表达式工具包?
java ×6
javac ×2
scala ×2
class ×1
compilation ×1
constructor ×1
debugging ×1
eclipse ×1
filesystems ×1
final ×1
fuzzy-search ×1
hash ×1
hashmap ×1
locking ×1
lucene ×1
opengrok ×1
performance ×1
proximity ×1
search ×1
templates ×1
unboxing ×1
windows ×1