常用类总结

这些常用类知道大概的方法,到时候再查看即可,多用就会记得了。

包装类

八个基本类型对应的类就是包装类。让基本类型作为对象来处理,有相应的属性和方法。

基本类型,字符串,包装类之间互相转变。

image

//基本数据类型转化包装类对象
Integer a = new Integer(3);
Integer b = Integer.valueOf(20);
//包装类对象转化成基本数据类型
int c = b.intValue();
double d = b.doubleValue();
//字符村转成包装类对象
Integer e = new Integer("55");
Integer f = Integer.parseInt("22");
//包装类对象转化成字符串
String s = b.toString();//""+b
//基本数据类型转化字符串
String s1 = 1+"";
//字符村转化基本数据类型
int g = Integer.parseInt(s1);
//常用的常量
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);

自动拆箱装箱

Integer a = 123;//自动装箱  Integer a = Integer.valueOf(123);
int b = a;//自动拆箱    int b = a.intValue();

Integer c = null;
if(c!=null){
    int d = c;//不加判断会出现空指针异常。
}

Integer缓存机制

对于Integer来说,在[-128,127]之间的数据自动生成Integer对象。而在此范围外的则生成新的Integer对象。

== 是对象比较

equals 是值比较值

String类

不可变字符序列

String的比较不能用==(对象的比较),而是用equals()比较。

  1. String类的下述方法能创建并返回一个新的String对象: concat() replace()substring()toLowerCase()toUpperCase()trim()

  2. 提供查找功能的有关方法: endsWith()、 startsWith()、 indexOf()、lastIndexOf()

  3. 提供比较功能的方法: equals()、equalsIgnoreCase()、compareTo()

  4. 其它方法: charAt() 、length()

StringBuilder

可变字符序列

线程不安全,效率高,一般使用这个,

StringBuffer,线程安全,效率低。

方法

· 常用方法列表:

  1. 重载的public StringBuilder append(…)方法

​ 可以为该StringBuilder 对象添加字符序列,仍然返回自身对象。

  1. 方法 public StringBuilder delete(int start,int end)

​ 可以删除从start开始到end-1为止的一段字符序列,仍然返回自身对象。

  1. 方法 public StringBuilder deleteCharAt(int index)

​ 移除此序列指定位置上的 char,仍然返回自身对象。

  1. 重载的public StringBuilder insert(…)方法

​ 可以为该StringBuilder 对象在指定位置插入字符序列,仍然返回自身对象。

  5. 方法` public StringBuilder reverse()`

​ 用于将字符序列逆序,仍然返回自身对象。

  1. 方法public String toString()返回此序列中数据的字符串表示形式。

  2. 方法setCharAt(index,char)替换字符

  3. 和 String 类含义类似的方法:

    public int indexOf(String str)
    public int indexOf(String str,int fromIndex)
    public String substring(int start)
    public String substring(int start,int end)
    public int length() 
    char charAt(int index)
    

注意

不能使用这种方法
for (int i = 0; i < 5000; i++) {
            str8 = str8 + i;//相当于产生了10000个对象
        }
必须使用这种方法
StringBuilder sb1 = new StringBuilder("");
     for (int i = 0; i < 5000; i++) {
          sb1.append(i);
     }

时间相关类关系图

image

Date类

获取当前时间

long now = System.currentTimeMillis();
Date d = new Date();//获取当前时间
long time = d.getTime();

DateFormate和SimpleDateFormate

String和Date转换

//将时间按照指定格式转化为字符串
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String str = df.format(new Date());
System.out.println(str);
//将指定格式字符串转化为时间类
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = df2.parse("2021-03-10 09:11:03");//需要处理异常
System.out.println(date);

也可以处理,当前是第几周,第几天

image

Calendar日历类GregorianCalendar

用于与日历相关的对象

//获得日期相关元素
Calendar calendar = new GregorianCalendar(2010,1,2,4,5,6);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);//0-11 1月-12月
int day = calendar.get(Calendar.DATE);
int weekday = calendar.get(Calendar.DAY_OF_WEEK);//1-7  周日-周六

//设置日期
Calendar c1 = new GregorianCalendar();//默认是当前日期
c1.set(Calendar.YEAR,2022);
c1.set(Calendar.MONTH,11);
c1.set(Calendar.DATE,31);


//计算日期
Calendar c2 = new GregorianCalendar();//默认是当前日期
c2.add(Calendar.DATE,100);

//日期对象和时间对象转化
Calendar c3 = new GregorianCalendar();//默认是当前日期
Date time = c3.getTime();
Calendar c4 = new GregorianCalendar();//默认是当前日期
c4.setTime(new Date());

Math类方法

  1. abs 绝对值

  2. acos,asin,atan,cos,sin,tan 三角函数

  3. sqrt 平方根

  4. pow(double a, double b) a的b次幂

  5. max(double a, double b) 取大值

  6. min(double a, double b) 取小值

  7. ceil(double a) 大于a的最小整数

  8. floor(double a) 小于a的最大整数

  9. random() 返回 0.0 到 1.0 的随机数

  10. long round(double a) double型的数据a转换为long型(四舍五入)

  11. toDegrees(double angrad) 弧度->角度

  12. toRadians(double angdeg) 角度->弧度

File类用法

java.io.File类:代表文件和目录。 在开发中,读取文件、生成文件、删除文件、修改文件的属性时经常会用到本类。

File可以代表文件,文件夹,或者不存在的路径。

image
image

posted @ 2021-05-12 20:09  我来自火星  阅读(41)  评论(0)    收藏  举报