类的方法
2022-09-23 01:29:30 0 举报
AI智能生成
类的方法
作者其他创作
大纲/内容
Object
boolean equals(Object obj);
public native int hashCode();
String toString();
Class getClass();
void finalize();
Object clone
System.gc();
封装类
整数系列:Byte/Short/Integer/Long
System.out.println(Byte.MAX_VALUE);
System.out.println(Byte.MIN_VALUE);
把字符串转换成对应的类型
Byte b1 = Byte.valueOf("123");
byte b2 = Byte.parseByte("11");
Integer i1 = Integer.valueOf("213");
int i2 = Integer.parseInt("456");
Float/Double
System.out.println(Float.MAX_VALUE);
System.out.println(Float.MIN_VALUE);
把字符串转换成对应的类型
Float f1 = Float.valueOf("132.432");
float f2 = Float.parseFloat("123.456");
把一个整数转成二进制字符串
System.out.println(Integer.toBinaryString(10));
判断字符的类型
是否是字母
Character.isAlphabetic('+')
Character.isLetter('9')
是否是数字
Character.isDigit('a')
是否是空白
Character.isWhitespace('\t')
是否是大写
Character.isUpperCase('a')
是否是小写
Character.isLowerCase('A')
转成大写
Character.toUpperCase('z')
转成小写
Character.toLowerCase('Z')
System
数据拷贝
System.arraycopy(src, srcPos, dest, destPos, length);
从计算机元年到当前经过的毫秒数
System.currentTimeMillis()
计算机元年 1970-1-1 0:0:0
jvm调用垃圾收集,不会立刻执行
System.gc();
退出虚拟机
System.exit(0);
输入的字节流
System.in/System.out/System.err
日期类
Date
当前日期
Date d1 = new Date();
从计算机元年经过3239371932791L毫秒数到的日期
Date d2 = new Date(3239371932791L);
判断日期的前后
System.out.println(d1.before(d2));
System.out.println(d1.after(d2));
local本地的
返回本地的当前时间
LocalDate d1 = LocalDate.now();
指定年月日的日期
LocalDate d2 = LocalDate.of(2000, 2 , 29);
指定时分秒的时间
LocalTime t2 = LocalTime.of(16,18,55);
日期时间类
LocalDateTime dt = LocalDateTime.now();
bigNumber大数字
BigInteger i1 = new BigInteger("123123212313213213123131313213132132131");
BigInteger i2 = new BigInteger("123123212313213213123131313213132132131");
BigDecimal大小数
BigDecimal j1 = new BigDecimal("123123212313213213123.131313213132132131");
BigDecimal j2 = new BigDecimal("123123212313213213123.131313213132132131");
快捷键:Alt + Shift + A;同时修改多行
SimpleDateFormat
简单的日期格式化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:S a E");
把字符串解析成具体日期
Date parse = sdf.parse(str);
String的非静态
求长度
int length = "hello".length();
根据位置获取字符
char charAt = "hello".charAt(0);
根据字符获取第一次发现位置, 找不到返回-1
int indexOf = "hello".indexOf('l');
从下标5开始往后找第一个l位置
int indexOf3 = "hello world".indexOf('l', 5);
从后往前找
int indexOf5 = "hello".lastIndexOf('l');
从下标5开始往前找第一个l位置
int indexOf7 = "hello world".lastIndexOf('l', 5);
包含
boolean contains = "hello world".contains("or");
是否相等
boolean equals = "hello".equals("HELLO");
boolean eic = "hello".equalsIgnoreCase("HELLO");
截取字串, 从下标4往后截取
String substring = "hello world".substring(4);
从哪到哪 [4,8)
String substring2 = "hello world".substring(4, 8);
比较大小,一样0,对象大返回正,参数大返回负数
int compareTo = "hello".compareTo("hzwew");
忽略大小写比较大小
int compareTo2 = "hello".compareToIgnoreCase("hzwew");
切分字符串, 切分后返回数组,不包括切分符号
String[] split = "www.baidu.com".split("[.]");
去掉字符串两边空白
String trim = " hello world ".trim();
判断是否为空
boolean empty = "".isEmpty();
转换大小写
String upperCase = "hello".toUpperCase();
String lowerCase = "HELLO".toLowerCase();
是否以开头和结尾
boolean startsWith = "hello".startsWith("he");
boolean endsWith = "hello".endsWith("he");
替换
String replaceAll = "h2e3l4l5o6".replaceAll("\\d", "");
String replaceFirst = "hello".replaceFirst("l", "w");
返回byte数组
byte[] bytes = "abcde".getBytes();
返回char数组
char[] charArray = "abcde".toCharArray();
String的静态
将其他类型转成字符串
String valueOf = String.valueOf(true);
字符串链接
String s = "aaa";
String concat = s.concat("bbb");
String concat = s.concat("bbb");
String join = String.join("-","abc","defg","haha");
格式化
String format = String.format("%s,%d,%f,%c,%b", "haha",100,1.1,'a',true);

收藏
0 条评论
下一页