我正在初始化两个整数a和b.
它编译好,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)
为什么会这样?
据我了解,下面的代码应打印true,因为两者Stream并Iterator指向的第一个元素.
但是,当我运行以下代码时,它正在打印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)
不同行为的原因是什么?
据我所知,下面的代码应该打印"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)
如果该值
p被装箱是true,false,一个byte,或一个char在所述范围内\u0000对\u007f或int或short之间号-128和127(含),然后让和是任何两个装箱转换的结果.情况总是如此.r1r2pr1 == r2
但是,如果通过反射调用方法,则始终通过框创建值new PrimitiveWrapper().
请帮我理解这个.
有两种方法可以提交和轮询任务结果
FutureTask futureTask = new FutureTask<String>(callable);
Run Code Online (Sandbox Code Playgroud)
使用Callable和的组合Future并提交ExecutorService.使用检索结果future.get().
Future future = service.submit(callable);
Run Code Online (Sandbox Code Playgroud)使用FutureTask.这将使用包装Callable然后检索结果FutureTask.
service.execute(task);
Run Code Online (Sandbox Code Playgroud)使用FutureTaskover Callable+ Future组合有什么好处?
我今天在阵列上工作,突然间我遇到了一个抛出意外异常的场景.
如果你看下面的代码,我认为它必须抛出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)
如果我弄错了,有人可以帮助我,让我知道吗?
在对进行深入分析时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)
我无法理解根本原因。请解释为什么会发生这种僵局。
我有一个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)
有人可以解释我这种行为.
我写了一个递归计算斐波纳契数的程序,用a ConcurrentHashMap和 computeIfAbsent()方法:
程序工作绝对正常,当我使用小值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)
有人可以告诉我为什么它会永远被卡住吗?
我发现了这个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) 我读过这篇文章负和正零。
根据我的理解,以下代码应该给出true 并true 作为输出。
然而,它是给予false和true作为输出。
我正在比较负零和正零。
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 ×10
java-8 ×2
arrays ×1
autoboxing ×1
concurrency ×1
entryset ×1
exception ×1
fibonacci ×1
future ×1
inheritance ×1
int ×1
iterator ×1
java-stream ×1
reflection ×1
try-catch ×1