Java知识整理——JavaAPI

(1)、String类

字符串是由多个字符组成的一串数据(字符序列),字符串可以看作是字符数据,通过API,我们知道字面值"abc"也是一个对象,字符串是常量,一旦被赋值就不能改变。

1)构造方法

public String() public String(byte[] bytes)把字节数据转成字符串

public String(byte[] bytes,int offset,int length)把字节数组中的一部分转成字符串

public String(char[] value)把字符数组转成字符串

public String(char[] value,int offset,int count)把字符数组中的一部分转成字符串

public String(String original)把字符串常量值转成字符串

2)String s = "abc"; s+="def";问会不会报错?如不会问s的结果是多少?

字符串如果是变量相加,先开空间,在做拼接

字符串如果是常量相加,是先加,然后再常量池中找,如果有就直接返回,否则,就创建

3)成员方法

1.String类的获取功能

public int length()获取字符串长度

public char charAt(int index)获取指定索引处的字符

public int indexOf(int ch)获取指定字符在字符串中第一次出现的索引,因为所有的字符在ASCII表中都对应一个固定的int类型的值

public int indexOf(String str)获取指定字符串在字符串中第一次出现的索引

public int indexOf(int ch,int fromIndex)获取指定字符在字符串中指定位置后第一次出现的索引

public String substring(int beginIndex)从指定索引开始截取字符串

public String substring(int beginIndex,int endIndex)从指定位置开始到指定位置结束截取字符串,含头不含尾

public int lastIndexOf(int ch)获取指定字符在字符串中最后一次出现的索引

2.String类的转换功能

public byte[] getBytes()把字符串转成字节数组

public char[] toCharArray()把字符串转成字符数组

public static String valueOf(char[] data)把字符数组转成字符串

public static String valueOf(int i1)把int类型的数据转成字符串

public String toLowerCase()字符串转小写

public String toUpperCase()转大写

public String concat(String str)字符串拼接

3.String类的替换功能

public String replace(char oldChar,char newChar)

public String replaceAll(String regex,String replacement),去除空格

public String trim()去除字符串两端空格

4.String类的比较功能

public int compareTo(String anotherString),比较字符串内容是否相等

5.String类的判断功能

public boolean equals(Object anObject)比较的是字符串内容是否相同,区分大小写

public boolean equalsIgnoreCase(String anotherString)比较的是字符串内容是否相同,不区分大小写

public boolean contains(String s)判断是否包含某字符串

public boolean endsWith(String suffix)判断是否以某字符串结尾

public boolean startsWith(String prefix)判断是否是以某字符串开始

public boolean isEmpty()判断是否是空  判断的是字符串内容是否为空

(2)、StringBuffer类

StringBuffer类的构造方法

public StringBuffer()默认容量的字符串缓冲区16个字符

public StringBuffer(int capacity)自定义容量的字符串缓冲区对象

public StringBuffer(String str)指定内容的字符串缓冲区对象,默认长度是16+str.length();

1.StringBuffer类的添加功能

public StringBuffer append(String str)将指定的字符串附加到此字符序列。

public StringBuffer insert(int offset,String str)在指定位置插入字符串

2.StringBuffer类的删除功能

public StringBuffer delete(int start,int end)删除从指定位置开始到指定位置结束的内容

public StringBuffer deleteCharAt(int index)删除指定位置的字符,返回本身

3.StringBuffer类的替换功能

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

4.StringBuffer类的反转功能

public StringBuffer reverse()

5.StringBuffer类的截取功能

public String substring(int start)

public String substring(int start,int end)

(3)、System类

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

成员方法

static void gc(),System.gc()运行垃圾回收器

static void exit(int ststus),终止当前正在运行的Java虚拟机,status为状态码

static long currentTimeMillis(),返回当前时间与1970年1月1日0时0分0秒之间的时间差(时间戳)

static long arraycopy(Object src,int srcPos,Object dest,int dastPos,int length),从src源数组复制到dest目标数组,复制从指定的位置(srcPos)开始,到目标数组的指定位置(dest),length长度

static Properties getProperties(),返回一个Properties对象,其中封装了系统的所有属性,这些属性是以键值对的形式存在(待完善)

static String getProperties(String key),获取指定键描述的系统属性

(4)、Runtime类

每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。可以通过getRuntime()方法(Rnuttime run = Runtime.getRuntime())获取当前运行时类。用于表示虚拟机运行时的状态。

成员方法

public int availableProcessors()向 Java 虚拟机返回可用处理器的数目。

public long freeMemory()返回 Java 虚拟机中的空闲内存量。

public long totalMemory()返回 Java 虚拟机中的内存总量。

public long maxMemory()返回 Java 虚拟机试图使用的最大内存量。如果内存本身没有限制,则返回值 Long.MAX_VALUE。

public Process exec(String command)throws IOException在单独的进程中执行指定的字符串命令。 调用Process对象的destroy()方法关闭进程。

(5)、Math类

Math类包含执行基本数字运算的方法,如基本指数,对数,平方根和三角函数

成员方法

public static int abs(int a),计算绝对值

public static double ceil(double a),求大于参数的最小整数

public static double floor(double a),求小于参数的最大整数

public static int max(int a,int b),求两个数中的最大值

public static double pow(double a,double b),返回第一个参数的第二个参数次幂的值

public static double random(),生成一个大于等于[0.0小于1.0)随机值

public static int round(float a),对小数进行四舍五入

public static double sqrt(double a),返回正确舍入的 double 值的正平方根

(6)、Random类

用来生成随机数的类,如果使用相同的种子创建两个Random Random,并且对每个实例进行相同的方法调用序列,则它们将生成并返回相同的数字序列

构造方法

public Random(),没有种子,用的是默认的种子,当前时间的毫秒值

public Random(long seed)给种子后,每次得到的随机数都是相同的

成员方法

public int nextInt(),返回的是int范围内的随机数

public int nextInt(int bound)返回0-bound之间的随机数,不包括bound

(7)、缓冲池

Integer 内部有一个-128到127的缓存池,但是如果是new出来的,那每一个对象都会去新建,不会用到缓存池的数据。实测其他基本数据类型的包装类都有这个缓存池,包括:Byte,Short,Long

(8)、Character类的成员方法

public static boolean isLowerCase(char ch)确定指定的字符是否是小写字符

public static boolean isUpperCase(char ch)确定指定的字符是否为大写字符。

public static boolean isDigit(char ch)确定指定的字符是否是数字。

public static char toLowerCase(char ch)转小写

public static char toUpperCase(char ch)转大写

(9)、Date类

表示特定的瞬间,精确到毫秒

构造方法

public Date()根据当前默认的毫秒值创建日期对象

public Date(long date)根据给定的毫秒值创建日期对象

成员方法

public long getTime()

public void setTime(long time)

(10)、DateFormat类

DateFormat是日期/时间格式化子类的抽象类,它以语言无关的方式格式化和分析日期或时间。

是抽象类,所以我们用的时候应该用其子类SimpleDateFormat

构造方法

SimpleDateFormat是一个具体的类

public SimpleDateFormat()

public SimpleDateFormat(String pattern)

成员方法

public final String format(Date date)

public Date parse(String source)

一些常用的模式字符串

年 y、月 M、日 d、时 H、分 m、秒 s

(11)、Calendar类

Calendar类是一个抽象类,可以为在某一特定时刻和一组之间的转换的方法calendar fields如YEAR,MONTH,DAY_OF_MONTH,HOUR,等等,以及用于操纵该日历字段,如获取的日期下个星期。 时间上的瞬间可以用毫秒值表示

成员方法

public int get(int field),返回日历字段的值

public static Calendar getInstance(),获取当前时间的日历对象

public abstract void add(int field,int amount),根据日历字段的值,为指定的日历字段增加或减去指定的时间量

public final void set(int year,int month,int date),设置年月日

(12)、BigInteger类

可以让超过Integer范围的数据进行运算

构造方法

public BigInteger(String val)

成员方法

public BigInteger add(BigInteger val)

public BigInteger subtract(BigInteger val)

public BigInteger multiply(BigInteger val)

public BigInteger divide(BigInteger val)

public BigInteger[] divideAndRemainder(BigInteger val)

BigInteger bigInteger = new BigInteger("1001");
BigInteger[] bigIntegers = bigInteger.divideAndRemainder(new BigInteger("10"));
for (BigInteger integer : bigIntegers) {
    System.out.println(integer);
}

(13)、BigDecimal类

在运算的时候浮点型数据有可能会丢失精度,所以为了能够精确的表示并计算浮点型数据,java就给我们提供了 BigDecimal来解决这个问题

不可变的,任意精度的带符号的十进制数字。

构造方法

public BigDecimal(String val)

成员方法

public BigDecimal add(BigDecimal augend)

public BigDecimal subtract(BigDecimal subtrahend)

public BigDecimal multiply(BigDecimal multiplicand)

public BigDecimal divide(BigDecimal divisor)

public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)

(14)、Scanner类

从jdk1.5之后用来获取用户键盘录入的

构造方法

public Scanner(InputStream source)

public static final InputStream in,“标准”输入流,对应键盘录入

InputStream in = System.in;

Scanner sc = new Scanner(in);

成员方法

hasNextXxx():判断是否还有下一个输入项,Xxx可以是int double float...,如果需要判断是否包含下一个字符串,则省略Xxx

nextXxx():获取下一个输入内容,Xxx可以是int double float.....,默认情况下,Scanner使用空格,回车等作为分隔符,如果数据间有空格,应该把数据用“”括起来

常用

public int nextInt();

public String nextLine();

(15)、 Arrays类

针对数组操作的工具类,该类包含用于操作数组的各种方法(如排序和搜索)

成员方法

public static String toString(int[] a),把数组变成字符串

public static void sort(int[] a),对数组排序

public static int binarySearch(int[] a,int key),二分查找

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值