java default parameter_JAVA菜鸟入门(7) default parameter , float/double vs BigDecimal

1  java的允许函数的默认参数吗?

java不支持类似C++那样,为函数设定默认参数,所以需要做的事情是,自己用函数重载的方式进行模拟。如下

public class FFOverload {

public String getName(String givenName,String familyName){

return givenName+"."+familyName;

}

public String getName(String givenName){

return getName(givenName,"BBB");

}

public static void main(String[] args) {

FFOverload demoDefaultPara=new FFOverload();

System.out.println(demoDefaultPara.getName("AAA"));

System.out.println(demoDefaultPara.getName("AAA", "CCC"));

}

}

输出:

AAA.BBB

AAA.CCC

2.  为什么floating point number不准确

首先来验证一下

public class FloatTest {

public static void main(String[] args) {

float a = 1.01f; //don't forget the trailing 'f' , else it will be treated as a double.

float b = 1.002f;

float c = 1.0000009f;

float d = a + b + c;

System.out.println("expected: 3.0120009, actua: "+ d);

}

}

输出:

expected: 3.0120009, actua: 3.012001

然后来看看原因:

In most programming languages, floating point numbers are represented a lot like scientific notation:

with an exponent and

a mantissa (also called the significand).

A very simple number, say 9.2, is actually this fraction:

5179139571476070 * 2 -49

Where the exponent is -49 and the mantissa is 5179139571476070.

The reason it is impossible to represent some decimal numbers this way is that both the exponent and the mantissa must be integers. In other words, all floats must be an integer multiplied by an integer power of 2.

Floating point numbers only have 32 or 64 bits of precision, so the digits are cut off at some point, and the resulting number is 0.199999999999999996 in decimal, not 0.2.

3. 如何尽可能准确地表示Floating Point Numbers?

3.1 使用BigDecimal Class

but currently there is a small unsolved issue in the code below. I did not see the difference between HALF_UP and HALF_DOWN in the code below.

import java.math.*;

public class FFBigDecimal {

public static void main(String[] args) {

BigDecimal bigDecimal1 = new BigDecimal(1.001);

BigDecimal bigDecimal2 = new BigDecimal(1.0005);

BigDecimal bigDecimal3 = new BigDecimal(1.000007);

BigDecimal bigDecimaBase = new BigDecimal(2.52150);

BigDecimal bigDecimal4 = bigDecimaBase.setScale(3, RoundingMode.HALF_DOWN);

System.out.println("Big Decimal is " + bigDecimal4.toString());

BigDecimal bigDecimal5 = bigDecimaBase .setScale(3, RoundingMode.HALF_UP);

System.out.println("Big Decimal is " + bigDecimal5.toString());

}

}

输出

Big Decimal is 2.521

Big Decimal is 2.522

why????  I expected:

2.521

2.522

HALF_UP的定义是这样的:

“Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for RoundingMode.UP if the discarded fraction is ≥ 0.5; otherwise, behaves as for RoundingMode.DOWN. Note that this is the rounding mode commonly taught at school.”

后来终于知道了原因,算是比较坑爹了。

BigDecimal的构造函数需要用String作为参数,否则将会出现一些比较奇怪的结果。所以上面的程度如果修改为:

import java.math.*;

public class FFBigDecimal {

public static void main(String[] args) {

BigDecimal bigDecimal1 = new BigDecimal("1.001");

BigDecimal bigDecimal2 = new BigDecimal("1.0005");

BigDecimal bigDecimal3 = new BigDecimal("1.000007");

//test 1

//bigDeciimal3 is immutable, so

// WRONG: bigDecimal3.add(bigDecimal1).add(bigDecimal2);

// CORRECT: bigDecimal3 = bigDecimal3.add(bigDecimal1).add(bigDecimal2);

bigDecimal3 = bigDecimal3.add(bigDecimal1).add(bigDecimal2);

BigDecimal bigDecimaBase = new BigDecimal("2.52150");

BigDecimal bigDecimal4 = bigDecimaBase.setScale(3, RoundingMode.HALF_DOWN);

System.out.println("Big Decimal is " + bigDecimal4.toString());

BigDecimal bigDecimal5 = bigDecimaBase .setScale(3, RoundingMode.HALF_UP);

System.out.println("Big Decimal is " + bigDecimal5.toString());

}

}

就是符合预期的,得到的输出结果将是:

Big Decimal is 2.521

Big Decimal is 2.522

3.2 如果在Double和Float中二选一,选择Double.

Double (8 位)

Float (4 位)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541872.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

gitlab修改默认端口

部署gitlab的时候,一启动,发现80和8080端口已经被占用,无奈,只得先将监听80端口的nginx和监听8080端口的jenkins停止。这会儿有空,琢磨一下如何修改gitlab的默认端口。 修改主要分为两部分,一部分是gitlab总…

Java ObjectOutputStream reset()方法与示例

ObjectOutputStream类reset()方法 (ObjectOutputStream Class reset() method) reset() method is available in java.io package. reset()方法在java.io包中可用。 reset() method is used to reset this stream. It reset the stream to the position marked most recently. …

Excel 自定义关闭按钮

遇到过这样一个需求,是在excel关闭的时候,不要excel本身的保存窗口,只用自定义的. 这个的需要第一,是点击关闭时候触发, 第二;触发后,不能还是弹出那个窗口 第三:取消后,…

Java OutputStreamWriter close()方法与示例

OutputStreamWriter类close()方法 (OutputStreamWriter Class close() method) close() method is available in java.io package. close()方法在java.io包中可用。 close() method is used to first flush before closing the stream and the method write() or flush() invok…

深入理解Netscaler INat

深入理解Netscaler INatNetscaler的INat主要是用作基于目的地址的转换,将client访问的公网IP通过Netscaler转换成服务器的私网IP,与DNAT作用类似。由于Netscaler默认的工作机制就是同时做源IP:【源端口】目的IP:【目的端口】的转换…

java 方法 示例_Java语言环境getDisplayCountry()方法与示例

java 方法 示例区域设置类getDisplayCountry()方法 (Locale Class getDisplayCountry() method) Syntax: 句法: public final String getDisplayCountry();public String getDisplayCountry(Locale lo);getDisplayCountry() method is available in java.util pack…

格力电器Java面试题_JAVA设计模式学习--工厂模式

今天谈一下对工厂模式学习的总结。看完视频和文章之后要自己表述工厂模式,总是感觉无从说起,不知道怎么去定义工厂模式,反复看了几遍之后终于理解一点。自己理解工厂模式是通过这两种模式的特点来理解和定义的,首先工厂模式有简单…

为什么玩我的世界老提示Java se错误_我的世界error错误信息 error could解决方法

我的世界是一个及其开放的沙盒游戏,而在这个游戏中有不少的问题,比如说遇到error该如何解决呢,看小编给大家带来的我的世界error错误的解决方法,希望大家喜欢。error应用程序错误信息。包括“Error:Unable to access jarfile mcpc…

Tomcat 服务器只能存有一个正在运行的项目

即使新建了一个new project (在同一个工作空间),启动Tomcat 还是会出现先前(工程名)一样的问题/异常。 【原因】: 在底下Server 那里——Tomcat 7.X 底下会有很多工程名,它会纪录!所…

Java Collections singletonMap()方法与示例

集合类singletonMap()方法 (Collections Class singletonMap() method) singletonMap() method is available in java.util package. singletonMap()方法在java.util包中可用。 singletonMap() method is used to return an immutable map (i.e. immutable map is a map that c…

java访问登录网页_===java怎样访问需要登录才能查看的网页????急!!===...

java中可以用java.net包下的东西访问网页,但是有的网页要求用户先输入用户名和密码才能查看,这些网页java怎么访问呢???注意:我说的要输入用户名和密码不是浏览器弹出一个框的那种,而是象csdn这…

javascript OOP(下)(九)

一、javascript模拟重载 java中根据参数类型和数量的区别来实现重载&#xff0c;javascript弱类型&#xff0c;没有直接的机制实现重载&#xff0c;javascript中参数类型不确定和参数个数任意&#xff0c;通过判断实际传入的参数的个数来实现重载。 <script> function Pe…

java calendar_Java Calendar getDisplayNames()方法与示例

java calendar日历类的getDisplayNames()方法 (Calendar Class getDisplayNames() method) getDisplayNames() method is available in java.util package. getDisplayNames()方法在java.util包中可用。 getDisplayNames() method is used to return Map that contains all fie…

Linux如何查找大文件或目录总结

转载&#xff1a;http://www.cnblogs.com/kerrycode/p/4391859.html 在Windows系统中&#xff0c;我们可以使用TreeSize工具查找一些大文件或文件夹&#xff0c;非常的方便高效&#xff0c;在Linux系统中&#xff0c;如何去搜索一些比较大的文件呢&#xff1f;下面我整理了一下…

java编写简单邮件_Javamail,编写简单的程序发送邮件

代码&#xff1a;package com.dai.mail; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.In…

java calendar_Java Calendar getLeastMaximum()方法与示例

java calendarCalendar类的getLeastMaximum()方法 (Calendar Class getLeastMaximum() method) getLeastMaximum() method is available in java.util package. getLeastMaximum()方法在java.util包中可用。 getLeastMaximum() method is used to get the least maximum value …

Shell 标准输入、输出和错误

防伪码&#xff1a;桃花潭水深千尺&#xff0c;不及汪伦送我情。文件描述符&#xff08;fd&#xff09;&#xff1a;文件描述符是一个非负整数&#xff0c;在打开现存文件或新建文件时&#xff0c;内核会返回一个文件描述符&#xff0c;读写文件也需要使用文件描述符来访问文件…

java需要会的工具_Java开发者必备的几款工具,一定要掌握!

原标题&#xff1a;Java开发者必备的几款工具&#xff0c;一定要掌握&#xff01;NotepadNotepad是用于编辑xml、脚本以及记笔记的最佳工具。这个工具的最好部分在于&#xff0c;你在Notepad上打开的任何一个文档&#xff0c;在关闭后都会有一个残留文档&#xff0c;它有助于在…

java的equals方法_Java LocalDateTime类| 带示例的equals()方法

java的equals方法LocalDateTime类equals()方法 (LocalDateTime Class equals() method) equals() method is available in java.time package. equals()方法在java.time包中可用。 equals() method is used to check whether this date-time and the given object are equal or…

portlet java_Java Portlet 规范概述

前言1、portlet是一种类似servlet的规范。2、servlet是web组件&#xff0c;portlet也是web组件。参考1、百度百科&#xff1a;portlethttp://baike.baidu.com/link?urlvMzVwpkf5WzOL23GLkgM4C5C7Sarqh1XXShS73L7k-MbGgM0ooZ4Dl2Efor3bb4tZmmLo6v-muG5UW7_CYMTUahttp://hintcnu…