引用类型常用方法

一、包装类
1、Integer
属性:
        Integer.MAX_VALUE        最大值
        Integer.MIN_VALUE        最小值
方法:
        parseInt(String str)        将字符串转换成int
        valueOf(String str)        同上
2、Double
属性:
        Double.MAX_VALUE        最大值
        Double.MIN_VALUE        最小值
方法:
        parseDouble(String str)        将字符串转换成double
        valueOf(String str)        同上
二、String类
 

str.charAt(index)                 //获取指定下标对应的字符,返回char类型
str.indexOf("字符串")        //获取指定字符串在原字符串中的下标,如果不包含该字符串则返回-1
str.lastIndexOf("字符串")         //与indexOf方法一致,区别:从后往前找
str.length()                      //获取字符串的长度
str1.equals(str2)             //判断两个字符串是否相等
str1.equalsIgnoreCase(str2)   //判断两个字符串是否相等,忽略大小写
str.isEmpty()                 //判断字符串是否为空串
str.startsWith("字符串")      //判断字符串是否以指定的字符串开头
str.startsWith("34", 2)      //判断字符串是否以指定的字符串开头,指定开始位置
str.endsWith("字符串")           //判断字符串是否以指定的字符串结尾
str.contains("字符串")        //判断字符串中是否包含自定的字符串

!!! 记得要重新赋值 !!!
str = str.concat("字符串");        //将字符串与指定的字符串进行拼接
str = str.replace("SB", "**");  //字符串替换:将字符串中指定的字符串替换成指定的字符串
str = str.substring(0, 5);    //字符串截取,从指定的下标开始和结束    范围是左闭右开
str = str.substring(6);        //字符串截取,从指定的下标开始一直到最后

String str = "zhangsan lisi wangwu";  //字符串切割,按照指定的字符串对原字符串进行切割
String[] s = str.split(" ");          

String str = "    n你好。  哈哈    ";//去除字符串前后的空格
str = str.trim();                    

byte[] b = str.getBytes();           //将字符串变成字节数组
char[] c = str.toCharArray();      //将字符串变成字符数组
str.toUpperCase()         //将字符串中的字母变成大写
str.toLowerCase()        //将字符串中的字母变成小写

int i = 10;
String s = i+"";                //方式1 将其他的类型的数据转换成String类型
String s2 = String.valueOf(i)    //方式2 

三、可变字符串 StringBuffer
append(String str)         //在字符串的后面追加字符串
delete(int start, int end)         //删除字符串,从指定的下标开始和结束
insert(int offset, String str)        //在指定下标位置添加指定的字符串
reverse()        //将字符串翻转
toString()        //将StringBuffer转换成String类型
四、Date类
 

public class DateDemo {
    public static void main(String[] args) {        
        //创建Date类的对象    默认是系统当前时间
        Date d1 = new Date();

        /**
         * year:年份,默认从1900开始计算
         * month:月份,默认是0-11
         * date:日期
         */
        Date d2 = new Date(1998-1900, 2-1, 20);        
        d1.before(d2)  //判断d1是否在d2之前      
        d1.after(d2)   //判断d1是否在d2之后
 
         d2.getTime()    //该方法返回的是自1970年1月1日00:00:00 UTC起经过的毫秒数。
    }
}

 五、SimpleDateFormat类
 


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatDemo {
    public static void main(String[] args) {
          //创建一个Date对象
          Date date = new Date();
          //创建日期格式化对象    2021年03月12日   14:15:30
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
          //调用日期格式化对象的format方法
          String time = sdf.format(date);
          System.out.println(time);  //2021年03月12日   14:15:30


          //将字符串格式的时间转换成Date类型
          String time = "2021-03-12 14:21:30";
          //创建日期格式化对象
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          //调用日期格式化对象的parse方法
          try {
              Date date = sdf.parse(time);
              System.out.println(date);
          } catch (ParseException e) {
              e.printStackTrace();
          }
    }
}

六、System类
System.currentTimeMillis()        //用于获取当前时间以毫秒为单位的时间戳。时间戳是从 1970 年                                                      1 月 1 日 00:00:00 UTC 开始到现在的毫秒数。
System.exit(0)        //用于终止当前正在运行的程序
七、Math类

public class MathDemo {
    public static void main(String[] args) {
        //求a的b次方法  参数1:底数   参数2:幂数
        System.out.println(Math.pow(2, 10));
        //求a平方根       参数1:要开方的数
        System.out.println(Math.sqrt(100));
        //求a立方根       参数1:要开立方的数
        System.out.println(Math.cbrt(27));

        //向上取整
        System.out.println(Math.ceil(10.2));
        //向下取整
        System.out.println(Math.floor(10.9));
        //四舍五入
        System.out.println(Math.round(10.5));

        //随机数 默认的范围[0,1)
        System.out.println(Math.random());
        //需求:随机一个两位数  [0,1)*90   [0,90) + 10     
        System.out.println((int)(Math.random()*90)+10);
    }
}

 八、BigDecimal类(用于精确计算,因为基本类型计算会有误差)

 

double d1 = 1.0;

double d2 = 0.9;

System.out.println(d1-d2);//0.0999999998

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值