小编Nar*_*ode的帖子

strictfp关键字在实现/扩展接口/类时的行为

JLS strictfp接口指定:

strictfp修饰符的作用是使接口声明中的所有float或double表达式都是显式FP-strict(第15.4节).

这意味着在接口中声明的所有嵌套类型都是隐式strictfp.

JLS strictfp类:

strictfp修饰符的作用是使接口声明中的所有float或double表达式都是显式FP-strict(第15.4节).

这意味着在接口中声明的所有方法以及在接口中声明的所有嵌套类型都是隐式strictfp.

从这两段开始,没有任何迹象表明strictfp在实现/扩展使用strictfp修饰符声明的接口/类时的行为.

在搜索之后,我找到了strictfp关键字用法的一个很好的解释使用strictfp修饰符来实现跨平台的浮点计算一致性,并指定:

扩展FP严格超类的子类不会继承严格的行为.当重写方法不是时,重写方法可以独立地选择FP-strict,反之亦然.

并补充说: 在此输入图像描述

strictfp在扩展使用strictfp关键字声明的类时测试了关键字的行为,这是正确的:strictfp行为不是由扩展类的类继承的,但问题是在实现用strictfp关键字声明的接口时它是不正确的:strictfp行为不是由实现的类继承的界面.

任何人都可以解释我strictfp实现/扩展使用strictfp修饰符声明的接口/类的正确行为吗?

java extends strictfp implements

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

java自动装箱/拆箱如何工作?

从JDK 5.0开始,自动装箱/拆箱是在java中引入的,技巧简单而有用,但是当我开始测试包装类和原始类型之间的不同转换时,我真的很困惑自动装箱的概念如何在java中工作,例如:

拳击

int intValue = 0;
Integer intObject = intValue;
byte byteValue = 0;
intObject = byteValue; // ==> Error
Run Code Online (Sandbox Code Playgroud)

尝试不同的情况下(后short,long,float,double),这是由编译器所接受的唯一情况是,当值的上做作运算符右侧的类型是int.当我查看我的源代码时Integer.class发现它只实现了一个带int参数的构造函数.

所以我的结论是自动装箱的概念是基于包装类中实现的构造函数.我想知道这个结论是否属实,还是有自动拳击使用的另一个概念?

拆箱

Integer intObject = new Integer(0);
byte byteValue = intObject; // ==> Error (the same Error with short)
int intValue = intObject; 
double doubleValue = intObject;
Run Code Online (Sandbox Code Playgroud)

关于拆箱的结论是包装类给出了对应的类型(Integer==> int)包装的值,然后编译器使用通常的转换基元类型的规则(byte=> short=> int=> long=> float=> double).我想知道这个结论是否属实,还是自动拆箱使用了另一个概念? …

java autoboxing unboxing

16
推荐指数
1
解决办法
9226
查看次数

Vector vs SynchronizedList性能

在阅读关于集合实现的Oracle教程时,我发现了以下句子:

如果您需要同步,则a Vector将比ArrayList同步的稍快Collections.synchronizedList

来源:列表实现

但是当搜索它们之间的差异时,许多人不鼓励使用,Vector并且应该在SynchronizedList需要同步时替换.那么哪一方有权被追随?

java collections synchronization list

7
推荐指数
1
解决办法
832
查看次数

带数组构造函数的引用方法

我尝试ArrayType[]::new在以下示例中使用带有表达式的引用方法:

public class Main
{
    public static void main(String[] args)
    {
        test1(3,A[]::new);
        test2(x -> new A[] { new A(), new A(), new A() });

        test3(A::new);
    }

    static void test1(int size, IntFunction<A[]> s)
    {
        System.out.println(Arrays.toString(s.apply(size)));
    }

    static void test2(IntFunction<A[]> s)
    {
        System.out.println(Arrays.toString(s.apply(3)));
    }

    static void test3(Supplier<A> s)
    {
        System.out.println(s.get());
    }
}

class A
{
    static int count = 0;
    int value = 0;

    A()
    {
        value = count++;
    }

    public String toString()
    {
        return Integer.toString(value);
    }
}
Run Code Online (Sandbox Code Playgroud)

产量

[null, …
Run Code Online (Sandbox Code Playgroud)

java java-8 method-reference

7
推荐指数
1
解决办法
500
查看次数

在catch块中重新抛出异常

如果这些信息是否正确,任何人都可以确认我:

在C++中,在catch块中我们可以使用throw语句重新抛出异常,但抛出的异常应该与当前捕获的异常具有相同的类型.

c++ exception throw

6
推荐指数
2
解决办法
8437
查看次数

jmap工具仅在根和输出列不清楚时起作用

在具有Oracle JDK 10.0.1-64bits的Ubuntu Mate 18.04-64bits上使用jmap时,该工具仅在同时以root身份运行目标和工具时才有效,但是使用同一普通用户运行这两者会产生以下错误:

Exception in thread "main" com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file /proc/13538/cwd/.attach_pid13538: target process 13538 doesn't respond within 10500ms or HotSpot VM not loaded
at jdk.attach/sun.tools.attach.VirtualMachineImpl.<init>(VirtualMachineImpl.java:103)
at jdk.attach/sun.tools.attach.AttachProviderImpl.attachVirtualMachine(AttachProviderImpl.java:58)
at jdk.attach/com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:207)
at jdk.jcmd/sun.tools.jmap.JMap.executeCommandForPid(JMap.java:124)
at jdk.jcmd/sun.tools.jmap.JMap.main(JMap.java:114)
Run Code Online (Sandbox Code Playgroud)

使用root用户运行以下命令时

jmap -clstats <pid>
Run Code Online (Sandbox Code Playgroud)

一切正常,但是我发现理解输出列的含义有些困难: 在此处输入图片说明 是否有任何官方文档解释每列的含义?

java jmap

5
推荐指数
1
解决办法
435
查看次数

停止Java线程的安全方法

谁能向我解释为什么第一个线程不起作用而第二个线程完美运行:

public class Test {

    public static void main(String args[]) throws InterruptedException {
        TestThread1 t1 = new TestThread1();
        TestThread2 t2 = new TestThread2();

        t1.startThread();
        t2.start();

        Thread.sleep(4000);

        t1.stopThread();
        t2.stopThread();
    }
}

class TestThread1 extends Thread {

    private volatile TestThread1 thread;

    public void startThread() {
        thread = new TestThread1();
        thread.start();
    }

    public void run() {
        while (thread != null) {
            System.out.println("RUNNING 1 ...");
        }
    }

    public void stopThread() {
        thread = null;
    }
}

class TestThread2 extends Thread {

    private volatile boolean …
Run Code Online (Sandbox Code Playgroud)

java multithreading

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