Select Page

Big Java学习笔记——数值

Big Java学习笔记——数值

[danger]态度声明:本站所有原创学习类文章,只为分享学习乐趣、拓展知识广度、提供讨论平台,不以应试,实用、探讨答案为目的,本站中所有文章中的引用会要求作者尽量标出出处,所有提供下载文件均为个人整理分享学习资料,如果有版权异议,请与博主沟通。本站每学习文必置顶此项,望大家共同监督。乐于分享,支持版权,转载请列明出处,想了解更多……[/danger]


[title]数值类型[/title]

Java有8种原始的类型,4种针对整型,2种针对浮点型和char及boolean。每个不同的数值类型都有不同的数值范围,这与他们在电脑里存储占几位数据直接关联。数字number literal,如果有小数点就是floating,如果没有,则是整型。

一般来讲,因为计算造成结果数值超出范围时,这种现象叫溢出overflow,[danger]请注意,溢出是没有错误提示的。[/danger]float类型一般不会出现溢出问题,但会出现舍位问题limited precision,当一个浮点数精确表示不可能时,四舍五入错误会发生,之所以会出现以上错误,都是因为计算机是通过二进制系统显示和存储数值的,就像用十进制无法精确的描述部分分数一样。[warning]因此,一般我们进行财务等精确计算时,不用double类型,而用BigDecimal类型,同时,int可以向double转换,而double不能向int无损转换[/warning][info]一般来说,我们不用float类型,因为它只存储四位,非常容易造成舍位问题。程序员一般运用float类型,当它们需要存储不需要非常精确的大量数据时。[/info]

[title]常量constants[/title]

一般来讲,当我们需要一些在计算时发挥重要作用的不需要变化数值时,我们需要使用数值常量。一般来讲,常量尽量起一些显而易见的名称,并且全部使用大写字母,尽管他们看起来很怪很长。在Java中,声明常量使用关键字[mark]final[/mark],final常量一旦被设置将永远不改变。通常,按照惯例我们会在一个类中instance variables之前声明这些常量,并且前面加上static和final关键字。

// in a method
// final typeName variableName = expression;
final double NICKEL_VALUE = 0.05;

// in a class
// accessSpecifier static final typeName variableName = expression;
public static final double NICKEL_VALUE = 0.05;

以下是我们之前完成的收款机的收款金额的实现例子:

/**
   A cash register totals up sales and computers change due.
*/
public class CashRegister
{
   // constants deklaration
   public static final double QUARTER_VALUE = 0.25;
   public static final double DIME_VALUE = 0.1;
   public static final double NICKEL_VALUE = 0.05;
   public static final double PENNY_VALUE = 0.01;
   
   // Instanz variables
   private double perchase;
   private double payment;
   
   /**
      Constructs a cash register with no money in it
   */
   public CashRegister()
   {
      purchase = 0;
      payment = 0;
   }
   
   /**
      compute the purchase
      @param amount the price of the purchased item
   */
   public void recordPurchase(double amount)
   {
      purchase = purchase + amount;
   }
   
   /**
      Processes the payment received from the customer.
      @param dollars the number of dollars in the payment
      @param quarters the number of quarters in the payment
      @param dimes the number of dimes in the payment
      @param nickels the number of nickels in the payment
      @param pennies the number of pennies in the payment
   */
   public void receivePayment(int dollars, int quarters, int dimes, int nickels, int pennies)
   {
      payment = dollars + quarters * QUERTER_VALUE + dimes * DIME_VALUE + nickels * NICKEL_VALUE + pennies * PENNY_VALUE;
   }
   
   /**
      Computes the change due and resets the machine for the next customer.
      @return the change due to the customer
   */
   public double giveChange()
   {
      double change = payment - purchase;
      purchase = 0;
      payment = 0;
      return change;
   }
}

接下来是测试类:

/**
   This is the test class of CashRegister.
*/
public class CashRegisterTester
{
   public static void main(String[] args)
   {
      CashRegister register = new CashRegister();
   
      register.recordPurchase(0.75);
      register.recordPurchase(1.50);
      register.receivePayment(2,0,5,0,0);
      System.out.print(“Change: “);
      System.out.println(register.giveChange());
      System.out.println(“Expected: 0.25”);
   
      // test the reset funktion
      register.recordPurchase(2.25);
      register.recordPurchase(19.25);
      register.receivePayment(23,2,0,0,0);
      System.out.print(“Change: ”);
      System.out.println(register.giveChange());
      System.out.println(“Expected: 2.0”);
   }
}

最后关于大数字,也就是上面所提到的存储范围外的数字和面临舍位的浮点数的存储和计算方法我们做以简介:

import java.math;

// for the bigger int number.
// we use the intern objects in the java.math.
BigInteger n = new BigInteger(“10000000”);
// of course, we can’t use normal arithmetic operators, we have to use methods called add, subtract and multiply
BigInterger r = n.multiply(n);
System.out.println(r);// Prints 100000000000000

// For floating-point computations without roundoff errors.
BigDecimal d = new BigDecimal(“4.35”);
BigDecimal e = new BigDecimal(“100”);
BigDecimal f = d.multiply(e);
System.out.println(f); // Prints 435.00

[danger]在程序中不要出现单独并且无意义的数字,会造成别人的无法理解。[/danger]

 

About The Author

Morgen

无论任何情况下,每天都要对自己说:“Guten Morgen!”