API常用类、基本数据类型包装类、Object、Arrays类、String类/StringBuffer类/StringBuilder类、Math类/Random、Date类/Calendar类

API常用类

1.Java API 概述

1.API(Application Programming Interface)应用程序编程接口。

​ 对Java对类或接口的功能和函数功能的说明文档,一般我们所说的api是指api文档,供开发人员学习。

2.基本数据类型包装类

1.Java是面向对象的语言,但是Java中的基本数据类型不面向对象,使用时有很多不便,为了解决这个不足,在设计时为每个基本数据类型设计了一个对应的类进行代表。八个基本数据类型对应的类统称为包装类

2.具体有以下八个封装类:

基本数据类型 包装类

在这里插入图片描述

​ 对于包装类,用于类的用途主要包含两种:

​ 1.作为和基本数据类型对应的类型存在。

​ 2.包含每种基本数据类型的相关属性如最大值、最小值等相关操作。

包含类常用属性方法:

​ 以下方法java.lang.Integer为例

​ public static final int MAX_VALUE最大的int型数

​ public static final int MIN_VALUE 最小的int型数

构造方法:

​ Integer(int a);

​ Integer(String a);

比较方法

​ static int compareTo(Integer a);

​ boolean equals(Object);

​ int max(int a,int b);

​ int min(int a,int b);

转换方法

​ static toBinaryString(int i);

​ static String toHexString(int i);

​ static String toOctalString(int i);

​ int intValue();

​ static int parseInt(String s);

​ String toString();

​ static Integer valuesof(int i)

​ static Integer valuesOf(String s)

public class IntDemo {
    public static void main(String[] args) {
        int a = 101;
        Integer b = new Integer(10);
        int b1 = b.intValue();  //将10从包装类中取出来
        System.out.println(b.byteValue());
        System.out.println(b1);
        System.out.println(Integer.MAX_VALUE); //int能取到的最大值
        System.out.println(Integer.MIN_VALUE); //int能渠道的最小值
        System.out.println(Integer.SIZE);
        System.out.println(Integer.BYTES); //字节
        System.out.println(Integer.toBinaryString(a)); //转换为二进制
        System.out.println(Integer.toOctalString(a));  //转换为八进制
        System.out.println(Integer.toHexString(a));    //转换为十六进制
        System.out.println(b.compareTo(a)); //比较 第一个值大返回1 相等返回0 第二个值大返回-1
        System.out.println(b.equals(b1));
    }
}

装箱和拆箱

  • 装箱:1.自动将基本数据类型转换为包装器类型

​ 2.装箱的时候自动调用的时Integer的valueOf(int) 方法

  • 拆箱: 1.自动将包装器类型转换为基本数据类型

​ 2.拆箱的时候自动调用的Integer和intValue方法

//装箱:
	int a = 12;
	Integer b = Integer.valueOf(a);
//拆箱
	int c = b.intValue();

int a = 12; 
Integer b = a; //自动装箱
int c = b;	   //自动拆箱

3.Object

​ 1.Ojbect类是所有Java类的祖先。每个类都使用Object作为超类。所有对象(包括数组)都实现这个类的方法。

​ 2.如果在类中未使用extends关键词指明基类,则默认基类为object类

public class Person{    }
等同于
public class Person extends Object{}

1)toString方法

​ 1.Object类中定义有public String toString()方法,其返回值是String类型,描述当前对象的有关信息。

​ 2.在进行String与其他类型数据的连接操作时自动调用toString()方法。

​ 例如:

System.out.println("info"+person)

可以根据需求自定义类型中重写toString()方法。

2)equals

​ Object类中定义有:

​ public boolean equals(Object obj)方法

​ 提供定义对象是否"相等"的逻辑

​ Object的equals方法定义为:x.equals(y),当x和y是同一个对象的引用时返回true否则false

​ JDK提供一些类,如String,Date等,重写了Object的equals方法,调用这些类的equals方法,x.equals(y),当x和y

public class StringDemo1 {
    public static void main(String[] args) {
        String [] a0 = {"abcdef"};
        String [] a1 = {"abcdef"};
        System.out.println(a0.equals(a1));

        System.out.println(a0.length);
        
    }
}

4.Arrays类

​ java.util.Arrays类

​ 用于操作数组工具类,里面定义了常见操作数组的静态方法。

1)equals方法

​ 1.比较两个非同一数组是否相等,而数组本身的equals判断另一个数组是否它本身。

​ 2.声明:public static boolean equals(type[] a ,type [] a2)

​ 3.参数的类型可以时原生数据类型和引用类型的任意一种类型。

​ 4.返回:如果两个相等,则返回true,否则false

2)sort 排序

​ 1.作用于数组的所有元素

​ public static void sort(type[] a)

​ 2.作用于数组指定范围内的元素

​ public static void sort(type[] a ,int fromIndex(包括),int toIndex(不包括)) 将指定的类型(除boolean以外的任意原生数组数据类型)数组所有元素(或指定范围内的元素)按数字升序进行排序。

​ ojbect型数组,根据元素的自然排序,对指定对象数组进行升序排序。(fromIndex == toindex,则排序范围为空)

​ 3.自定义对象排序

​ 自定义类实现Comparable接口

​ 重写compare to方法

3)binarySearch

使用二分搜索算法搜索指定数组

1.声明:

​ public static int binarySearch(type[] a ,type key)

​ public static int binarySearch(long[] a,int fromIndex,int toIndex ,long key)

2.描述:使用二分搜索算法搜索指定的type型数组,以获得指定的值。

3.参数:

​ a - 要搜索的数组。

​ key - 要搜索的值。

​ fromIndex - 要排序的第一个元素的索引(包括)。

​ toIndex - 要排序的最后一个元素的索引(不包括)。

​ type -byte、double、float、object、long、int、short、char

​ 如果key在数组中,则返回搜索值的索引;否则返回-1。

4)toString()方法

​ 1.声明 public static String toString(type[] a)

​ 2.描述:返回指定数组内容的字符串表示形式

​ 3.基本数组,字符串表示形式由数组的元素列表组成,括在[] ,相邻元素用“,”分隔

5.String类/StringBuffer类/StringBuilder类

1.String类

1)String类概述

​ 字符串是由多个字符组成一串数据的字符串常量,java中所有字符串都是String类的实例。

2)创建形式

​ 1.第一种 String s = “abc”;

​ 先在栈中创建一个对String类的对象引用变量s,然后去字符串常量池中查有没有"abc",如果没有则在常量池中添加"abc" ,s引用变量指向常量池中的"abc",如果常量池中有,则直接指向该地址即可,不用重写创建。

​ 2.第二种:都在堆中创建新对象,值储存在堆内存的对象中。

​ String s = new String(“abc”);

3)构造方法

​ public String()

​ public String(String str)

​ public String(byte[] bytes)

​ public String(char[] value)

4)判断功能

​ boolean equals(Object obj)

​ boolean equalsIgnoreCase(String str)

​ boolean contains(String str)

​ boolean isEmpty()

​ boolean startsWith(String prefix)

​ boolean endsWith(String suffix)

5)获取功能

​ int length()

​ char charAt(int index)

​ int indexOf(String str)

​ int indexOf(String str,int fromIndex)

​ String substring(int start)

​ String substring(int start,int end)

6)转换功能

​ byte[] getBytes()

​ char[] toCharArray()

​ static String valueOf(char[] chs)

​ String toLowerCase()

​ String toUpperCase()

​ String concat(String str)

​ Stirng[] split(分割符);

7)替换功能

​ String replace(char old,char new)

​ String replace(String old,String new)

​ replaceAll(String regex, String replacement)

​ replaceFirst(String regex, String replacement)

8.去除字符串两个空格

​ String trim() //去除头尾空格

public class StringDemo2 {
    public static void main(String[] args) {
        //tolowercase(); 将字符串全部转换为小写
        String s = "abDSGd3465";
        String s1 = s.toLowerCase();
        System.out.println(s1);
        //touppercase(); 将字符串全部转换为大写
        String s2 = s.toUpperCase();
        System.out.println(s2);
        //concat(); 字符串连接 相当于 s+="";
        String s3 = s.concat("gh");
        System.out.println(s3);
        //split(); 将字符串拆分
        String s4 = "ab:cd:efg";
        String[] s5 = s4.split(":");
        System.out.println(Arrays.toString(s5));
        //replase();按照指定字符替换
        String s6 = s.replace("ab","CD");
        System.out.println(s6);
        //replaseAll(); 按照正则表达式替换
        String s7 = s.replaceAll("\\d","");
        System.out.println(s7);
        //replaseFirst(); 按照正则表达式替换第一个出现的
        String s8 = s.replaceFirst("a","b");
        System.out.println(s8);
        //trim(); 去除两边的空格
        String s9 = " aagb ";
        String  s10 = s9.trim();
        System.out.println(s10);
    }
}

2.StringBuffer类

1)StringBuffer类概述

​ 对字符串进行拼接操作,每次拼接,都会构建一个新的String

对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题

线程安全的可变字符序列。

2)StringBuffer和String的区别
  1. StringBuffer对象的内容可以修改;而String对象一旦产生后就不可以被修改,重新赋值,其实是两个对象。
  2. String是对象,不是原始类型;为不可变对象,一旦被创建,就不能修改其值。
  3. 对于已经存在的String对象的修改,实际上是重新创建一个新的对象,然后把新的值保存进去。
3)StringBuffer的方法
  • 构造方法

​ public StringBuffer()

​ public StringBuffer(String str)

  • 添加功能

​ public StringBuffer append(String str)

​ public StringBuffer insert(int offset,String str)

  • 删除功能

    public StringBuffer deleteCharAt(int index)

    public StringBuffer delete(int start,int end)

  • 替换功能

    public StringBuffer replace(int start,int end,String str)

  • 反转功能

    public StringBuffer reverse()

  • 截取功能

    public String substring(int strat)

    public String substriing(int start,int end)

  • 截取功能和前面几个功能的不同

    返回值类型是String类型,本身没有发生改变

  • StringBuilder类功能和StringBuffer功能完全一致,StringBuffer是线程安全的

4)String类StringBuffer类StringBuilder区别

String:是字符常量,使用于少量的字符串操作的情况

StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况

StringBuffer:适用多线程下在字符缓冲区进行大量操纵的情况

6.Math类/Random类

1)Math类

​ 返回值类型一般为double型。

​ abs 绝对值

​ sqrt 平方根

​ pow(double a, double b) a的b次幂

​ max(double a, double b)

​ min(double a, double b)

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

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

public class MathDemo {
    public static void main(String[] args) {
        System.out.println(Math.sqrt(9));   // 求平方根
        System.out.println(Math.pow(2,4));  //求pow(n,m)n的m次方
        System.out.println(Math.abs(-4));   //绝对值
        System.out.println(Math.floor(9.4));   //向下取整
        System.out.println(Math.ceil(9.1));     //向上取整
        System.out.println(Math.round(3.5));    //四舍五入
        System.out.println(Math.random());    //在0 到1之间随机取数
    }
}

2)Random类

​ Random类概述:此类用于产生随机数

​ 构造方法:public Random()

​ 成员方法

​ public int nextInt()

​ public int nextInt(int n)

public class RandomDemo {
    public static void main(String[] args) {
        //Random类取随机数
        Random random = new Random();
        System.out.println(random.nextInt());
        System.out.println(random.nextFloat());
        System.out.println(random.nextLong());
        System.out.println(random.nextBoolean());
        System.out.println(random.nextDouble());
        System.out.println(random.nextInt(50)); //0 到49的随机数
    }
}

7.Date类/Calendar类/SimpleDateFormat类

1)Date类

​ 使用Date类代表当前系统时间

Date d = new Date();
Date d = new Date(long d);
public class DataDemo {
    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(date.getTime());  //1970-1-0:0:0至今的毫秒差
        System.out.println(new Date(1656316592959L));
        System.out.println(date.getDate());
        System.out.println(date.getMinutes());
        System.out.println(date.getDay());
    }
}

2)Calendar类

​ Calendar类是一个抽象类,在实际使用时实现特定的子类的对象,创建

​ 对象的过程对程序员来说是透明的,只需要使用getInstance方法创建即可。

	Calendar c1 = Calendar.getInstance(); 

	c1.get(Calendar. YEAR);
public class CalendarDemo {
    public static void main(String[] args) {
        Calendar calendar = new GregorianCalendar();
        System.out.println(calendar.getTimeInMillis()); //获得1970-1-0:0:0至今的毫秒差
        System.out.println(new Date(1656316917461L));
        System.out.println(calendar.getWeekYear());
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
        System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));
        Calendar c1 = Calendar.getInstance();
        System.out.println(c1.getWeekYear());
        System.out.println(c1.get(Calendar.YEAR));
        System.out.println(c1.get(Calendar.DAY_OF_WEEK));
    }
}

3)SimpleDateFormat类

SimpleDateFormat 日期格式化类

● 构造方法

SimpleDateFormat(格式); // yyyy-MM-dd

● 日期转字符串

Date now=new Date();

myFmt.format(now);

● 字符串转日期

myFmt.parse(“2018-02-10”);

字符串日期格式与 指定格式必须一致

例如:String s = “2018-03-15”;

new SimpleDateFormat(“yyyy-MM-dd”)

public class SimpleDateFormatDemo {
    public static void main(String[] args) {
        Date dates = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(dates));
        String str = "2002-1-24";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf1.parse(str);
            System.out.println(sdf1.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}

8.BigInteger/BigDecimal

在 Java 中,有许多数字处理的类,比如 Integer类,但是Integer类有一定的局限性。

● 我们都知道 Integer 是 Int 的包装类,int 的最大值为 2^31-1。若希望描述 更大的整数数据时,使用Integer 数据类型就无法实现了,所以Java中提供了BigInteger 类。

● BigInteger类型的数字范围较Integer,Long类型的数字范围要大得多,它支 持任意精度的整数,也就是说在运算中 BigInteger 类型可以准确地表示任何大小的整数值而不会丢失任何信息。

1) BigInteger类位于java.math包中

● 构造方法

BigInteger(String val) /BigInteger(byte[] val) …

基本运算方法

add(),subtract(),multiply(),divide()

在计算机中不论是float 还是double都是浮点数,而计算机是二进制的,浮点数会失去

一定的精确度。

● 根本原因是:十进制值通常没有完全相同的二进制表示形式;十进制数的二进制表示形式可

能不精确。只能无限接近于那个值.

double a = 1.0-0.9;

double b = 0.8-0.7;

System.out.println(a==b); // 结果?

• 但是,在项目中,我们不可能让这种情况出现,特别是金融项目,因为涉及金额的计算

都必须十分精确,你想想,如果你的支付宝账户余额显示193.99999999999998,那是

一种怎么样的体验?

2)Java在java.math包中提供的API类BigDecimal

• 构造方法

BigDecimal(String val)

基本运算方法

add(),subtract(),multiply(),divide()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

⁢⁢Mrx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值