小编Evg*_*eev的帖子

Eclipse如何创建一个具有未解决的编译问题的类?

当我尝试用javac编译这个类时,我得到一个编译错误,并且没有创建Test.class.

public class Test {
    public static void main(String[] args) {
        int x = 1L;  // <- this cannot compile
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我在Eclipse中创建这个类时,我可以看到Test.class出现在target/classes中.当我尝试使用java.exe从命令行运行此类时,我得到了

线程"main"中的异常java.lang.Error:未解决的编译问题:
类型不匹配:无法从long转换为int

Eclipse是否使用自己的特殊Java编译器来创建损坏的.class?java.exe如何知道.class中的complilation问题?

java eclipse compiler-errors

68
推荐指数
3
解决办法
6088
查看次数

Java OutOfMemoryError奇怪的行为

假设我们的最大内存为256M,为什么这段代码有效:

public static void main(String... args) {
  for (int i = 0; i < 2; i++)
  {
      byte[] a1 = new byte[150000000];
  }
  byte[] a2 = new byte[150000000];
}
Run Code Online (Sandbox Code Playgroud)

但是这个扔了一个OOME?

public static void main(String... args) {
  //for (int i = 0; i < 2; i++)
  {
      byte[] a1 = new byte[150000000];
  }
  byte[] a2 = new byte[150000000];
}
Run Code Online (Sandbox Code Playgroud)

java out-of-memory

55
推荐指数
2
解决办法
1957
查看次数

Commons Lang StringUtils.replace性能vs.String.replace

当我比较Apache的性能StringUtils.replace()VS String.replace()我很惊讶地知道,前者是快约4倍.我使用Google的Caliper框架来衡量效果.这是我的考试

public class Performance extends SimpleBenchmark {
    String s = "111222111222";

    public int timeM1(int n) {
        int res = 0;
        for (int x = 0; x < n; x++) {
            res += s.replace("111", "333").length();
        }
        return res;
    }

    public int timeM2(int n) {
        int res = 0;
        for (int x = 0; x < n; x++) {
            res += StringUtils.replace(s, "111", "333", -1).length();
        }
        return res;
    }

    public static void main(String... args) {
        Runner.main(Performance.class, args);
    } …
Run Code Online (Sandbox Code Playgroud)

java

39
推荐指数
4
解决办法
4万
查看次数

使用Apache commons-io IOUtils.close可以安全吗?

这是代码

    BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"));
    try {
        bw.write("test");
    } finally {
        IOUtils.closeQuietly(bw);
    }
Run Code Online (Sandbox Code Playgroud)

安全与否?据我所知,当我们关闭BufferedWriter时,它会将其缓冲区刷新到底层流,并可能因错误而失败.但IOUtils.closeQuietly API表示将忽略任何异常.

由于IOUtils.closeQuietly,数据丢失是否有可能被忽视?

java apache-commons apache-commons-io

34
推荐指数
4
解决办法
3万
查看次数

在不丢失原始异常的情况下,在finally块中静默关闭InputStream的正确方法是什么?

我想知道下面的代码是否正确地关闭了finally块中的InputStream

InputStream is = new FileInputStream("test");
try {
    for(;;) {
        int b = is.read();
        ...
    }
} finally {
    try {
        is.close();
    } catch(IOException e) {
    }
}
Run Code Online (Sandbox Code Playgroud)

如果在is.close()期间发生异常,则在is.read()期间发生异常将被忽略/抑制吗?

java

26
推荐指数
4
解决办法
2万
查看次数

为什么一个类可以加载另一个类的私有内部类?

我有

class A {
    private static class B {
        B() {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

虽然B是私有的但我从另一个类加载A $ B.class没有问题.为什么允许这样做?

class C {
    public static void main(String[] args) throws Exception {
        System.out.println(Class.forName("A$B").newInstance());
    }
}
Run Code Online (Sandbox Code Playgroud)

产量

A$B@affc70
Run Code Online (Sandbox Code Playgroud)

UPDATE

我理解加载任何类的限制是有意提出的,但必须有一个合理的解释原因.

请注意,包私有B {}构造函数是故意的.如果我删除它我会得到

java.lang.IllegalAccessException:类B无法使用修饰符"private"访问类A $ B的成员

java

23
推荐指数
2
解决办法
1373
查看次数

如何将数字格式化为十六进制字符串?

我想将int数字格式化为十六进制字符串. System.out.println(Integer.toHexString(1));打印,1但我想要它0x00000001.我怎么做?

java formatting

21
推荐指数
4
解决办法
5万
查看次数

java.io.BufferedOutputStream可以安全使用吗?

乍一看这段代码似乎完全没问题

BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("1.txt"));
byte[] bytes = new byte[4096];
bout.write(bytes);
bout.close();
Run Code Online (Sandbox Code Playgroud)

但如果我们仔细研究一下,我们会看到close()实现如下

public void close() throws IOException {
    try {
      flush();
    } catch (IOException ignored) {
    }
    out.close();
}
Run Code Online (Sandbox Code Playgroud)

是否可能由于flush()错误被忽略,数据可能会丢失并且程序不会注意到它?没有提到任何危险FilterOutputStream.close(BufferedOutputStream继承close()自)API.

更新:在close()期间模拟IO错误我更改了测试以写入闪存,在bout.close()之前添加了5秒的睡眠,而在测试休眠时我从USB中移除了Flash.测试完成没有例外,但是当我插入Flash并检查它时 - 1.txt不存在.

然后我覆盖了close()

    BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("g:/1.txt")) {
        @Override
        public void close() throws IOException {
            flush();
            super.close();
        }
    };
Run Code Online (Sandbox Code Playgroud)

并再次进行测试并得到了

Exception in thread "main" java.io.FileNotFoundException: g:\1.txt (The system cannot the specified path)
    at …
Run Code Online (Sandbox Code Playgroud)

java

19
推荐指数
1
解决办法
3500
查看次数

从同步方法调用同步方法的同步成本是多少?

这之间的表现有什么不同吗?

synchronized void x() {
    y();
}

synchronized void y() {
}
Run Code Online (Sandbox Code Playgroud)

还有这个

synchronized void x() {
    y();
}

void y() {
}
Run Code Online (Sandbox Code Playgroud)

java concurrency synchronization

18
推荐指数
2
解决办法
3169
查看次数

当我增加最大堆大小时,为什么最大线程数会减少?

此测试显示可以在Java中创建的最大线程数

    System.out.println("Max memory  " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "M");
    for (int i = 0;; i++) {
        Thread t = new Thread() {
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                }
            };
        };
        try {
            t.start();
        } catch (Error e) {
            System.out.println("Max threads " + i);
            e.printStackTrace();
            System.exit(1);
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我使用默认堆大小(256M)运行它时,我得到了

Max memory  247M
Max threads 2247
java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:691)
    at test.Test1.main(Test1.java:19)
Run Code Online (Sandbox Code Playgroud)

当我将最大堆大小增加到512M时,我得到了

Max …
Run Code Online (Sandbox Code Playgroud)

java memory heap multithreading

17
推荐指数
1
解决办法
1940
查看次数