• 31、Java高级特性——Math类、Random类、String类、StringBuffer类、StringBuilder类


    目录

    一、Math类

    1、Math类中的方法 

    1.1  圆周率:PI

    1.2   绝对值:abs()

    1.3   返回最小近似值:ceil()

    1.4  返回最大近似值

    1.5  四舍五入:round() 

    1.6  最大值和最小值:max()/min() 

    1.7  求指定次幂 :pow()

    1.8  随机数:random()

    二、Random类 

    1、Random类构造方法 

    2、Random中的方法

    3、代码演示

    4、同种子 (参数相同)

    三、String类

    1、String类的常用方法

    2、拼接字符串:concat(String string) 

    3、字符串提取和查询

    四、StringBuffer

    1、StringBuffer类的构造方法

    2、StringBuffer类常用方法

    五、StringBuilder类

    六、String类、StringBuffer类和StringBuilder类异同


    一、Math类

    Math类是被关键字final修饰的类,不能被继承

    1、Math类中的方法 

    Math类中的方法有很多,都是用于数学计算的,我们就随便看几个

    1.1  圆周率:PI

    1. public static void main(String[] args) {
    2. double pi = Math.PI;
    3. System.out.println("圆周率:"+pi);
    4. }

     

    1.2   绝对值:abs()

    1. public static void main(String[] args) {
    2. int num = Math.abs(-100);
    3. System.out.println("-100的绝对值为:"+num);
    4. }

    1.3   返回最小近似值:ceil()

    返回比参数大的最小整数

    1. public static void main(String[] args) {
    2. double ceil = Math.ceil(9.2);
    3. System.out.println("9.2的近似值为:"+ceil);
    4. }

    1.4  返回最大近似值

    返回比参数小的最大整数

    1. public static void main(String[] args) {
    2. double floor = Math.floor(9.2);
    3. System.out.println("近似值为:"+floor);
    4. }

    1.5  四舍五入:round() 

    1. public static void main(String[] args) {
    2. double round = Math.round(10.6);
    3. System.out.println("四舍五入:"+round);
    4. }

    1.6  最大值和最小值:max()/min() 

    1. public static void main(String[] args) {
    2. System.out.println(Math.max(108, 8));
    3. System.out.println(Math.min(108, 8));
    4. }

      

    1.7  求指定次幂 :pow()

    1. public static void main(String[] args) {
    2. System.out.println(Math.pow(2, 10));
    3. }

    1.8  随机数:random()

     Math.random();返回一个[0.0,1.0]之间的double类型的数据

    1. public static void main(String[] args) {
    2. double random01 = Math.random();
    3. System.out.println(random01);
    4. }

    Math.random()*10;返回一个[0.0,10.0]之间的double类型的数据 

    1. public static void main(String[] args) {
    2. double random02 = Math.random()*10;
    3. System.out.println(random02);
    4. }

    (int)(Math.random()*10);返回[0.0,10.0]之间的int类型的数据

    1. public static void main(String[] args) {
    2. int random03 = (int)(Math.random()*10);
    3. System.out.println(random03);
    4. }

    (int)(Math.random()*(num2-num1)+num1);输出num1到num2之间的整数

    1. lic class MathDemo {
    2. public static void main(String[] args) {
    3. //输出一个1-999之间的随机数
    4. int random04 = (int)(Math.random()*(998-1)+1);
    5. System.out.println(random04);
    6. }

    二、Random类 

    在java.util包中;

    此类的实例用于生成伪随机数流,就是生成随机数

    1、Random类构造方法 

    2、Random中的方法

    3、代码演示

    1. public static void main(String[] args) {
    2. //创建Random类对象
    3. Random random = new Random();
    4. //获取int类型的随机数
    5. int num1 = random.nextInt();
    6. System.out.println("int类型随机数:"+num1);
    7. //产生的数可能性为int类型的取值范围
    8. //生成指定范围的随机数
    9. int num2 = random.nextInt(10);
    10. System.out.println("0-10的随机数:"+num2);
    11. //随机boolean值
    12. System.out.println("随机boolean值:"+random.nextBoolean());
    13. }

    4、同种子 (参数相同)

    如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。为了保证此属性的实现,为类 Random 指定了特定的算法。为了 Java 代码的完全可移植性,Java 实现必须让类 Random 使用此处所示的所有算法。但是允许 Random 类的子类使用其他算法,只要其符合所有方法的常规协定即可。

    1. public static void main(String[] args) {
    2. //如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列
    3. Random r1 = new Random(100);
    4. Random r2 = new Random(100);
    5. System.out.println(r1.nextInt());
    6. System.out.println(r2.nextInt());
    7. }

    三、String类

    我们在前面已经对String类已经有过了解,今天我们看一下String类中常用方法

    1、String类的常用方法

    (1)获取字符串长度:length();

    (2)获取指定索引的字符:charAt();

    (3)字符串的比较:equals();

    (4)字符串大写转小写:toLowerCase();

    (5)字符串小写转大写:toUpperCase();

    1. public static void main(String[] args) {
    2. //创建String类对象
    3. String str1 = new String("yfgtetwyubfuy");
    4. String str2 = "fhubuyfd";
    5. //获取字符串的长度:length()
    6. int lengthStr1 = str1.length();
    7. System.out.println("字符串str1的长度为:"+lengthStr1);
    8. int lengthStr2= str2.length();
    9. System.out.println("字符串str2的长度为:"+lengthStr2);
    10. System.out.println("**********************************************************");
    11. //获取指定索引的字符:charAt()
    12. char str1Char = str1.charAt(5);
    13. System.out.println("字符串str1的第六个字符是:"+str1Char);
    14. System.out.println("字符串str2的第三个字符是:"+str2.charAt(2));
    15. System.out.println("**********************************************************");
    16. //字符串的比较:equals()
    17. boolean result1 = str1.equals(str2);
    18. System.out.println("字符串str1与str2是否相同:"+result1);
    19. System.out.println("字符串str2是否与fhubuyfd相同:"+str2.equals("fhubuyfd"));
    20. System.out.println("**********************************************************");
    21. //字符串大写转小写:toLowerCase();
    22. String str3 = "JDIUGNRLasd";
    23. String str4 = str3.toLowerCase();
    24. System.out.println("将字符串str3转为小写为:"+str4);
    25. System.out.println("**********************************************************");
    26. //字符串小写转大写:toUpperCase();
    27. String str5 = "keriolkJUHNU";
    28. String str6 = str5.toUpperCase();
    29. System.out.println("将字符串str5转换大写:"+str6);
    30. System.out.println("**********************************************************");
    31. }

    2、拼接字符串:concat(String string) 

    第一种方式:

    使用拼接符号"+"拼接

    第二种方式:

    使用concat(String string)方法

    1. public static void main(String[] args) {
    2. String str1 = "qwerty";
    3. String str2 = "asdfghj";
    4. //使用"+"实现拼接
    5. //输出[q,w,e,r,t,y]
    6. for(int i = 0;i
    7. if(i == 0){
    8. System.out.print("["+str1.charAt(i)+",");
    9. }else if(i > 0 && i 1){
    10. System.out.print(str1.charAt(i)+",");
    11. }else{
    12. System.out.println(str1.length()+"]");
    13. }
    14. }
    15. System.out.println("**********************************************************");
    16. //调用concat(String string)实现拼接
    17. String str3 = str1+"nfdiugnriun";
    18. System.out.println(str3);
    19. String str4 = str1.concat(str2);
    20. System.out.println(str4);
    21. }

    1. public static void main(String[] args) {
    2. System.out.println(3+5+"hello");
    3. System.out.println(3+"hello"+5);
    4. System.out.println("hello"+3+5);
    5. System.out.println(3+""+5+"hello");
    6. }

    程序不仅是从上到下执行,还要遵循从左到右执行 

    3、字符串提取和查询

    1. public static void main(String[] args) {
    2. String str1 = "pneumonoultramicroscopicsilicovolcanoconiosis";
    3. //返回第一个o出现的位置
    4. int numStr1 = str1.indexOf(111);
    5. System.out.println("o在str1中第一次出现的位置(索引):"+numStr1);
    6. System.out.println("**********************************************************");
    7. //返回最后一个o的索引
    8. int numStr2 = str1.lastIndexOf(111);
    9. System.out.println("o在str1中最后出现的位置(索引):"+numStr1);
    10. System.out.println("**********************************************************");
    11. //返回第一次出现字符串co的索引
    12. int numStr3 = str1.indexOf("co");
    13. System.out.println("co在str1中首次出现的位置:"+numStr3);
    14. System.out.println("**********************************************************");
    15. //返回最后一次出现字符串co的索引
    16. int numStr4 = str1.lastIndexOf("co");
    17. System.out.println("co在str1中最后一次出现的位置:"+numStr4);
    18. System.out.println("**********************************************************");
    19. //返回第十个字符后的所有字符
    20. String str2 = str1.substring(10);
    21. System.out.println("第十个字符后的字符串:"+str2);
    22. System.out.println("**********************************************************");
    23. //返回第2到第20个字符
    24. String str3 = str1.substring(1,20);
    25. System.out.println("第2到第20的字符串为:"+str3);
    26. System.out.println("**********************************************************");
    27. //去除字符串前后的空格
    28. String str4 = " qwer df ";
    29. String str5 = str4.trim();
    30. System.out.println("字符串str4去除空格后为"+str5);
    31. }

    4、String类实用方法

     

    1. public static void main(String[] args) {
    2. String str1 = "长亭外 古道边 芳草碧连天 晚风拂 柳笛声残 夕阳山外山";
    3. //String[] split(String regex) 根据给定正则表达式的匹配拆分此字符串
    4. //按空格来拆分字符串,返回的是一个字符数组
    5. String[] strs1 = str1.split(" ");
    6. for(String string:strs1){
    7. System.out.println(string);
    8. }
    9. System.out.println("**********************************************************");
    10. //boolean endsWith(String suffix)测试此字符串是否以指定的后缀结束
    11. String str2 = "qwertyuiop";
    12. boolean result1 = str2.endsWith("iop");
    13. System.out.println("字符串str2是否以iop结尾?"+result1);
    14. boolean result2 = str2.endsWith("iops");
    15. System.out.println("字符串str2是否以iops结尾?"+result2);
    16. System.out.println("**********************************************************");
    17. // boolean startsWith(String prefix,int toffset)测试此字符串从指定索引开始的子字符串是否以指定前缀开始
    18. String str3 = "asdfghjkl";
    19. boolean result3 = str3.startsWith("asd");
    20. System.out.println("字符串str3是否以asd开头:"+result3);
    21. boolean result4 = str3.startsWith("fgh",3);
    22. System.out.println("字符串str3是否第四位开始是以fgh开头:"+result4);
    23. System.out.println("**********************************************************");
    24. //byte[] getBytes()使用平台的默认字符集将此String编码为byte序列,并将结果存储到一个新的byte数组中
    25. String str4 = "zxcvbnm";
    26. byte[] bytes = str4.getBytes();
    27. for(byte strBytes:bytes){
    28. System.out.print(strBytes+"\t");
    29. System.out.println((char)strBytes);
    30. }
    31. System.out.println("**********************************************************");
    32. //char[] toCharArray() 将此字符串转换为一个新的字符数组
    33. String str5 = "123456789";
    34. char[] chars = str5.toCharArray();
    35. for(char strChar:chars){
    36. System.out.print(strChar+"\t");
    37. }
    38. }

    四、StringBuffer

    StringBuffer类位于java.util包中,是String类的增强类。是线程安全的可变字符序列。一个类似于String的字符串缓冲区,但不能修改。

    1、StringBuffer类的构造方法

    2、StringBuffer类常用方法

    append(Type type):将字符串表示形式添加到序列

    StringBuffer delete(int start, int end):移除此序列的子字符串中的字符

    insert(int offset,Type type):插入

    reverse():字符串反转

    1. public static void main(String[] args) {
    2. //创建StringBuffer类对象
    3. StringBuffer sb01 = new StringBuffer("qwertyuiop");
    4. //append(Type type)将字符串表示形式添加到序列
    5. StringBuffer sb02 = sb01.append("asdfg");
    6. System.out.println("sb01:"+sb01);
    7. System.out.println("sb02:"+sb02);
    8. sb01.append(100);
    9. System.out.println("sb01:"+sb01);
    10. System.out.println("sb02:"+sb02);
    11. System.out.println("**********************************************************");
    12. // StringBuffer delete(int start, int end)移除此序列的子字符串中的字符。
    13. sb02.delete(2,5);
    14. System.out.println("sb01:"+sb01);
    15. System.out.println("sb02:"+sb02);
    16. System.out.println("**********************************************************");
    17. //insert(int offset,Type type)插入
    18. sb02.insert(3, 456);
    19. System.out.println("sb01:"+sb01);
    20. System.out.println("sb02:"+sb02);
    21. System.out.println("**********************************************************");
    22. //reverse():字符串反转
    23. sb01.reverse();
    24. System.out.println("sb01:"+sb01);
    25. System.out.println("sb02:"+sb02);
    26. }

    五、StringBuilder类

    (1)java.lang.StringBuilder是JDK 5.0版本新增的类,它是一个可变的字符序列。

    (2)一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。

    StringBuilder 上的主要操作是 appendinsert 方法,可重载这些方法,以接受任意类型的数据。每个方法都能有效地将给定的数据转换成字符串,然后将该字符串的字符追加或插入到字符串生成器中。append 方法始终将这些字符添加到生成器的末端;而 insert 方法则在指定的点添加字符。

    (3)使用StringBuilder类处理字符串的方法与StringBuffer类基本一样。

    六、String类、StringBuffer类和StringBuilder类异同

    (1)String类:字符串常量

     String是不可变的对象,在每次对String类型进行改变时其实都等同于生成了一个新的String对象,然后指向新的String对象,所以经常改变内容的字符串最好不要用String类型,因为每次生成对象都会对系统性能产生影响。

    (2)StringBuffer类:字符串变量

    StringBuffer是可变的字符串,在每次对StringBuffer对象进行改变时,会对StringBuffer对象本身进行操作,而不是生成新的对象,再改变对象引用。所以,在字符串对象经常改变的情况下,推荐使用StringBuffer类。

    (3)StringBuilder类:字符串变量

     JDK 5.0版本以后提供了StringBuilder类,它和StringBuffer类等价,区别在于StringBuffer类是线程安全的,StringBuilder类是单线程的,不提供同步,理论上效率更高。

     

     

     

     

  • 相关阅读:
    C++基础——模板讲解
    SSM+中小型企业绩效管理系统毕业设计-附源码081536
    RAID卡管理工具使用
    Hexagon_V65_Programmers_Reference_Manual(48)
    [笔记]快乐的Linux命令行《二》文件系统中跳转
    代码随想录刷题| 01背包理论基础 LeetCode 416. 分割等和子集
    教你自己搭建一个IP池(绝对超好用!!!!)
    C++文件服务器项目—FastCGI—4
    万有引力的几个结论证明
    微信小程序购物/超市/餐饮/酒店商城开发搭建过程和需求
  • 原文地址:https://blog.csdn.net/CSDN_Loveletter/article/details/127965123