Double.parseDouble和Double.valueOf的区别

先来看下两个得源码

valueOf方法

public static Double valueOf(String s) throws NumberFormatException {
        return new Double(parseDouble(s));
    }
parseDouble方法
public static double parseDouble(String s) throws NumberFormatException {
        return FloatingDecimal.parseDouble(s);
    }

关于parseDouble方法种用到得FloatingDecimal.parseDouble()方法代码太长我就不贴了。。有兴趣的可以自己去看一下

可以看出来,valueOf本质上还是调用的parseDouble方法,parseDouble()将String类型转换为double类型,valueOf再将其装箱。

 

@Override public void actionPerformed(ActionEvent e) {//触发此动作时执行 for (int i = 0; i < 10; i++) { if (e.getSource() == numberButtons[i]) {//返回最初发生 Event 的对象,那个对象被触发,就返回这个对象 textField.setText(textField.getText().concat(String.valueOf(i))); } } if (e.getSource() == decButton) { textField.setText(textField.getText().concat(".")); } if (e.getSource() == addButton) { num1 = Double.parseDouble(textField.getText()); operator = '+'; textField.setText(""); } if (e.getSource() == subButton) { num1 = Double.parseDouble(textField.getText()); operator = '-'; textField.setText(""); } if (e.getSource() == mulButton) { num1 = Double.parseDouble(textField.getText()); operator = '*'; textField.setText(""); } if (e.getSource() == divButton) { num1 = Double.parseDouble(textField.getText()); operator = '/'; textField.setText(""); } if (e.getSource() == equButton) { num2 = Double.parseDouble(textField.getText()); switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; } textField.setText(String.valueOf(result)); num1 = result; } if (e.getSource() == clrButton) { textField.setText(""); } if (e.getSource() == delButton) { String string = textField.getText(); textField.setText(""); for (int i = 0; i < string.length() - 1; i++) { textField.setText(textField.getText() + string.charAt(i)); } } } }请注释出每一步在做什么
06-02
package 界面; import java.awt.*; import java.awt.event.*; import javax.swing.*; class SimpleComputeDemo4 { JTextField firstTF,secondTF,resultTF; JButton SZHButton,HZSButton,clearButton; public void init() { JFrame f=new JFrame("温度转换"); f.setSize(400,200); f.setVisible(true); JPanel p=new JPanel(); JLabel firstLabel=new JLabel("摄氏度"); JLabel secondLabel=new JLabel("华氏度"); JLabel thirdLabel=new JLabel("结果"); firstTF=new JTextField(10); secondTF=new JTextField(10); resultTF=new JTextField(10); SZHButton=new JButton("摄转华"); HZSButton=new JButton("华转摄"); clearButton=new JButton("清除"); f.add(p); p.setLayout(null); firstLabel.setBounds(20,10,80,30); firstTF.setBounds(120,10,120,30); secondLabel.setBounds(20,50,80,30); secondTF.setBounds(120,50,120,30); thirdLabel.setBounds(20,90,80,30); resultTF.setBounds(120,90,120,30); SZHButton.setBounds(20,170,80,30 ); HZSButton.setBounds(120,170,80,30); clearButton.setBounds(220,170,60,30); p.add(firstLabel); p.add(firstTF); p.add(secondLabel); p.add(secondTF); p.add(thirdLabel); p.add(resultTF); p.add(SZHButton); p.add(HZSButton); p.add(clearButton); SZHButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { double first,second,result=0; first=Double.parseDouble(firstTF.getText()); second=Double.parseDouble(secondTF.getText()); result=9*first/5+32; resultTF.setText(String.valueOf(result)); } }); HZSButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { double first,second,result=0; first=Double.parseDouble(firstTF.getText()); second=Double.parseDouble(secondTF.getText()); result=5*(second-32)/9; resultTF.setText(String.valueOf(result)); } }); clearButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { firstTF.setText(null); secondTF.setText(null); resultTF.setText(null); } }); } } class TestSimpleComputeDemo { public static void main(String args[]) { new SimpleComputeDemo4().init(); } }给这段代码加详细的注释
05-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值