博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
String与Math类的API用法注意事项
阅读量:2056 次
发布时间:2019-04-28

本文共 3710 字,大约阅读时间需要 12 分钟。

(一)String类

(1)字符串的“==”比较

【例题】

public static void main(String[] args) {		String str = "abc";		String str1 = "abc";		String str2 = new String("abc");		System.out.println(str == str1);//true		System.out.println(str == str2);//false		str2 = str2.intern();		System.out.println(str == str2);//true	}

String存放在方法区的常量池里,String对象是被共享的,str与str1共同指向方法区里的“abc”的内存单元。str2,因为是被new出来的,所以str2指向的是堆里的内存单元,堆里开辟的单元在指向常量池。所以str2 == str 为false。

intern()方法 。如果池已经包含一个等于此 String 对象的字符串,则返回池中的字符串。否则,将此 String 对象添加到池中,并返回此 String 对象的引用。所以,此时str2的指向,已经改变了,直接指向常量池中。

(2)字符串相加

【例题】

String会产生多少个对象,StringBuilder又会产生多少个对象?

public static void main(String[] args) {		String s = "";		for (int i = 0;i < 100;i++){			s += "a";		}				StringBuilder sb = new StringBuilder();		for (int i = 0;i < 100;i++){			sb.append("a");		}		String s = sb.toString();	}

分析:

补充:Java 语言提供对字符串串联符号("+")以及将其他对象转换为字符串的特殊支持。字符串串联是通过 StringBuilder(或 StringBuffer)类及其 append 方法实现的。

1、字符串相加:以上代码,相当于以下代码

String s = "";

 for (int i = 0;i < 100;i++){

         StringBuilder str = new StringBuilder(s);

          str.append("a");

          s = str.toString();

   }

 

会产生 100个StringBuilder对象,100个“a”对象,100个toString对象,100个S对象。

其中StringBuilder的toString方法

  public String toString() {

        // Create a copy, don't share the array
        return new String(value, 0, count);
    }

所以,产生100个toString对象

2、StringBuilder相加:

只产生100个“a”对象

 

(二)Math类

(1)ceil、floor、round(面试常考)使用

ceil:向上取整

        System.out.println(Math.ceil(10.1));    //11.0

        System.out.println(Math.ceil(10.5));    //11.0
        System.out.println(Math.ceil(-10.1));    //-10.0
        System.out.println(Math.ceil(-10.5));    //-10.0

floor:向下取整

        System.out.println(Math.floor(10.1));    //10.0

        System.out.println(Math.floor(10.5));    //10.0
        System.out.println(Math.floor(-10.1));    //-11.0
        System.out.println(Math.floor(-10.5));    //-11.0

round:四舍五入

        System.out.println(Math.round(10.1));    //10

        System.out.println(Math.round(10.5));    //11
        System.out.println(Math.round(-10.1));    //-10
        System.out.println(Math.round(-10.5));    //-10
        System.out.println(Math.round(-10.7));    //-11

(2)BigInteger与BigDecimal的使用

[BigDecimal]

众所周知,大多数语言对于高精度的减法运算都会有一定的误差,Java也不例外

public static void main(String[] args) {		Double d1 = 2.99;		Double d2 = 0.01;		System.out.println(d1-d2);	}

结果:2.9800000000000004

但通过使用BigDecimal,可以杜绝这个精度的情况,不过大家应该注意的是BigDecimal传入的参数应为字符串,才可以避免精度问题。

public static void main(String[] args) {		BigDecimal big = new BigDecimal("2.99");		BigDecimal big1 = new BigDecimal("0.01");		System.out.println(big.subtract(big1));	}

add:大十进制的加法

subtract:大十进制的减法
multiply:大十进制的乘法
divide:大十进制的除法

[BigInteger]

BigInteger:用于大量数字的存储和运算。可以存放(2^26)67,108,864位

public static void main(String[] args) {		BigInteger big = new BigInteger("46547876666666666666666687999999999999999454234654864654787666666666666666668799999999999999945423465486");		BigInteger big2 = new BigInteger("46547876666666666666666687999999999999999454234654864654787666666666666666668799999999999999945423465486");		System.out.println(big.multiply(big2));	}

结果:2166704822175211111111113097153848888888838080453395398368831101685602900829979431067637589723399412298614672621555080787246832602971598060851279537468857869650278666666433806786073600002978598119557833216196

 

(3)数字格式化

0.00 0代表占位符,如果有有效数字,那么正常显示,如果没有有效数字,以0占位

public static void main(String[] args) {		DecimalFormat df = new DecimalFormat("00.00");		double d = 2.8454848;		System.out.println(df.format(d));	}

结果:02.85,保留小数点后两位

 

# 代表占位符,如果有有效数字,那么正常显示,如果没有有效数字,那么直接不显示。

public static void main(String[] args) {		DecimalFormat df = new DecimalFormat("##.##");		double d = 2.8454848;		System.out.println(df.format(d));	}

结果:2.85,保留小数点后两位

 

0.00E0 科学计数法

public static void main(String[] args) {		DecimalFormat df = new DecimalFormat("0.0E0");		double d = 28454.848;		System.out.println(df.format(d));	}

结果:2.8E4

转载地址:http://dddlf.baihongyu.com/

你可能感兴趣的文章
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【UML】《Theach yourself uml in 24hours》——hour2&hour3
查看>>
【linux】nohup和&的作用
查看>>
【UML】《Theach yourself uml in 24hours》——hour4
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【深度学习】GRU的结构图及公式
查看>>
【python】re模块常用方法
查看>>
【JavaScript】call()和apply()方法
查看>>
【JavaScript】箭头函数与普通函数的区别
查看>>
前端面试题
查看>>
【JavaScript】常用方法记录
查看>>
C++ 数据存储类型
查看>>
39. Combination Sum
查看>>
剑指Offer 1.二维数组中的查找
查看>>
剑指offer 2.重建二叉树
查看>>