常用类之克隆
protected Object clone ( ) throws CloneNotSupportedException
克隆方法: 创建并返回此对象的一个副本
Object 类的 clone 方法执行特定的复制操作。
首先,如果此对象的类不能实现接口 Cloneable,则会抛出 CloneNotSupportedException。
protected void finalize ( ) throws Throwable ( 跟垃圾回收器有关系: System类的时候讲)
垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。
文本扫描器:Scanner
文本扫描器: Scanner ( java. util. Scanner)
2 ) 创建键盘录入对象: Scanner sc = new Scanner ( System. in) ;
构造方法:
public Scanner ( InputStream source)
形式参数:
抽象类: java. io. InputStream: 字节输入流( 用来读取字节数)
如果方法的形式参数是一个抽象类, 那么需要传递该抽象类的子类对象
System. in: System: 是jdk提供的一类
public static final InputStream in: 成员变量 : 标准输入流
public static final PrintStream out: 标准输出流
Scanenr: 键盘录入的细节问题
先录入int 类型, 在录入String类型 ( nextLine ( ) )
问题:
当前字符串录入数据: 值就被漏掉了, 原因就是回车符号( 本身字符串)
在下一次录入String类型前, 再次创建一个Scanner类型对象
Scanner类中判断功能
Scanner类中提供了一些判断功能:
public boolean hasNextXXX ( ) : 判断当前是否是执行XXX类型
int , short , float , double . . .
public XXX nextXXX ( ) : 接收指定XXX类型
举例:
public boolean haxNextInt ( ) : 判断接收的类型是否是int 类型
public int nextInt ( ) : 接收指定的int 类型的数据
public boolean haxNextLine ( ) : 判断接收的数据是否是一个String类型
public String nextLine ( ) : 接收一个String类型
常用类之String
常用类之String
String 类代表字符串
字符串是常量;它们的值在创建之后不能更改( 常量的地址值)
String str = "abc" ; Student s = new Student ( ) ;
String类是一个特殊的引用类型
String类型如果作为参数传递, 形式参的改变不会影响实际参数
( 跟基本类型作为参数的效果一样)
null 和 "" 一样吗? null: 对象为空 "" : 空字符序列 "" + 任何数据
String类的构造方法
String类的构造方法
public String ( ) : 无参构造方法: 创建一个字符串对象
public String ( byte [ ] bytes) : 使用平台的默认编码集 : 将字节数组-- -- > 构造出String类型
public String ( byte [ ] bytes, int offset, int length) : 将字节数组的一部分转换成字符串
参数1 : 传递的实际字节数组对象
参数2 : 从哪个位置开始
参数3 : 指定长度
public String ( char [ ] value) : 将字符数组-- - 构造成一个字符串对象
public String ( char [ ] value, int offset, int count) : 将字符数组一部分转换成字符串
参数1 : 传递的实际字符数组对象
参数2 : 从哪个位置开始
参数3 : 指定长度
public String ( String original) : 将一个字符串值存储进来, 创建一个字符串对象
面试题:(关于length)
面试题:
数组中有没有length ( ) 方法, String类型有没有length ( ) 方法, 集合中有没有length ( ) ?
数组中: length属性
String类型中有length方法( ) : 获取字符串长度
集合中: 没有length方法( ) , size ( )
面试题:(==和equals)
面试题: 以下s1和s2实际含义
String s1 = "hello" ;
String s2 = new String ( "hello" ) ;
前者: 创建的是一个对象
将常量赋值的形式: 直接在方法区中的常量池中 先查看是否存在常量"hello" , 如果存在
"hello" , 将地址值赋值给它, 如果不存在, 在常量池中开辟空间!
后者: 创建两个对象
new XXX ( ) : 堆内存
"hello" : 常量池中
s1和s2 : 都是表示"hello" 字符串
== : 连接两个引用类型 比较地址值是否相同
equals: 默认比较的是地址值, 重写toString后比较的是内容
equals()方法
String类用的比较多些:
equals ( ) 方法
public boolean equals ( String otherString)
在String类型中: equals: 比较是字符串的内容是否相同( 底层已经重写了Object的equals方法)
变量相加, 先去开空间, 再然后在相加, == ( 比较的地址值是否相同)
常量相加: 先相加, 然后常量池看是否存在这个值, 如果存在直接返回地址, 如果不存在开辟空间
String类型的判断功能
String类型的判断功能
public boolean equals ( Object anObject) : 比较两个字符串内容是否相同
public boolean equalsIgnoreCase ( String anotherString) : 不区分大小写比较
public boolean startsWith ( String prefix) : 判断是否以指定的子串开头
public boolean endsWith ( String suffix) : 判断是否以指定的子串结尾
public boolean isEmpty ( ) : 判断字符串是否为空
String类的获取功能和分割功能:
String类的获取功能:
int length ( ) : 获取字符串长度
public char charAt ( int index) : 获取指定索引处的字符
public String concat ( String str) : 字符串拼接功能: 使用当前字符串和指定str字符串进行拼接
public int indexOf ( int ch) : 获取指定字符在此字符串中第一次出现索引值
public int indexOf ( String str) : 获取指定字符串在此字符串中第一次出现索引值
String类的分割功能
public String[ ] split ( String regex) : 通过指定的字符串进行分割, 返回值字符串数组
String类转换功能:
String类转换功能:
public char [ ] toCharArray ( ) : 将字符串转换成字符数组
public byte [ ] getBytes ( ) : 将字符串-- -- 变成字节数组 ( 过程: 编码)
public static String valueOf ( XXX xx) : 可以将任何类型的数据转换成String
举例:
int i = 100 ; -- -- > int -- - String
String. valueOf ( i) ; -- -- > "100"
public String toUpperCase ( ) : 将此字符串转换成大写
public String toLowerCase ( ) : 将此字符串转换成小写
String类的截取功能
String类的截取功能
public String substring ( int beginIndex) : 从指定位置开始截取, 默认截取到末尾
public String substring ( int beginIndex, int endIndex)
从指定位置开始截取, 到指定位置结束, 但是包含beginIndex, 不包含endIndex索引( 包前不包后)
String的其他功能
替换功能
public String replace ( char oldChar, char newChar) : 将以前的字符用心的字符替换掉
public String replace ( String oldStr, char newStr) : 将以前的字符用心的字符替换掉
去除两端空格
public String trim ( )
按照字段顺序比较! : 如何比较的
public int compareTo ( String anotherString)
常用类之Characte
Character: char 类型的包装类类型, 包含一个char 类型的值
构造方法
public Character ( char value)
判断功能:
public static boolean isDigit ( char ch) : 判断当前字符是否是数字字符
public static boolean isLowerCase ( char ch) : 判断当前字符是否是小写字母字符
public static boolean isUpperCase ( char ch) : 判断当前字符是否是大写字母字符
常用类之Integer
Integer类的构造方法
Integer ( int value) : 将int 类型包装了Integer类型
Integer ( String s) : 将字符串内容包装Integer类型
注意事项:
String是一个数字字符串, 否则出现异常!
Integer类: 包含了int 类型的值
public static String toBinaryString ( int i) : 将整数值以"二进制" 文本形式体现. .
public static String toOctalString ( int i)
public static String toHexString ( int i)
成员变量( 字段)
public static final int MIN_VALUE: 最小值
public static final int MAX_VALUE: 最大值
常用类之StringBuffer
两个获取功能
int length ( ) : 获取长度( 缓冲区中字符数)
public int capacity ( ) : 获取缓冲区的容量大小
StringBuffer的构造方法:
1 ) public StringBuffer ( ) : 空参构造, 初始容量为 16 个字符。( 默认的容量足够, 不需要指定容量大小)
2 ) public StringBuffer ( String str) : 构造一个缓冲区, 指定字符串内容到缓冲区中, 容量大小等于16 个+ 当前字符串的实际长度
3 ) public StringBuffer ( int capacity) : 指定容量大小, 构造一个缓冲区
注意事项:
不能将String类型的值赋值给一个StringBuffer类型
StringBuffer sb = "hello" ;
String s = new String ( "hello" ) ;
StringBuffer sb2 = s ;
StringBuffer的添加功能:
StringBuffer的添加功能:
public StringBuffer append ( 任何类型数据. . . ) : 将任何类型的数据追加到缓冲区中( 追加末尾)
public StringBuffer insert ( int offset, String str/ 任何类型数据) : 将某个元素插入到指定位置
StringBuffer的删除功能
StringBuffer的删除功能
public StringBuffer deleteCharAt ( int index) : 删除指定位置处的字符, 返回缓冲区本身
public StringBuffer delete ( int start, int end) : 删除从指定位置到指定位置结束( 包前不包后, end- 1 位置)
StringBuffer的反转功能
反转功能
键盘录入一个数据, 将字符串进行反转-- - ( 内容还是字符串内容)
public StringBuffer reverse ( ) : 将缓冲区中的字符进行反转!
StringBuffer的截取功能和替换功能
StringBuffer的截取功能
public String substring ( int start) : 从指定位置开始截取, 默认截取到末尾
public String substring ( int start, int end) : 从指定位置开始截取, 截取到指定位置结束!
StringBuffer的替换功能
public StringBuffer replace ( int start, int end, String str)
从指定位置开始到指定位置结束( end- 1 处) 使用str子串进行替换
String<–>StringBuffer相互转换
String< -- > StringBuffer
String-- -- > StringBuffer
String s = "world" ;
方式1 : 通过StringBuffer构造方法: StringBuffer ( String str)
StringBuffer sb = new StringBuffer ( s) ;
System. out. println ( sb) ;
System. out. println ( "-------------------------------" ) ;
方式2 : StringBuffer ( ) 无参构造+ append ( String str) : 追加
StringBuffer sb2 = new StringBuffer ( ) ;
sb2. append ( s) ;
System. out. println ( sb2) ;
System. out. println ( "-------------------------------" ) ;
StringBuffer-- -- - > String
StringBuffer buffer = new StringBuffer ( "hello" ) ;
方式1
String str = new String ( buffer) ;
System. out. println ( str) ;
System. out. println ( "-------------------------------" ) ;
方式2
String str2 = buffer. toString ( ) ;
System. out. println ( str2) ;
常用类之Math
java. lang. Math:
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
public static double abs ( double a/ . . . ) : 求某个数据的绝对值
public static double ceil ( double a) : 向上取整
public static double floor ( double a) : 向下取整
public static int min ( int a, int b) : 求两个int 数据的最小值
public static int max ( int a, int b) : 求两个int 数据的最大值
public static double random ( ) : 随机数[ 0.0 , 1.0 ) -- -- -- -- - > Java提供随机数生成器: Random类 nextInt ( int n)
public static int round ( float a) : 四舍五入. .
public static long round ( double a)
public static double sqrt ( double a) : 开根( 一个正整数的平方根) 4 -- - 2
public static double pow ( double a, double b) : 返回结果: a的b次幂
常用类之Calendar(日历类)
java. util. Calendar类:
Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,
并为操作日历字段(例如获得下星期的日期)提供了一些方法
Calendar不能直接实例化!
某一个类如果不能直接实例化:
首先里面会存在一些静态功能, 静态功能的返回值就是该类本身!
public static Calendar getInstance ( )
public int get ( int field) : 获取的日历字段的值!
参数: 传递日历字段
Calendar的成员方法:
给日历字段添加或者减去时间偏移量
public abstract void add ( int field, int amount)
日历对象. add ( Calendar. DATE, - 5 ) : 当前日历时间的5 天前
设置当前年月日, 日历时间
public final void set ( int year, int month, int date)
日期类和字符串类之间的转换Date<–>String
String文本格式和Date日期格式相互转换
DateFormat继承自Format类: 都是抽象类, 不能直接实例化! , 提供更具体的子类
java. text. SimpleDateFormat extends DateFormat
它允许进行格式化(日期 - > 文本)、解析(文本 - > 日期)
SimpleDateFormat的构造方法
public SimpleDateFormat ( String pattern) :
创建一个SimpleDateFormat对象
参数: 指定一个模式 要遵循一定的规则( jdk对它规定了)
y年 -- -- -- 2020 "yyyy"
M年中月份-- -- 10 / 03 "MM"
d月中的天数-- -- 05 / 21 'dd'
h: 表示上午/ 下午的小时数 hh
H: 一天中的小时数 HH
m: 分钟数 mm
s: 秒数 ss
模式
"yyyy-MM-dd HH:mm:ss"
"yyyy年MM月dd日 HH时mm分ss秒"
Date-- -- > String
格式化的步骤:
1 ) 存在Date对象
2 ) 创建SimpleDateFormat ( String pattern)
3 ) public final String format ( Date date) : 格式化方法
格式化实例:
Date date = new Date ( ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd" ) ;
String dateStr = sdf. format ( date) ;
String-- -- - > Date
步骤:
1 ) 存在String "日期文本" "2020-10-21"
2 ) 创建SimpleDateFormat ( String pattern)
3 ) public Date parse ( String source) throws ParseException
解析:
SimpleDateFormat中的参数模式: 必须和String 日期文本格式一致, 否则出现解析异常 ( ParseException)
解析实例:
String source = "2022-6-30 20:00:30" ;
SimpleDateFormat sdf2 = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
Date d = sdf2. parse ( source) ;
常用类之Date
java. util. Date类
特定的瞬间,精确到毫秒。
构造方法:
public Date ( ) 无参构造
成员方法:
public long getTime ( ) : 返回1970 年 1 月 1 日所表示的时间毫秒
java. util. Date 应用场景 String: 文本格式 和Date格式的相互转换
常用类之System
System 类包含一些有用的类字段和方法。它不能被实例化。
字段:
public static final InputStream in : 标准的"输入流"
public static final PrintStream out : 标准的"输出流"
键盘录入:
Scanner ( InputStream in) -- -- > Scanner ( System. in)
输出语句:
System. out. println ( "xxx" ) -- -- - > System. out-- - > PrintStream ps ( 字节打印流)
ps. println ( "x" ) : 字节打印流调用方法名println ( String str)
成员方法:
public static void gc ( ) 运行垃圾回收器( 手动开启)
实际开发中, 开发者不需要开启
运行垃圾回收器, 它会调用Obejct类finalize ( ) , 回收没有更多引用的对象
public static void exit ( int status) : 参数状态为0 :
表示正常终止jvm
public static long currentTimeMillis ( ) : 获取当前系统时间的毫秒值
常用类之arraycopy
public static void arraycopy ( Object src, int srcPos, Object dest, int destPos, int length)
从原数组中的指定位置开始进行复制指定length长度复制到目标数组中的某个位置
参数1 : 指定源数组
参数2 : 源数组中的某个位置
参数3 : 目标数组对象
参数4 : 目标数组对象中的某个位置
参数5 : 指定几个元素复制( 长度)
常用类之exit
public static void exit ( int status) : 参数状态为0 :
表示正常终止jvm
应用场景:
模拟登录, 注册操作( 使用ArrayList)
while ( true ) {
使用switch 语句
"1" 注册功能
break ;
"2" 登录功能
break ;
"3" 退出
break ; -- -- 都是switch 语句
结束while 循环, 也可以使用System. exit ( 0 ) ;
}
public static long currentTimeMillis ( ) : 获取当前系统时间的毫秒值
常用类之Random
java. util. Random: 随机数生成器
构造方法
public Random ( ) : ( 推荐) , 产生的随机数是变化的
public Random ( long seed) : 每次产生的随机数 是固定的!
成员方法:
public int nextInt ( ) : 获取的是int 范围内的随机数
public int nextInt ( int n) : 获取0 到n之间的随机数[ 0 , n)
获取随机数两种方式:
Math类的random ( ) 方法: 返回值double 类型
Random类空参构造+ nextInt ( int n) : 获取随机数
常用类之BigDecimal
java. math. BigDecimal
描述: 对浮点类型的数据进行精确计算的!
构造方法:
public BigDecimal ( String val) : 字符串"数字字符串"
"1.0"
"0.01.."
成员方法:
public BigDecimal add ( BigDecimal augend) : 求和
public BigDecimal subtract ( BigDecimal subtrahend) : 减法
public BigDecimal multiply ( BigDecimal multiplicand) : 乘法
public BigDecimal divide ( BigDecimal divisor)