我的情况表现非常重要.在我的算法的核心有一个方法,它使用两个double基元进行一些基本计算.每次运行该算法时,该方法被调用超过一千万次.
代码看起来像这样;
public int compare(double xA, double xB, double yA, double yB);
double x = xA * xB;
double y = yA * yB;
double diff = x - y;
return (diff < 0.0 ? -1 : (diff > 0.0 ? 1 : 0));
}
Run Code Online (Sandbox Code Playgroud)
参数xA并yA从集合中获取它们的值.这个集合可以在代码中调整.我看到很大(大约两倍)性能差异取决于我放入集合的值.似乎如果集合包含a 0.1或a 0.3,性能会受到很大影响.将设置保持为倍数0.5可以获得最佳性能.
是编译器优化x * 0.5为x >> 1等?或者这是因为0.1无法用二进制定义?
我想更好地了解这种情况,以便我可以优化它.我想这可能是一个非常难的问题,除非有人确切地知道javac和jvm(在我们的例子中是热点)如何处理双倍乘法.
假设我有一个ObjectInfo类,其中包含Object name和Object type作为String.(我只是为了提出问题而做点什么.)
class ObjectInfo {
String objectName;
String objectType;
private ObjectInfo(String objectName, String objectType) {
this.objectName = objectName;
this.objectType = objectType;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我想提供一个静态工厂方法来创建这个类的实例,以下两种方法中的哪一种更好?为什么?
public static ObjectInfo newInstance(String objectName, String objectType) {
return new ObjectInfo(objectName, objectType)
}
public static ObjectInfo valueOf(String objectName, String objectType) {
return new ObjectInfo(objectName, objectType)
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想问的是什么时候我们应该使用valueOf()&newInstance()?程序员社区中是否有任何约定?
-Ankit
我有一个问题,当我尝试将一个mouselistener添加到JTextPane中的JLabel或JButton时,我得到错误"无法通过调用转换转换为Mouselistener".我更喜欢将组件放在JEditorPane中.我还听说过可以使用HyperlinkEvent.
基本上我想要一个可以在JEditorPane(preffered)/ JTextPane中右键/左键单击的组件.任何帮助,将不胜感激
现在它工作(sortof)它只是重复右键点击,我不需要绘制按钮边缘.我可以在按钮的文字下划线吗?
示例代码如下......
import java.awt.*;
import javax.swing.*;
import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.applet.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class jlabeltest extends Applet {
public void init() {
jlabeltest editorPaneExample = new jlabeltest();
editorPaneExample.setSize(550, 300);
// editorPaneExample.setText("tutorialData.com");
editorPaneExample.setVisible(true);
}
public jlabeltest() {
JTextPane editorPane = new JTextPane();
editorPane.setSelectedTextColor(Color.red);
editorPane.setText("<p color='#FF0000'>Cool!</p>");
InlineB label = new InlineB("JLabel");
label.setAlignmentY(0.85f);
label.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e)
{
if (e.isPopupTrigger())
{
JOptionPane.showMessageDialog(null,"Hello!");
// do your work here
}
}
});
editorPane.insertComponent(label); …Run Code Online (Sandbox Code Playgroud)