炫酷企业网站,网络营销具有很强的什么特征,如何查询网站备案信息,可信验证网站1 java的允许函数的默认参数吗?java不支持类似C那样#xff0c;为函数设定默认参数#xff0c;所以需要做的事情是#xff0c;自己用函数重载的方式进行模拟。如下public class FFOverload {public String getName(String givenName,String familyName){return givenName那样为函数设定默认参数所以需要做的事情是自己用函数重载的方式进行模拟。如下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 demoDefaultParanew FFOverload();System.out.println(demoDefaultPara.getName(AAA));System.out.println(demoDefaultPara.getName(AAA, CCC));}}输出:AAA.BBBAAA.CCC2. 为什么floating point number不准确首先来验证一下public class FloatTest {public static void main(String[] args) {float a 1.01f; //dont 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 anda mantissa (also called the significand).A very simple number, say 9.2, is actually this fraction:5179139571476070 * 2 -49Where 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 Classbut 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.521Big Decimal is 2.522why???? I expected:2.5212.522HALF_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.521Big Decimal is 2.5223.2 如果在Double和Float中二选一选择Double.Double (8 位)Float (4 位)