API&常用类

24 篇文章 0 订阅

Java API概述

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

是对Java预先定义的类或接口功能和函数功能的说明文档,目的是提供给开发人员进行使用帮助说明

基本数据类型封装类

基本数据类型包装类
byte        Byte
shortShort
charCharacter
int        Integer
longLong
floatFloat
doubleDouble
booleanBoolean

包装类的主要用途

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

        2.包含每种基本数据类型的相关属性如最大值,最小值等,以及相关的操作方法

包装类常用属性方法


        System.out.println(Integer.MIN_VALUE);// -2147483648  最小的int型数(-2^31)
        System.out.println(Integer.MAX_VALUE);// 2147483647  最大的int型数(2^31-1)
        System.out.println(Integer.BYTES);// 4
        System.out.println(Integer.SIZE);// 32

        /*
            构造方法
         */
        Integer i1 = new Integer("20");
        Integer i2 = new Integer(20);



         /*
            其他方法
         */
        System.out.println(i1 == i2);// false


        /*
        public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
        }
         */

         System.out.println(i1.equals(i2)); // true  比较两个数是否相等

        // 求两个数中的最大值
        System.out.println(Integer.max(10, 5));
        // 求两个数中的最小值
        System.out.println(Integer.min(10,5));
        
        /*
            转换方法
         */
        System.out.println(Integer.toBinaryString(3));// 转为2进制
        System.out.println(Integer.toHexString(17));// 转为16进制
        System.out.println(Integer.toOctalString(9));// 转为8进制

        Integer i1 = new Integer(10);// 基本类型包装到对象中
        int i2 = i1.intValue();// 把对象中的基本类型取出来

        int i3 = Integer.parseInt("10");// 将String 类转为int类型
        i1.toString();// 将int包装类型 转为String类型
        Integer i4 = Integer.valueOf(10);// 将基本类型转为引用类型
        Integer i5 = Integer.valueOf("10");// 将String 转为引用类型

装箱和拆箱

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

           装箱的时候调用的是Integer的valueOf(int)方法

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

           拆箱的时候自动调用的是Integer的intValue方法

      

        /*
            自动装箱: 自动的将基本类型转为引用类型
         */
        int a = 10;
        // 手动装箱
        Integer a1 = new Integer(a);
        Integer a2 = Integer.valueOf(a);

        // 自动装箱
        Integer a3 = a;
        Integer a4 = 10;

        /*
         底层代码
                public static Integer valueOf(int i) {
                               -128                                +127
                    if (i >= IntegerCache.low && i <= IntegerCache.high)
                        return IntegerCache.cache[i + (-IntegerCache.low)];
                    return new Integer(i);
                }
         */
        
        // 自动拆箱: 底层默认调用intValue()
        int a5 = a3;

Object类

Object 类是所有Java类的祖先(根基类).每个类都使用Object作为父类.所有对象都实现这个类的方法.如果在类的声明中未使用extends关键字知名其基类,则默认基类为Object类

toString()方法   返回对象的字符串表示形式

 Car c = new Car("宝马", 30000);
        /*
            native 修饰的方法成为本地方法(有些方法Java是不实现的,直接调用把本地操作系统中的方法)
            输出一个对象时,默认会调用类中toString(),我们的类中如果没有定义toString(),找父类中的toString()
            Object类中的toString(){
                    getClass().getName()+"@"  hashCode()  --->  转成16进制的字符串
            }
         */

        /* 若对象没有方法中没有重写toString(),则直接调用Object中的方法 com.zwy.javaapi.objectdemo.Car@1b6d3586
            若对象那个重写了Object中的toString方法

        @Override
        public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", press=" + press +
                '}';
        }
            则输出为  Car{name='宝马', press=30000.0}

        */
        System.out.println(c.toString());

Arrays类

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

equals方法

比较两个非同一数组中的内容是否相等

public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        int[] b = {1, 2, 3, 4, 5};
        /*
            Arrays.equals(a,b)  比较数组中的内容是否相等
         */
        System.out.println(Arrays.equals(a,b));// true
    }

sort方法  排序

object型数组,根据元素的自然顺序,对指定对象数组进行升序排序

  public static void main(String[] args) {
        int[] a = {5,3,2,4,1};

        //Arrays.sort(a);  底层使用的是快速排序
        Arrays.sort(a,0,3);// 对某个区间排序,开始位置(包含开始),结束位置(不包含结束位置)
        System.out.println(Arrays.toString(a));// [1, 2, 3, 4, 5]
    }

自定义对象排序,自定义类实现Comparable接口,重写compareTo方法

        自定义类


public class User implements Comparable<User> {
    private int id;
    private String account;
    private String password;

    public User(int id, String account, String password) {
        this.id = id;
        this.account = account;
        this.password = password;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", account='" + account + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    /*
        为引用类型提供一个自定义排序规则
        此方法会在Arrays类中的sort()方法底层调用
        结果是用来判断大小  小于0  等于0  大于0
     */
    @Override
    public int compareTo(User o) {
        return this.id - o.id;
    }
}

测试

 public static void main(String[] args) {
        /*
            数组
         */
        User user1 = new User(1, "123456", "111111");
        User user2 = new User(2, "123457", "111111");
        User user3 = new User(3, "123458", "111111");
        User user4 = new User(4, "123454", "111111");
        User user5 = new User(5, "123459", "111111");

        /*
            创建User类型数组
         */

        User[] users = new User[]{user1, user2, user3, user4, user5};

        Arrays.sort(users);
        System.out.println(Arrays.toString(users));

    }

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

public static void main(String[] args) {
     int [] a = {8,1,5,9,6,2,3,4,7};
     /*
        二分查找法(这般查找) 前提: 有序的
      */

        Arrays.sort(a);
        System.out.println(Arrays.binarySearch(a,3));// 返回负数表示没有找到

        /*
            public static int binarySearch(int[] a, int fromIndex, int toIndex,
                                           int key) {
                rangeCheck(a.length, fromIndex, toIndex);
                return binarySearch0(a, fromIndex, toIndex, key);
            }

        a  表示要搜索的数组,fromIndex  要搜索的第一个元素的索引(包括), toIndex  要搜索的最后一个元素的索引(不包括), key  要搜索的值
         */
        System.out.println(Arrays.binarySearch(a,0,6,3));


        

    }

toString方法

返回指定数组内容的字符串表示形式

String 类

由多个字符组成的一串数据,值一旦创建不可改变,一旦值改变,就会创建一个新的对象

 private final char value[];

String 有两种创建形式:

第一种:

String s = "abc";

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

第二种:

一概在堆中创建新对象,值存储在堆内存的对象中

String s = new String("abc");

String 类方法中有判断功能的方法


        String s1 = "abc";
        String s2 = new String("abC");
        System.out.println(s1.equals(s2));// 比较字符串内容是否相等
        System.out.println(s1.equalsIgnoreCase(s2));// 比较字符串内容是否相等(忽略大小写)

        System.out.println(s1.contains("ac"));// 是否包含指定的子串
        System.out.println(s1.isEmpty());// 判断是否为""
        System.out.println(s1.startsWith("ab"));// 判断以指定字符开头
        System.out.println(s1.endsWith("bc"));// 判断以指定字符结尾

String 类方法中有获取功能的方法

        String s = "abcdefgd";
        System.out.println(s.length());
        char c = s.charAt(4);// 获取指定位置(索引)的字符
        System.out.println(c);
        int index = s.indexOf("d");// 从前向后找,只找首次出现的位置
        System.out.println(index);

        int index1 = s.indexOf("d",index+1);// 从前向后找,从指定位置开始
        System.out.println(index1);

        int index2 = s.lastIndexOf("d");// 从后向前查找,首次出现的位置
        System.out.println(index2);

        String s1 = "abcdefgd";
        String s2 = s1.substring(3);// 从指定的位置开始截取字符串,直接到最后一位,最终返回一个新的字符串对象
        System.out.println(s1);
        System.out.println(s2);

        String s3 = s1.substring(2, 6);// 截取指定区间  包含开始位置,不包含结束位置
        System.out.println(s3);

String 类方法中有获取功能的方法

        String s = "abcd";
        char[] chars = s.toCharArray(); // 将字符串转换为一个新的char数组
        System.out.println(Arrays.toString(chars));//  [a, b, c, d]

        String s1 = new String(chars);  //将char数组转为字符串
        System.out.println(s1);// abcd
        String s2 = String.valueOf(chars);
        System.out.println(s2);// abcd

        String s3 = "abcdEFG";
        System.out.println(s3.toUpperCase());// 转大写  ABCDEFG
        System.out.println(s3.toLowerCase());// 转小写  abcdefg

        String s4 = s3.concat("xxxxx"); // + 可以来连接其他类型
        System.out.println(s4);// abcdEFGxxxxx

        String s5 = "ab:cde:efg";
        String[] split = s5.split(":"); // 按照指定的分隔符,将字符串拆分为数组
        System.out.println(Arrays.toString(split));// [ab, cde, efg]

          String s6 = "你好";// 默认是utf-8 编码  在utf-8编码中,一个汉字占3个字节
        byte[] b = new byte[0];
        b=s6.getBytes("UTF-8"); // 编码
        System.out.println(Arrays.toString(b)); // [-28, -67, -96, -27, -91, -67]

        String s7 = new String(b,"UTF-8");// 解码
        System.out.println(s7);// 你好

        String s8  = new String(b,0,3,"UTF-8");// 解码第0-3
        System.out.println(s8);// 你

String 类方法中有替换功能的方法

        String s1 = "abcdefg";
        // 替换字符
        System.out.println(s1.replace('a', 'w')); // wabcdefg
        // 替换字符串  若未找到要更改的字符串,则不做处理
        System.out.println(s1.replace("ab", "wj"));// wjcdefg

        String s2 = "abcdabceabc";
        // 将所有匹配的字符串全部替换
        System.out.println(s2.replaceAll("abc", "wjw"));// wjwdwjwewjw
        // 替换第一个匹配的字符串
        System.out.println(s2.replaceFirst("abc","wjw"));// wjwdabceabc

        String s3 = " Hello World ";
        System.out.println(s3);// " Hello World "
        // 删除字符串前后的空格
        System.out.println(s3.trim());// "Hello World"

StringBuffer类

StringBuffer是Java中的一个类,用于创建和操作可变字符串,类似于String类,但它允许用户修改字符串的内容而不创建新的对象

        /*
            线程安全,可变字符串
                char[] value;
                AbstractStringBuilder(int capacity) {
                  value = new char[capacity];
                 }

                  super(16); // 无参
                  super(str.length() + 16);  // 有参

                 当向StringBuffer中添加内容时,是将内容添加到底层的数组中,数组没有被final修饰
                 当数组装满时,会创建一个新的数组,将新数组地址给底层数组,StringBuffer对象是不会改变的
         */
        //new StringBuffer(); // 默认底层char数组长度是16
       /* StringBuffer sb = new StringBuffer("abcd");
        sb.append("ef111111"); // 向末尾追加内容
        sb.append("ef2222");
        sb.append("ef3333");
        System.out.println(sb);*/

        StringBuffer sb = new StringBuffer("abcdefg");

        //sb.insert(2,"666"); // 向指定的位置差插入字符串
        //sb.deleteCharAt(2);// 删除指定位置的字符
        // sb.delete(0,2);// 删除指定区间内容,包含开始,不包含结束
        //sb.replace(0,3,"xxx");// 将指定区间的字符替换
        //sb.reverse();// 逆序
        //String sub =  sb.substring(2);// 截取一个字符串副本返回
       // String sub1 =  sb.substring(2,4);// 截取一个字符串副本返回
        //System.out.println(sub);
        System.out.println(sb);
        String s1 = "abcd";
        System.out.println(s1.length());//4
        System.out.println(sb.length());// 返回的是底层数组中实际装入字符的个数

StringBuilder类

StringBuilder用于创建和操作可变字符串,它与StringBuffer类似,但是不是线程安全的,因此在单线程环境下使用StringBuilder比StringBuffer更高效.

 /*
        多线程不安全  可变字符串

            @Override
            public StringBuilder append(String str) {
                super.append(str);
                return this;
            }

                @Override
                public synchronized StringBuffer append(String str) {
                    toStringCache = null;
                    super.append(str);
                    return this;
                }


           String             :底层都是数组实现   final char[] 值不能改变 ,改变后会创建一个新的对象
           StringBuffer       :线程安全的   可变字符串  char[]
           StringBuilder      :线程不安全   可变字符串
     */
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("aaa");

    }

String类,StringBuffer类和StringBuild类的区别

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

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

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

Math类

java.lang.Math提供了一系列静态方法用于科学计算,其方法的参数和返回值类型一般为double型

public static void main(String[] args) {
        int a = 2;
        int b = 3;
        int c = -4;
        int d = 4;
        System.out.println(Math.abs(c));// 求绝对值  4
        System.out.println(Math.sqrt(d));// 求平方根  2
        System.out.println(Math.pow(a, b));// 求a的b次幂 8
        System.out.println(Math.max(a, b));// 求a和b中的最大值  3
        System.out.println(Math.min(a, b));// 求a和b中的最小值  2
        System.out.println(Math.random());// 返回0.0到1.0的随机数

    }

Random类

此类用于产生随机数

 public static void main(String[] args) {
        Random r = new Random();
        System.out.println(r.nextInt());
        System.out.println(r.nextInt(8));// 生成0~8之间的随机数
    }

System类

System类包含一些有用的类字段和方法,它不能被实例化

    public static void main(String[] args) {
        System.exit(1);// 终止当前虚拟机的运行
        System.out.println(System.currentTimeMillis());// 返回当前时间(单位:毫秒)
    }

Date类

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

public static void main(String[] args) {
        long l = System.currentTimeMillis();
        Date date = new Date();// 创建一个日期对象,里面包含了程序运行时的那一刻的时间,提供方法方便实现
        System.out.println(date.getTime());// 1682928491899
        System.out.println(date.getYear()+1900);// 有划线的称为过期方法,不建议使用,有新方法代替
        System.out.println(date.getMonth()+1);

        Date date1 = new Date(1682928491899L);
        System.out.println(date1);

    }

Calendar类

Calendar类是一个抽象类,在实际使用时实现特定的子类的对象,创建对象的过程对程序员来说是头名的,只需要使用getInstance方法创建即可

    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        System.out.println(c.get(Calendar.YEAR));// 获取系统当前的年份
    }

SimpleDateFormat类

SimpleDateFormat日期格式化类

public static void main(String[] args) throws ParseException {
        SimpleDateFormat Myformat = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        // 日期转为字符串
        System.out.println( Myformat.format(now)); // 2023-05-15
        // 字符串转日期
        Date parse = Myformat.parse("2023-05-15");// Mon May 15 00:00:00 CST 2023
        System.out.println(parse);

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值