小编Jok*_*ker的帖子

括号中的整数给出编译错误

我正在初始化两个整数ab.

它编译好,a但有一个错误b.

public class Main_1 {
    public static void main(String[] args) {
        int a = -2147483648; //Working fine
        int b = -(2147483648); //Compilation error: The literal 2147483648 of type int is out of range 
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么会这样?

java int compiler-errors

21
推荐指数
3
解决办法
1950
查看次数

在Map的entrySet中流与Iterator

据我了解,下面的代码应打印true,因为两者StreamIterator指向的第一个元素.

但是,当我运行以下代码时,它正在打印false:

final HashMap<String, String> map = new HashMap<>();
map.put("A", "B");
final Set<Map.Entry<String, String>> set = Collections.unmodifiableMap(map).entrySet();
Map.Entry<String, String> entry1 = set.iterator().next();
Map.Entry<String, String> entry2 = set.stream().findFirst().get();
System.out.println(entry1 == entry2);
Run Code Online (Sandbox Code Playgroud)

不同行为的原因是什么?

java iterator java-8 java-stream entryset

19
推荐指数
2
解决办法
1686
查看次数

为什么自动装箱在通过反射调用时不使用valueOf()?

据我所知,下面的代码应该打印"true",但是当我运行它时会打印出来"false".

public class Test {
    public static boolean testTrue() {
        return true;
    }

    public static void main(String[] args) throws Exception {
        Object trueResult = Test.class.getMethod("testTrue").invoke(null);
        System.out.println(trueResult == Boolean.TRUE);
    }
}
Run Code Online (Sandbox Code Playgroud)

根据JLS§5.1.7.拳击转换:

如果该值p被装箱是true,false,一个byte,或一个char在所述范围内\u0000\u007fintshort之间号-128127(含),然后让和是任何两个装箱转换的结果.情况总是如此.r1r2pr1 == r2

但是,如果通过反射调用方法,则始终通过框创建值new PrimitiveWrapper().

请帮我理解这个.

java autoboxing

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

使用FutureTask而不是Callable有什么好处?

有两种方法可以提交和轮询任务结果

FutureTask futureTask = new FutureTask<String>(callable);
Run Code Online (Sandbox Code Playgroud)
  1. 使用Callable和的组合Future并提交ExecutorService.使用检索结果future.get().

    Future future = service.submit(callable);
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用FutureTask.这将使用包装Callable然后检索结果FutureTask.

    service.execute(task);
    
    Run Code Online (Sandbox Code Playgroud)

使用FutureTaskover Callable+ Future组合有什么好处?

java concurrency multithreading future

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

java中的Arrays.copyOfRange方法抛出不正确的异常

我今天在阵列上工作,突然间我遇到了一个抛出意外异常的场景.

如果你看下面的代码,我认为它必须抛出ArrayIndexOutOfBoundsException,但令人惊讶的是它IllegalArgumentException反而投掷:

import java.util.Arrays;
public class RangeTest {
public static void main(String[] args) {
    int[] a = new int[] {0,1,2,3,4,5,6,7,8,9};
    int[] b = Arrays.copyOfRange(a, Integer.MIN_VALUE, 10);
    // If we'll use Integer.MIN_VALUE+100 instead Integer.MIN_VALUE,
    // OutOfMemoryError will be thrown
    for (int k = 0; k < b.length; k++)
        System.out.print(b[k] + " ");
   }
}
Run Code Online (Sandbox Code Playgroud)

如果我弄错了,有人可以帮助我,让我知道吗?

java arrays exception-handling exception try-catch

18
推荐指数
1
解决办法
2749
查看次数

ConcurrentHashMap陷入循环-为什么?

在对进行深入分析时ConcurrentHashMap,我在互联网上找到了一篇博客文章,内容甚至ConcurrentHashMap可能陷入无限循环。

它给出了这个例子。当我运行此代码时-卡住了:

public class Test {
    public static void main(String[] args) throws Exception {
        Map<Long, Long> map = new ConcurrentHashMap<>();
        map.put(0L, 0L);
        map.put((1L << 32) + 1, 0L);
        for (long key : map.keySet()) {
            map.put(key, map.remove(key));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我无法理解根本原因。请解释为什么会发生这种僵局。

java concurrenthashmap

18
推荐指数
3
解决办法
587
查看次数

Class.getDeclaredMethods()反射不需要的行为

我有一个A类,它是一个抽象类,B类是具体的,并扩展A.

除了B类之外,调用B.class.getDeclaredMethods()还会返回A类的方法签名,但JAVA文档说明了一些不同之处 getDeclaredMethods()

"这包括公共,受保护,默认(包)访问和私有方法,但不包括继承的方法."

所以从上面的文档中我期待从抽象父类继承的方法foo()不应该从getDeclaredMethods()调用返回,但是我从调用返回从抽象父类继承的方法foo()getDeclaredMethods().

import java.lang.reflect.*;

public class B extends A {
    public static void main(String[] args) throws Exception {
        Method[] methods = B.class.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            System.out.println(methods[i]);
        }
    }
}


abstract class A {
    public void foo() {
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释我这种行为.

在此输入图像描述

java reflection

17
推荐指数
2
解决办法
1860
查看次数

ConcurrentHashMap和Fibonacci数字 - 结果不一致

我写了一个递归计算斐波纳契数的程序,用a ConcurrentHashMapcomputeIfAbsent()方法:

程序工作绝对正常,当我使用小值8,9,10,但当从程序的值增加永远停止时卡在无限循环中10 to 20

 public class Test {
    static Map<Integer, Integer> concurrentMap = new ConcurrentHashMap<>();

    public static void main(String[] args) {
        System.out.println("Fibonacci result for 20 is" + fibonacci(20));
    }

    static int fibonacci(int i) {
        if (i == 0)
            return i;

        if (i == 1)
            return 1;

        return concurrentMap.computeIfAbsent(i, (key) -> {
            System.out.println("Value is " + key);
            return fibonacci(i - 2) + fibonacci(i - 1);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我为什么它会永远被卡住吗?

java fibonacci concurrenthashmap

15
推荐指数
1
解决办法
1062
查看次数

处理继承时出现IllegalAccessError - 为什么?

我发现了这个JDK错误,想要了解它为什么会发生.

场景(取自错误报告)非常简单:class声明一个private方法,并interface声明一个public具有相同签名的方法.它编译没有错误.

但是,当我运行此代码时,我得到了 IllegalAccessError

interface I {
    public void m();
}

class A {
    private void m() {
        System.out.println("Inside Class A");
    }

}

abstract class B extends A implements I {
}

class C extends B {
    public void m() {
        System.out.println("Inside Class C");
    }
}

public class Test {
    public static void main(String... args) {
        B b = new C();
        b.m();
    }
}
Run Code Online (Sandbox Code Playgroud)

请帮助我理解为什么这个错误存在,因为我的代码编译正常.

Exception …
Run Code Online (Sandbox Code Playgroud)

java inheritance

15
推荐指数
2
解决办法
251
查看次数

+0 和 -0 显示 int 和 float 数据的不同行为

我读过这篇文章负和正零

根据我的理解,以下代码应该给出truetrue 作为输出。

然而,它是给予falsetrue作为输出。

我正在比较负零和正零。

public class Test {
     public static void main(String[] args) {
            float f = 0;
            float f2 = -f;
            Float F = new Float(f);
            Float F1 = new Float(f2);
            System.out.println(F1.equals(F));

            int i = 0;
            int i2 = -i;
            Integer I = new Integer(i);
            Integer I1 = new Integer(i2);
            System.out.println(I1.equals(I));
      }
  }
Run Code Online (Sandbox Code Playgroud)

为什么我们对 0Integer和0 有不同的行为Float

java java-8

14
推荐指数
2
解决办法
411
查看次数