有关常用类
包装类
- 八种基本类型的包装类(Wrapper)
基本数据类型 | 包装类 |
---|---|
boolean | Boolean |
char | Char |
byte | Byte |
short | Short |
int | Int |
long | Long |
float | Float |
double | Double |
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Rj3bU1dy-1640881559368)(C:\Users\11021\AppData\Roaming\Typora\typora-user-images\image-20211228155549739.png)][外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oHX8PPGQ-1640881559369)(C:\Users\11021\AppData\Roaming\Typora\typora-user-images\image-20211228155623182.png)][外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bGkDNFV4-1640881559369)(C:\Users\11021\AppData\Roaming\Typora\typora-user-images\image-20211228155655130.png)]
- 装箱和拆箱,包装类和基本数据类型之间的转换
public static void main(String[] args) {
int a = 2;
Integer b = a; //自动装箱
int c = b; //自动拆箱
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
}
a=2
b=2
c=2
- 包装类和String之间的转换
public class WrapperVsString {
public static void main(String[] args) {
//Integer -> String
Integer i = 200;
//No.1
String s = i + "";
//No.2
String s1 = i.toString();
//No.3
String s2 = String.valueOf(i);
//String -> Integer
String str = "300";
//No.1
Integer integer = Integer.parseInt(str);
//No.2
Integer integer1 = new Integer(str);
}
}
- 常用方法
System.out.println("最大值:"+Integer.MAX_VALUE);
System.out.println("最小值:"+Integer.MIN_VALUE);
Character c = 'a';
System.out.println("是不是数字"+Character.isDigit(c));
System.out.println("是不是字母"+Character.isLetter(c));
System.out.println("是不是大写" + Character.isUpperCase(c));
System.out.println("是不是小写" + Character.isLowerCase(c));
System.out.println("是不是空格" + Character.isWhitespace(c));
System.out.println("转大写" + Character.toUpperCase(c));
System.out.println("转小写" + Character.toLowerCase(c));
String
- 基本概念
String 中的字符是Unicode字符编码,一个字符占2个字节,对比char,char一个字符占1个
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3Obd1GOc-1640881559370)(C:\Users\11021\AppData\Roaming\Typora\typora-user-images\image-20211228164245542.png)]
- CharSequence:实现字符序列接口
- Comparable:实现此接口,说明String对象可以比较
- Serializable:实现此接口,说明String可以串行化,即可以网络传输
- 构造器
String可以有很多种构造器,常用的有5个
String是final类不能被继承
private final char value[];
//value是final类型,不可以修改地址,字符内容可以改
//用于存放字符串内容
解释一下第二句话:
final char value[] = {'a','b','c'};
value[1] = 'k'; //这一行并不报错
char value1[] = {'t','o','m'};
value = value1; //这一行报错
public class String01 {
public static void main(String[] args) {
String s = "hsp";
String string1 = new String("hsp");
System.out.println(s.equals(string1)); //true
//都指向常量池中的0x9
System.out.println(s == string1); //false
//两者地址不一样,如下图,s1的地址0x11 , s的地址0x99
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MVtqdPIg-1640881559370)(C:\Users\11021\AppData\Roaming\Typora\typora-user-images\image-20211228192136992.png)]
- 常用方法
String str1 = "I love java!";
String str2 = "I LOVE JAVA!";
System.out.println("字符串比较"+str1.equals(str2));
System.out.println("字符串比较忽略大小写"+str1.equalsIgnoreCase(str2));
System.out.println("索引a第一次出现"+str1.indexOf("a"));
System.out.println("索引a最后一次出现"+str2.lastIndexOf("a"));
System.out.println("截取下标4之后的内容"+str1.substring(4));
System.out.println("截取下标0~5之间的内容"+str1.substring(0, 5));
String str3 = "i loVe JAvA!";
System.out.println("转大写"+str3.toUpperCase());
System.out.println("转小写"+str3.toLowerCase());
String str4 = "赤瞳";
System.out.println("拼接:"+str4.concat("黑瞳").concat("塔兹米").concat("雷欧奈"));
String str5 = "F:\\a\\b\\ccc";
String[] split = str5.split("\\\\");
System.out.println("===分隔输出===");
for (String s : split) {
System.out.print(" "+s);
}
String str6 = "abcd";
String str7 = "AbC";
System.out.println(str6.compareTo(str7));
char[] chars = str6.toCharArray();
System.out.println("===转成字符数组输出===");
for (char aChar : chars) {
System.out.print( " " + aChar);
}
System.out.println("======格式化(format)输出======");
int a = 14;
String b = "i love";
double c = 7.0;
String format = String.format("%d, %s , %.2f ", a, b, c);//.2 小数点后保留两位输出
System.out.println(format);
增强String:StringBuffer,StringBuilder
-
基本概念
String类是保存字符串常量,每次更新需要重新开辟空间,因此有了后来的StringBuilder和StringBuffer
stringbuffer是一个容器,可变长度,即对字符串内容进行增删
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oMdQ4vst-1640881559371)(C:\Users\11021\AppData\Roaming\Typora\typora-user-images\image-20211229191927007.png)]
stringBuffer注意点: 1.继承父类 AbstractStringBuilder abstract class AbstractStringBuilder implements Appendable, CharSequence { char[] value; //区别与String ,不是final //该数组存放字符串内容,直接放到堆中 2.继承 Serializable,可以串行化 3.StringBuffer是final类,不能被继承
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b8AS7g5z-1640881559371)(C:\Users\11021\AppData\Roaming\Typora\typora-user-images\image-20211229233424840.png)]
-
构造器
public static void main(String[] args) { //构造器char数组大小默认16 StringBuffer stringBuffer = new StringBuffer(); //构造器char数组大小100 StringBuffer stringBuffer1 = new StringBuffer(100); //构造器char数组大小16+6 StringBuffer string = new StringBuffer("string"); }
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SKUHSImu-1640881559372)(C:\Users\11021\AppData\Roaming\Typora\typora-user-images\image-20211229234811340.png)]
-
String和StringBuffer之间的转换
public static void main(String[] args) {
//String转StringBuffer
String str = "abc";
//No.1 append方法
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(str);
//No.2 构造器
StringBuffer stringBuffer1 = new StringBuffer(str);
//StringBuffer转String
StringBuffer tom = new StringBuffer("Tom");
//No.1 toString方法
System.out.println(tom.toString());
//No.2 构造器
String s = new String(tom);
}
- 常用方法
StringBuffer stringBuffer = new StringBuffer("I");
//增
stringBuffer.append(" love java!");
System.out.println(stringBuffer);
//删
stringBuffer.delete(0,2);
//改
stringBuffer.replace(0,4,"replace");
//查
System.out.println(stringBuffer.indexOf("va"));
System.out.println(stringBuffer);
//插入
stringBuffer.insert(3,"插入");
System.out.println(stringBuffer);
//获取长度
System.out.println(stringBuffer.length());
- StringBuilder类似于StringBuffer,但不是线程安全的,用于单线程,但是比StringBuffer快
- 有大量修改操作用StringBuffer,StringBuilder,否则String
Math
public static void main(String[] args) {
System.out.println(Math.abs(-88) + "绝对值");
System.out.println(Math.pow(2, 3) + "幂");
System.out.println(Math.ceil(1.1) + " 1.1 向上取整");
System.out.println(Math.floor(1.1) + " 1.1 向下取整");
System.out.println(Math.round(1.1) + " 1.1 四舍五入");
System.out.println(Math.sqrt(4) + "开方");
System.out.printf("%.2f + 随机数",10*(Math.random()));
System.out.println();
System.out.println(Math.max(6, 89));
System.out.println(Math.min(67, 8));
}
Arrays
public static void main(String[] args) {
Integer []integer = {1,5,9,4,7};
System.out.println("字符串形式"+Arrays.toString(integer));
Arrays.sort(integer);
System.out.println("排序:");
for (Integer integer1 : integer) {
System.out.print(" "+integer1);
}
System.out.println();
System.out.println("二分查找" + Arrays.binarySearch(integer, 9));
//复制
Integer[] integers = Arrays.copyOf(integer, integer.length);
System.out.println(Arrays.toString(integer));
System.out.println();
//填充
Arrays.fill(integer,99);
System.out.println(Arrays.toString(integer));
//比较
int[] ints = {1,2,3,4,5,6};
int[] ints1 = {1,2,3,4,5,6,7};
System.out.println(Arrays.equals(ints, ints1));
//集合
List<Integer> asList = Arrays.asList(1, 3, 4, 5, 56, 6);
System.out.println("asList:"+asList);
}
System
public static void main(String[] args) {
int a = 1;
System.out.println(a);
// System.exit(a);//退出当前程序
System.out.println(a);
System.out.println("======");
int[] b = {1,23,45};//复制
int[] b1 = new int[3];
System.arraycopy(b,0,b1,0,b.length);
System.out.println(Arrays.toString(b1));
System.out.println("======");
//计算当前时间与1970-1-1的毫秒数
System.out.println("计算当前时间与1970-1-1的毫秒数\n"+System.currentTimeMillis());
System.gc();
}
大数
public static void main(String[] args) {
//No.1 BigInteger 整数
BigInteger i1 = new BigInteger("11111111111111111111111111111111111111111111");
BigInteger i2 = new BigInteger("2");
System.out.println(i1.add(i2) + " 加");
System.out.println(i1.subtract(i2) + " 减");
System.out.println(i1.multiply(i2) + " 乘");
System.out.println(i1.divide(i2) + " 除");
System.out.println("=============");
//No.2 BigDecimal 小数
BigDecimal d1 = new BigDecimal("2.2222222222222222222222222");
BigDecimal d2 = new BigDecimal("2.24343245");
System.out.println(d1.add(d2));
System.out.println(d1.subtract(d2));
System.out.println(d1.multiply(d2));
System.out.println(d1.divide(d2, BigDecimal.ROUND_CEILING) + "保留d1的小数位数");
}
日期类
Date SimpleDateFormat
public static void main(String[] args) throws ParseException {
Date r = new Date();
System.out.println(r + "系统时间");
System.out.println(new Date(78787878) + " 通过毫秒数得到时间");
System.out.println(r.getTime() + " 通过时间获得毫秒数");
System.out.println(new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E").format(r) + "系统时间转换成格式规则日期");
System.out.println(new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E").parse("2021年12月30日 13:18:20 星期四") + "规则转系统时间");
}
Calendar
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
System.out.println(c);
System.out.println(c.get(Calendar.YEAR) + "年" + (c.get(Calendar.MONTH)+1) + "月" + c.get(Calendar.DATE) + "日");
System.out.println(c.get(Calendar.HOUR_OF_DAY) + "时" + c.get(Calendar.MINUTE) + "分" + c.get(Calendar.SECOND) + "秒");
}
LocalDate LocalTime LocalDateTime
public static void main(String[] args) {
System.out.println(LocalDate.now());
System.out.println(LocalTime.now());
System.out.println(LocalDateTime.now());
LocalDateTime t = LocalDateTime.now();
System.out.println(t.getYear());
System.out.println(t.getMonth());
System.out.println(t.getMonthValue());
System.out.println(t.getDayOfMonth());
System.out.println(t.getHour());
System.out.println(t.getMinute());
System.out.println(t.getSecond());
System.out.println(DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm:ss E").format(t));
System.out.println(DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm:ss E").format(t.plusMinutes(6666)) + "加上6666分钟的时间");
System.out.println(DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm:ss E").format(t.minusDays(34)) + "减去34天的时间");
}
Instant 时间戳
public static void main(String[] args) {
//和Date之间的相互转换
Instant now = Instant.now();
Date date = Date.from(now);
Instant instant = date.toInstant();
System.out.println(now);
System.out.println(date);
System.out.println(instant);
}
ateTimeFormatter.ofPattern(“yyyy年MM月dd日 hh:mm:ss E”).format(t.minusDays(34)) + “减去34天的时间”);
}
### Instant 时间戳
```java
public static void main(String[] args) {
//和Date之间的相互转换
Instant now = Instant.now();
Date date = Date.from(now);
Instant instant = date.toInstant();
System.out.println(now);
System.out.println(date);
System.out.println(instant);
}