常用类和枚举类

System 系统类​​​​​​​

1. 说明:其它老师讲的时候,会给一个 api 文档,但是我不会给(在职的人不去看)跟着源码学习,不看 api

2. 常用方法:

//学习数组的时候,自己写过数组拷贝的代码
 public static native void arraycopy(Object src,  int  srcPos,Object dest, int destPos, int length);

//查询当前系统时间
System.currentTimeMillis();
public static native long currentTimeMillis();

//垃圾回收器
public static void gc() {
    Runtime.getRuntime().gc();
}

 字符串类(String)

1. 构造器 -> 字段 -> 方法

  • 构造器
//无参构造器
public String() {
    //做了数组初始化
    this.value = new char[0];
}
//带一个参数
public String(String original) {
    //给char 数组赋值
    this.value = original.value;
    //缓存String 的 hash 值
    this.hash = original.hash;
}
//传递 char 类型数组
public String(char value[]) {
    this.value = Arrays.copyOf(value, value.length);
}
  •  字段        
/** The value is used for character storage. */
//存储字符的数组
private final char value[];

/** Cache the hash code for the string */
//缓存 hash code
private int hash; // Default to 0
  • 方法
//查看字符串长度
public int length() {
    return value.length;
}
//判断是否为空
public boolean isEmpty() {
    return value.length == 0;
}
//返回一个索引对应字符值
public char charAt(int index) {
    return value[index];
}
//判断是否包含一个字符
public boolean contains(CharSequence s) {
    return indexOf(s.toString()) > -1;
}
//获取byte[]
public byte[] getBytes() {
    return StringCoding.encode(value, 0, value.length);
}
//以什么开头
public boolean startsWith(String prefix) {
    return startsWith(prefix, 0);
}
//以什么结尾
public boolean endsWith(String suffix) {
    return startsWith(suffix, value.length - suffix.value.length);
}
//equals 方法
equals(Object obj)
//以什么什么断开
public String[] split(String regex) {
    return split(regex, 0);
}
//去掉空字符串 “   abc   ”
public String trim() {}

需求练习

1. 创建一个字符串 String str = "   我爱学习-java"

  • 判断是否为空字符串

  • 通过 "-" 分割字符串

  • 去除字符串两端的空格

  • 判断字符串是否包含 java

String str = "   我爱学习-java";
boolean empty = str.isEmpty();
System.out.println("当前字符串是否为空:"+empty);

String[] split = str.split("-");
System.out.println(Arrays.toString(split));

String trim = str.trim();
System.out.println(trim);

boolean isContains = str.contains("java");
System.out.println("当前字符串是否包含java:"+isContains);

2. 创建两个字符串

  • String str = "sy";

  • String strNew = new String("sy");

  • 比较内容时怎么比较?

  • 怎么判断这两块空间不是同一块?(javap -verbose xx.class)

  • 画出String 创建的一个流程图?

3. 字符串类可以被继承吗?

StringBuilder(字符缓冲区)

1. StringBuilder 也是final 修饰的不可以不继承

2. 里面的 char[] 数组不是使用 final 修饰的,可以替换

3. 构造器

//默认的数组长度为16
public StringBuilder() {
    super(16);
}
//指定容量
public StringBuilder(int capacity) {
    super(capacity);
}

4. 方法

//做char[] 的拷贝或者拼接
@Override
public StringBuilder append(String str) {
    super.append(str);
    return this;//放回当前对象
}

//将 StringBuilder 对象转换成字符串对象
@Override
public String toString() {
    // Create a copy, don't share the array
    return new String(value, 0, count);
}

 5. 字段

/**
* The value is used for character storage.
*/
char[] value;
​
/**
* The count is the number of characters used.
*/
int count;

StringBuffer

1. 拼接 10万次字符串,去比较各自的耗时(StringBuffer用时短)
2. 注意:
   - StringBuilder 性能最好 > StringBuffer > String
   - StringBuffer  是线程安全的 

基本类型的包装类型

1. 问题:

  • 一切皆对象?int age = 18;

  • 给你一个数,8888,转换成二进制,算法非常难

  • double 类型,默认值 0.0,缺考怎么去表示?考试0分的时候怎么去表示?

2. 基本数据类型,缺少对象,jdk 提供了包装类型来解决如上问题。

Number 抽象类的学习

//获取 int 类型的值
public abstract int intValue();

//获取 long 类型的值
public abstract long longValue();

public abstract float floatValue();

public abstract double doubleValue();

public byte byteValue() {
    return (byte)intValue();
}

public short shortValue() {
    return (short)intValue();
}

Integer (int)

1. integer 继承结构 

 2. 构造器学习

public Integer(String s) throws NumberFormatException {
    this.value = parseInt(s, 10);
}
//将字符串解析成 int 类型
public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s,10);
}

//将字符串解析成 Integer 类型的数
public static Integer valueOf(String s) throws NumberFormatException {
    return Integer.valueOf(parseInt(s, 10));
}

//将Integer 对象转换成 int 数
public int intValue() {
    return value;
}

3. 自动装箱和拆箱(语法糖) 

//自动装箱
Integer integer3 = 10;
Integer integer3 = Integer.valueOf(10);
//自动拆箱
int intValue = integer3;
int intValue = integer3.intValue();

4. 整数类型做了一个缓存处理 

private static class IntegerCache {
    //缓存了 [-128,127]
}

String Integer int 相互转换

1. String 转 Integer

Integer.valueOf()

new Integer();

 2. String 转 int

Integer.valueOf();

3. Integer 转 int 

integer 对象.intValue();

## 拓展内容(面试内容,面试前再回头来听)

1. 缓存设计的优点

2. Integer 类型和 int 类型不是同一个类型

  • 节约空间开销

  • 超过缓存范围才重新分配空间

  • 通过方法的重载证明 

3. 两个方法分别定义 int a = 100000;当int 类型的值,超过 short 的最大值时,存储到常量池里面

包装类型对应

基本类型默认值包装类型包装类型默认值
int0Integernull
long0Longnull
booleanfalseBooleannull
byte0Bytenull
short0Shortnull
float0.0Floatnull
double0.0Doublenull
char''Characternull

注意:开发过程中,建议使用包装类型

大数据运算

1. BigInteger : java 开发中,我们的数超过了 long 类型,使用它 

public BigInteger(String val) {
    this(val, 10);
}
  •  四则运算:
public BigInteger add(BigInteger val) {}

public BigInteger subtract(BigInteger val) {}

public BigInteger multiply(BigInteger val) {}

public BigInteger divide(BigInteger val) {}

 2. BigDecimal:具有精度运算的

  • 需求:

    • 打印 0.09 + 0.01

    • 打印 1 - 0.34

    • 打印 1.403 / 100

  • 注意:做金钱运算使用 BigDecimal

  • 里面有加减乘除的方法

随机数 Random

1. 需求:随机生成 1- 100 之间的数,生成10次

Random random = new Random();

for (int i = 0; i < 10; i++) {
    int j = random.nextInt(100);
    System.out.println(j);
}

2. 需求:随机生成 100-200 之间的随机数,生成10次  

//多思考
Random random = new Random();

for (int i = 0; i < 10; i++) {
    int j = random.nextInt(100) + 100;
    System.out.println(j);
}

Math 类()数学类

1. Math 类包含了数学的常用方法  

  • 平方

  • 三角函数

  • 平方根

  • 指数

  • 绝对值

Arrays 工具类

正则表达式

1. 正则表达式:使用字符串来定义匹配规则(regex)

2. 正则表达式的匹配练习:

3. 注意:正则不用手动去写,也不用去记,了解一下就行了。

4. 去网站生成正则表达式就行

日期处理

Date

//创建当前日期对象
public Date() {
    this(System.currentTimeMillis());
}

//打印符合我们本地人观看的时间日期
@Deprecated
public String toLocaleString() {
    DateFormat formatter = DateFormat.getDateTimeInstance();
    return formatter.format(this);
}

SimpleDateFormat -->DateFormat

把日期变成字符串

public final String format(Date date)

把字符串变成日期

 public Date parse(String source) 

最常用的日期格式

  • yyyy-MM-dd

  • yyyy-MM-dd HH:mm:ss

字母含义示例
y年份。一般用 yy 表示两位年份,yyyy 表示 4 位年份使用 yy 表示的年扮,如 11; 使用 yyyy 表示的年份,如 2011
M月份。一般用 MM 表示月份,如果使用 MMM,则会 根据语言环境显示不同语言的月份使用 MM 表示的月份,如 05; 使用 MMM 表示月份,在 Locale.CHINA 语言环境下,如“十月”;在 Locale.US 语言环境下,如 Oct
d月份中的天数。一般用 dd 表示天数使用 dd 表示的天数,如 10
D年份中的天数。表示当天是当年的第几天, 用 D 表示使用 D 表示的年份中的天数,如 295
E星期几。用 E 表示,会根据语言环境的不同, 显示不 同语言的星期几使用 E 表示星期几,在 Locale.CHINA 语 言环境下,如“星期四”;在 Locale.US 语 言环境下,如 Thu
H一天中的小时数(0~23)。一般用 HH 表示小时数使用 HH 表示的小时数,如 18
h一天中的小时数(1~12)。一般使用 hh 表示小时数使用 hh 表示的小时数,如 10 (注意 10 有 可能是 10 点,也可能是 22 点)
m分钟数。一般使用 mm 表示分钟数使用 mm 表示的分钟数,如 29
s秒数。一般使用 ss 表示秒数使用 ss 表示的秒数,如 38
S毫秒数。一般使用 SSS 表示毫秒数使用 SSS 表示的毫秒数,如 156

Calendar 日历类

1. 获取日历对象  

public static Calendar getInstance()

2. 获取当前时间  

public final Date getTime() {
    return new Date(getTimeInMillis());
}

工具类的设计

1. 日期工具类的设计  

  • 需求:建立一个员工类, Employee, entry 入职时间,birthday 生日,创建员工管理类 EmployeeManager input 录入数据

  • 发现如果不使用工具类,代码太臃肿了。所以需要抽工具类

2. 数组工具类自行设计

枚举

1. 需求:定义一个学生类,定义一个成员变量 restDay(表示学习哪天休息)

2. 问题如下

  • 非法数据设置、

  • 解决办法1:使用指定的单独管日期的类 WeekDay,并且不能够修改内容,使用7个常量来表示,能保证选择数据安全性,但是还是吧不能保证数据安全

  • 解决办法2:私有化构造器,使用对象来代替常量

 1. 枚举:

[public] enum 枚举名称{
    常量1,常量2;
}

 2. 特点

  • 构造器私有化

  • 里面是一个个常量

  • 枚举里面可以提供带参数构造器

  • 可以提供 set get方法

  • 可以定义普通方法

单例设计模式

1. 单例模式:只能创建出一个对象,不能创建多个,提供方法去获取需要的对象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值