Day 16
一、包装类(掌握)
1.1 定义
基本类型数据的对应的引用类型数据 基本类型数据 有八种,包装类也有八种
1.2 包装类
byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean
二、Integer(掌握)
2.1 定义
Integer
类在对象中包装了一个基本类型 int
的值。Integer
类型的对象包含一个 int
类型的字段。该类提供了多个方法,能在 int
类型和 String
类型之间互相转换
2.2 创建对象
构造方法摘要
Integer ( int value)
构造一个新分配的 Integer 对象,它表示指定的 int 值。
Integer ( String s)
构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。
package com. qf. pack;
public class Demo02 {
public static void main ( String[ ] args) {
Integer i1 = new Integer ( 110 ) ;
System. out. println ( i1) ;
Integer i2 = new Integer ( "220" ) ;
System. out. println ( i2) ;
System. out. println ( i2. getClass ( ) . getName ( ) ) ;
Integer i3 = new Integer ( "a" ) ;
System. out. println ( i3) ;
}
}
2.3 常用的转换的方法
package com. qf. pack;
public class Demo03 {
public static void main ( String[ ] args) {
Integer i1 = new Integer ( 120 ) ;
System. out. println ( i1) ;
Integer i2 = Integer. valueOf ( 220 ) ;
System. out. println ( i2) ;
Integer i3 = new Integer ( 330 ) ;
int ii3 = i3. intValue ( ) ;
System. out. println ( ii3) ;
Integer i4 = new Integer ( "440" ) ;
System. out. println ( i4) ;
Integer i5 = Integer. valueOf ( "550" ) ;
System. out. println ( i5) ;
String s5 = i5. toString ( ) ;
System. out. println ( s5) ;
System. out. println ( s5. getClass ( ) . getName ( ) ) ;
}
}
2.4 自动装箱和拆箱
package com. qf. pack;
public class Demo05 {
public static void main ( String[ ] args) {
Integer i1 = new Integer ( 110 ) ;
int ii1 = i1. intValue ( ) ;
Integer i2 = 220 ;
System. out. println ( i2) ;
System. out. println ( i2. getClass ( ) . getName ( ) ) ;
int ii2 = i2;
System. out. println ( ii2) ;
}
}
三、字符串String(掌握)
3.1 定义
java中所有的字符串都是此类的实例 字符串是常量,一旦确定不可修改
package com. qf. str;
public class Demo01 {
public static void main ( String[ ] args) {
String str = "abc" ;
char data[ ] = { 'a' , 'b' , 'c' } ;
String str1 = new String ( data) ;
System. out. println ( str) ;
System. out. println ( str1) ;
System. out. println ( str. equals ( str1) ) ;
System. out. println ( str == str1) ;
String s1 = "Hello" ;
System. out. println ( s1. hashCode ( ) ) ;
s1 += "World" ;
System. out. println ( s1. hashCode ( ) ) ;
}
}
3.2 创建字符串对象
package com. qf. str;
public class Demo02 {
public static void main ( String[ ] args) {
byte [ ] bytes = new byte [ ] { 65 , 66 , 67 , 68 , 69 } ;
System. out. println ( bytes) ;
String sb = new String ( bytes) ;
System. out. println ( sb) ;
String sb1 = new String ( bytes, 1 , 2 ) ;
System. out. println ( sb1) ;
}
}
package com. qf. str;
public class Demo03 {
public static void main ( String[ ] args) {
char [ ] chars = new char [ ] { '床' , '前' , '明' , '月' , '光' , ',' } ;
System. out. println ( chars) ;
System. out. println ( chars. getClass ( ) . getName ( ) + "@" + chars. hashCode ( ) ) ;
String sc1 = new String ( chars) ;
System. out. println ( sc1) ;
String sc2 = new String ( chars, 1 , 3 ) ;
System. out. println ( sc2) ;
}
}
package com. qf. str;
public class Demo04 {
public static void main ( String[ ] args) {
int [ ] arr = { 97 , 98 , 99 , 100 , 101 } ;
String si1 = new String ( arr, 0 , arr. length) ;
System. out. println ( si1) ;
String ss = new String ( "Hello" ) ;
System. out. println ( ss) ;
}
}
3.3 字符串常用方法
package com. qf. str;
import java. util. Arrays;
public class Demo05 {
public static void main ( String[ ] args) {
String s = "疑是地上霜。" ;
System. out. println ( s) ;
String ss = s. concat ( "\n举头望明月,\n低头思故乡" ) ;
System. out. println ( s) ;
System. out. println ( ss) ;
String str = "日照香炉生紫烟,遥看瀑布挂前川。" ;
String str1 = str. replace ( '挂' , '钉' ) ;
System. out. println ( str) ;
System. out. println ( str1) ;
String str2 = "唧唧复唧唧,木兰当户织。" ;
String str22 = str2. replaceAll ( "唧" , "鸡" ) ;
System. out. println ( str22) ;
String str3 = "啦啦啦,啦啦啦,我是卖报的小行家。" ;
System. out. println ( str3) ;
String[ ] ret = str3. split ( "啦" , 3 ) ;
System. out. println ( Arrays. toString ( ret) ) ;
String str4 = "ABCDE" ;
byte [ ] bytes = str4. getBytes ( ) ;
System. out. println ( Arrays. toString ( bytes) ) ;
char [ ] cs = str4. toCharArray ( ) ;
System. out. println ( cs) ;
char [ ] charss = new char [ 5 ] ;
str4. getChars ( 0 , 3 , charss, 2 ) ;
System. out. println ( charss) ;
}
}
package com. qf. str;
public class Demo06 {
public static void main ( String[ ] args) {
String str1 = "张三丰" ;
boolean ret = str1. charAt ( 0 ) == '张' ? true : false ;
String str11 = str1. charAt ( 0 ) == '张' ? "姓张" : "不姓张" ;
int indexOf = str1. indexOf ( "三丰" ) ;
System. out. println ( indexOf) ;
System. out. println ( str1. startsWith ( "张" ) ) ;
String str2 = "aBcDeFg" ;
String str22 = str2. toLowerCase ( ) ;
System. out. println ( str22) ;
String str222 = str2. toUpperCase ( ) ;
System. out. println ( str222) ;
}
}
四、缓冲字符串(掌握)
4.1 定义
线程安全的可变字符序列。 一个类似于 String
的字符串缓冲区,通过某些方法调用可以改变该序列的长度和内容。 StringBuffer
上的主要操作是 append
和 insert
方法,可重载这些方法,以接受任意类型的数据。
4.2 创建对象
package com. qf. sb;
public class Demo01 {
public static void main ( String[ ] args) {
StringBuffer sb1 = new StringBuffer ( ) ;
System. out. println ( sb1) ;
System. out. println ( sb1. length ( ) ) ;
System. out. println ( sb1. capacity ( ) ) ;
StringBuffer sb2 = new StringBuffer ( "床前明月光" ) ;
System. out. println ( sb2) ;
System. out. println ( sb2. getClass ( ) . getName ( ) ) ;
System. out. println ( sb2. length ( ) ) ;
System. out. println ( sb2. capacity ( ) ) ;
}
}
4.3 插入数据的方法
package com. qf. sb;
public class Demo02 {
public static void main ( String[ ] args) {
StringBuffer sb1 = new StringBuffer ( ) ;
sb1. append ( "锄禾日当午," ) ;
sb1. append ( "汗滴禾下土." ) ;
sb1. append ( "谁知盘中餐," ) ;
sb1. append ( "粒粒皆辛苦." ) ;
System. out. println ( sb1) ;
System. out. println ( sb1. length ( ) ) ;
System. out. println ( sb1. capacity ( ) ) ;
StringBuffer sb2 = new StringBuffer ( "浔阳江头夜送客,枫叶荻花秋瑟瑟,主人下马客在船,举杯欲饮无管弦." ) ;
System. out. println ( sb2) ;
sb2. insert ( 0 , true ) ;
System. out. println ( sb2) ;
sb2. insert ( 0 , 0.5 ) ;
System. out. println ( sb2) ;
sb2. insert ( 10 , "《琵琶行》" ) ;
System. out. println ( sb2) ;
}
}
4.4 删除的方法
package com. qf. sb;
public class Demo03 {
public static void main ( String[ ] args) {
StringBuffer sb1 = new StringBuffer ( "浔阳江头夜送客,枫叶荻花秋瑟瑟,主人下马客在船,举杯欲饮无管弦." ) ;
System. out. println ( sb1) ;
sb1. delete ( 24 , 32 ) ;
System. out. println ( sb1) ;
sb1. deleteCharAt ( 0 ) ;
System. out. println ( sb1) ;
}
}
4.5 修改的方法
package com. qf. sb;
public class Demo04 {
public static void main ( String[ ] args) {
StringBuffer sb1 = new StringBuffer ( "浔阳江头夜送客,枫叶荻花秋瑟瑟,主人下马客在船,举杯欲饮无管弦." ) ;
System. out. println ( sb1) ;
sb1. replace ( 0 , 8 , "《琵琶行》" ) ;
System. out. println ( sb1) ;
sb1. reverse ( ) ;
System. out. println ( sb1) ;
sb1. reverse ( ) ;
System. out. println ( sb1) ;
System. out. println ( sb1. capacity ( ) ) ;
sb1. trimToSize ( ) ;
System. out. println ( sb1. capacity ( ) ) ;
sb1. setLength ( 10 ) ;
System. out. println ( sb1) ;
}
}
4.6 查找的方法
package com. qf. sb;
public class Demo05 {
public static void main ( String[ ] args) {
StringBuffer sb1 = new StringBuffer ( "浔阳江头夜送客,枫叶荻花秋瑟瑟,主人下马客在船,举杯欲饮无管弦." ) ;
System. out. println ( sb1) ;
System. out. println ( sb1. charAt ( 10 ) ) ;
System. out. println ( sb1. indexOf ( "荻" , 11 ) ) ;
}
}
4.7 String 和 StringBuffer互转的方法
package com. qf. sb;
public class Demo06 {
public static void main ( String[ ] args) {
StringBuffer stringBuffer = new StringBuffer ( "清明上河图" ) ;
String string = stringBuffer. toString ( ) ;
}
}
五、BigDecimal(了解)
5.1 定义
不可变的、任意精度的有符号十进制数。 解决运算中出现的小数位不准确的问题
5.2 创建和使用
package com. qf. bd;
import java. math. BigDecimal;
public class Demo03 {
public static void main ( String[ ] args) {
BigDecimal bd1 = new BigDecimal ( "1.0" ) ;
BigDecimal bd2 = new BigDecimal ( "0.9" ) ;
System. out. println ( bd1. add ( bd2) ) ;
System. out. println ( bd1. subtract ( bd2) ) ;
System. out. println ( bd1. multiply ( bd2) ) ;
System. out. println ( bd1. divide ( bd2, 2 , BigDecimal. ROUND_HALF_UP) ) ;
}
}
package com. qf. bd;
import java. math. BigDecimal;
public class Demo02 {
public static void main ( String[ ] args) {
BigDecimal bd1 = new BigDecimal ( 1.0 ) ;
BigDecimal bd2 = new BigDecimal ( 0.9 ) ;
System. out. println ( bd1) ;
System. out. println ( bd2) ;
System. out. println ( bd1. add ( bd2) ) ;
System. out. println ( bd1. subtract ( bd2) ) ;
System. out. println ( bd1. multiply ( bd2) ) ;
System. out. println ( "============================" ) ;
BigDecimal bd3 = new BigDecimal ( "1.0" ) ;
BigDecimal bd4 = new BigDecimal ( "0.9" ) ;
System. out. println ( bd3) ;
System. out. println ( bd4) ;
System. out. println ( bd3. add ( bd4) ) ;
System. out. println ( bd3. subtract ( bd4) ) ;
System. out. println ( bd3. multiply ( bd4) ) ;
}
}
六、Math(掌握)
6.1 定义
Math
类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。Math类没有构造方法,但是Math类中的方法基本都是静态的方法,可以直接调用
6.2 Math常用方法
package com. qf. math;
public class Demo01 {
public static void main ( String[ ] args) {
System. out. println ( Math. abs ( 3.3 ) ) ;
System. out. println ( Math. abs ( - 3.3 ) ) ;
System. out. println ( Math. ceil ( 6.6 ) ) ;
System. out. println ( Math. floor ( 6.6 ) ) ;
System. out. println ( Math. sqrt ( 9 ) ) ;
System. out. println ( Math. cbrt ( 8 ) ) ;
System. out. println ( Math. round ( 5.5 ) ) ;
for ( int i = 0 ; i < 100 ; i++ ) {
System. out. println ( Math. random ( ) ) ;
}
}
}
七、Random(掌握)
7.1 定义
7.2 常用方法
package com. qf. math;
import java. util. Random;
public class Demo02 {
public static void main ( String[ ] args) {
Random r = new Random ( ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
System. out. println ( r. nextInt ( 100 ) ) ;
}
}
}
八、Date(掌握)
8.1 定义
package com. qf. date;
import java. util. Date;
public class Demo01 {
public static void main ( String[ ] args) {
System. out. println ( System. currentTimeMillis ( ) ) ;
Date date = new Date ( ) ;
System. out. println ( date) ;
Date date2 = new Date ( 1591602993205 L) ;
System. out. println ( date2) ;
System. out. println ( date. getYear ( ) ) ;
System. out. println ( date. getMonth ( ) ) ;
System. out. println ( date. getHours ( ) ) ;
}
}
九、SimpleDateFormat(掌握)
9.1 定义
SimpleDateFormat
是一个以与语言环境有关的方式来格式化和解析日期的具体类
9.2 案例
package com. qf. date;
import java. text. ParseException;
import java. text. SimpleDateFormat;
import java. util. Date;
public class Demo02 {
public static void main ( String[ ] args) throws ParseException {
Date date = new Date ( ) ;
System. out. println ( date) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy年MM月dd日 HH时mm分ss秒" ) ;
String time = sdf. format ( date) ;
System. out. println ( time) ;
Date date2 = sdf. parse ( "2020年6月8日 16时08分08秒" ) ;
System. out. println ( date2) ;
System. out. println ( date2. getTime ( ) ) ;
}
}