Java学习——常用类库

2022-07-31,,,

API

API(Application Program Interface)就是应用程序编程接口,我们可以理解为:类的说明书。用于说明 类/接口/枚举 类型来自哪里(包名),叫什么(名称),做什么(解释),有什么(属性、方法、构造函数)以及他们具体做什么(方法的解释说明)。

常用

Object

java.lang包下的所有类,在java程序中使用时,都不需要导包。

  1. 在java.lang包下
  2. Object类是所有类的超类
  3. 所有对象(包括数组)都实现这个类的方法

 

常用方法:

hashCode():返回对象的哈希值——可以理解为对象在内存中的地址

equals(Object o):判断调用对象与o的地址是否相同(建议每个子类都重写

toString():以字符串形式返回对象信息(建议每个子类都重写


equals()与 == 的区别:

equals()是方法; == 是比较运算符

在Object类中,二者都用于比较对象地址是否相同

在其他子类中,一般重写equals(),用于比较对象内容是否相同


public class TestObject {



public static void main(String[] args) {



Object obj1 = new Object();

Object obj2 = new Object();

boolean r1 = obj1.equals(obj2);

System.out.println(r1);

Student s1 = new Student(18,"张三","103");

Student s2 = new Student(18,"张三","104");

boolean r2 = s1.equals(s2);

System.out.println(r2);

}

}

学生类中重写equals方法

@Override

public boolean equals(Object o){

Student other = (Student)o;

if(this.name.equals(other.name)

   &&this.age == other.age

   &&this.className.equals(other.className)){

    return true;

}

return false;

}

String类

String是类,但不能被继承

  1. 在java.lang包下
  2. Java程序中所有字符串值都是String类的对象
  3. 字符串是常量,创建后值不可更改

 

构造函数:

private static void testConstroct() {

// 无参构造

String s1 = new String();

// 有参构造

byte[] bytes = {65,66,67,97,98,99};

char[] chars = {'a','b','c'};

String s2 = new String(bytes);

String s3 = new String(bytes,2,2);

String s4 = new String("Java");

System.out.println(s4);

}

 

常用方法:

charAt(int index) : 返回指定索引处的 char 值。返回char类型

concat(String str) :将指定字符串连接到调用字符串的结尾。返回String类型(没有改变字符串的值,而是开辟了新的内存地址存入新的字符串,调用对象指向新的地址)

startsWith(String prefix) :测试此字符串是否以指定的前缀开始。返回boolean类型

endsWith(String suffix) :测试此字符串是否以指定的后缀结束。返回boolean类型

getBytes() :将调用字符串转换成新的byte数组,将结果存储并返回该byte数组。(可以使用Array.toString()再转换成String)返回byte[]类型

indexOf(char ch / String str [, int fromIndex]) :返回指定字符 / 子字符串在此字符串中第一次出现处的索引[,从指定的索引开始]返回int类型

lastIndexOf(char ch / String str [, int fromIndex]) :返回指定字符 / 子字符串在此字符串中最后一次出现处的索引[,从指定的索引开始]返回int类型

length():返回调用字符串的长度——即字符串内字符个数,返回int类型

isEmpty():判断字符串是否为空,返回boolean类型。

replace(char oldChar, char newChar) :返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。

replaceAll(String regex, String replacment) :使用给定的 replacement 替换此字符串匹配给定的正则表达式的子字符串。

replaceFirst(String regex, String replacment) : 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。

split(String regex[, int limit]) : 根据给定正则表达式拆分字符串;limit为拆分次数上限(可有可无,没有就算全部),返回String[]类型

substring(int begin[, int end]) : 返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。

toCharArray() : 将此字符串转换为一个新的字符数组,返回值类型char[]

trim() : 返回字符串的副本,忽略前导空白和尾部空白。

java正则表达式汇总

StringBuffer

 

线程安全的可变字符序列. 效率低

StringBuffer 转 String:

StringBuffer的toString()

new String(StringBuffer)

String.valueOf(Object );

 

构造函数:

StringBuffer sb1 = new StringBuffer("java");

 

常用方法:

setCharAt(char ch) : 将给定索引处的字符设置为ch

deleteCharAt(int index) : 将指定索引处的字符删除

insert(int index, a ) : 将a的字符串表达形式从指定索引从插入;a的数据类型可以是int、long、float、double、char、boolean、Object、String、char[](char[]可以指定char[]的起始位置和长度);返回值类型:StringBuffer

append(a) : 将a的字符串表达形式追加;a的数据类型可以是int、long、float、double、char、boolean、Object、String、StringBuffer、char[](char[]可以指定char[]的起始位置和长度);返回值类型:StringBuffer

 

StringBuilder

不保证线程安全的可变序列,与StringBuffer的API完全一致,但效率更高。

 

包装类

包装类主要指 将8种基本数据类型包装成对应的类

.大多数情况,包装类的主要作用是用于 基本数据类型与字符串的相互装换.

基本数据类型

包装类

byte

Byte

short

Short

int

Integer

long

Long

float

Float

double

Double

boolean

Boolean

char

Character

  1. 8种基本数据类型中除了Character以外都有parseXxx(String s) 将字符串转为对应的基本数据类型
  2. 自动装箱:可以直接将基本数据类型赋值给对应的包装类对象
  3. 自动拆箱:可以直接将包装类对象转为基本数据类型

int 转 Integer:

int a = 1;

Integer i1 = new Integer(a);

Integer i2 = a;

Integer i3 = Integer.valueOf(a);

 

Integer 转 int :

Integer i = new Integer(2);

int n1 = i.intValue();

int n2 = i;

 

int 转 String :

int i = 1;

String s1 =  “”+i;

String s2 = String.valueOf(i);

 

String 转 int :

String s = “1”;

int i = Integer.parseInt(s);

 

String 转 Integer :

String s = “1”;

Integer i1 = new Integer(s);

Integer i2 = Integer.valueOf(s);

 

Integer 转 String :

Integer i = 1;

String s = String.valueOf(i);


 

Math

public static void main(String[] args) {

// 圆周率常量值

System.out.println(Math.PI);

// 绝对值

System.out.println(Math.abs(-1));

System.out.println(Math.abs(1));

// 开立方根

System.out.println(Math.cbrt(27));

// 开平方根

System.out.println(Math.sqrt(2));

// 第一个参数的第二个参数次幂

System.out.println(Math.pow(2, 0.25));

// 上限最小整数double值

System.out.println(Math.ceil(12.3));

System.out.println(Math.ceil(12.8));

System.out.println(Math.ceil(-12.3));

System.out.println(Math.ceil(-12.8));

System.out.println("-------");

// 下限最大整数double值

System.out.println(Math.floor(12.3));

System.out.println(Math.floor(12.8));

System.out.println(Math.floor(-12.3));

System.out.println(Math.floor(-12.8));

// 四舍五入

System.out.println(Math.round(1.4));

// 返回 >=0.0 <`1.0 的double随机数

System.out.println(Math.random());

// 1-10的随机数 ?

int i = 1;

while(i <= 100) {

// System.out.println(Math.round(Math.random()*10)); // 有0

// System.out.println((int)(Math.random()*10));

System.out.print((int)(Math.random()*10+1)+" ");

i++;

}

}

 

System

long l = System.currentTimeMillis();//获取当前毫秒值,从1990-1-1开始

 

Random

package com.zhiyou100.homework;



import java.util.Random;



public class TestRandom {



public static void main(String[] args) {



Random r1 = new Random();

System.out.println(r1.nextInt());

// Random r2 = new Random(1);

// System.out.println(r2.nextInt());

// 摇骰子 色子

System.out.println(r1.nextInt(6)+1);

/*

 * 生成6位数随机数验证码

 */

/*

 * 点名程序

 */

}

public static void dianming() {

System.out.println("点名开始 ...");

String[] nameArr = {"A","B","C","D","邢士祥"};

for (int i = 10; i > 0; i--) {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(i);

}

System.out.println("就是你 --> "+nameArr[new Random().nextInt(5)]);



}

}

Date

 

在 JDK 1.1 之前,类 Date 有两个其他的函数。它允许把日期解释为年、月、日、小时、分钟和秒值。它也允许格式化和解析日期字符串。不过,这些函数的 API 不易于实现国际化。从 JDK 1.1 开始,应该使用 Calendar 类实现日期和时间字段之间转换,使用 DateFormat 类来格式化和解析日期字符串。Date 中的相应方法已废弃。

常用方法:

public static void main(String[] args) {

// 创建当前日期对象

Date d1 = new Date();

System.out.println(d1);

// year-1900 month[0-11]

// 以指定年月日创建日期对象

Date d2 = new Date(100,0,1);

System.out.println(d2);

// 以指定毫秒值创建日期对象

Date d3 = new Date(31536000000L);

System.out.println(d3);

// 获得日期对象距 1970-01-01的毫秒值

System.out.println(d2.getTime());
}

DateFormat & SimpleDateFormat

DateFormat 是抽象类, SimpleDateFormat是DateFormat 的实现子类。

SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。

SimpleDateFormat 使得可以选择任何用户定义的日期-时间格式的模式。但是,仍然建议通过 DateFormat 中的 getTimeInstance、getDateInstance 或 getDateTimeInstance 来创建日期-时间格式器。每一个这样的类方法都能够返回一个以默认格式模式初始化的日期/时间格式器。可以根据需要使用 applyPattern 方法来修改格式模式。

方法演示

public static void main(String[] args) throws ParseException {

Date d1 = new Date();

System.out.println("格式化前 : "+d1);

String pattern = "yyyy-MM-dd";

SimpleDateFormat sdf = new SimpleDateFormat(pattern);

String format = sdf.format(d1);

System.out.println("格式化后 : "+format);

System.out.println("----------");

String birthday = "2000-01-01";

// 解析: 只会解析默认pattern模板格式字符串

Date d2 = sdf.parse(birthday);

System.out.println(d2);

}

Calendar

public static void main(String[] args) {

Calendar rightNow = Calendar.getInstance();

// System.out.println(rightNow);

// 得到当前时间的某一字段值

System.out.println(rightNow.get(Calendar.YEAR));

System.out.println(rightNow.get(Calendar.MONTH)+1);

System.out.println(rightNow.get(Calendar.DATE));

// 日 一 二 .... 五 六

System.out.println(rightNow.get(Calendar.DAY_OF_WEEK));

System.out.println(rightNow.get(Calendar.HOUR_OF_DAY));

System.out.println(rightNow.get(Calendar.MINUTE));

System.out.println(rightNow.get(Calendar.SECOND));

// 重新设置值

rightNow.set(Calendar.YEAR, 2000);

// 设置时 0-11 代表1-12月

rightNow.set(Calendar.MONTH, 0);

rightNow.set(Calendar.DATE, 1);

System.out.println(rightNow.get(Calendar.YEAR));

// 得到时0-11 代表1-12月

System.out.println(rightNow.get(Calendar.MONTH));

System.out.println(rightNow.get(Calendar.DATE));

rightNow.add(Calendar.YEAR, 1);

rightNow.add(Calendar.DATE, 31);

System.out.println(rightNow.get(Calendar.YEAR));

System.out.println(rightNow.get(Calendar.MONTH)+1);

System.out.println(rightNow.get(Calendar.DATE));

}

 

本文地址:https://blog.csdn.net/ThisisK/article/details/107584780

《Java学习——常用类库.doc》

下载本文的Word格式文档,以方便收藏与打印。