System
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| System.out.println("hello");
Scanner sc = new Scanner(System.in);
long time = System.currentTimeMillis(); System.out.println(time);
System.exit(0); System.out.println("程序结束了");
|
String
String介绍
class final String 是一个最终类,不能被继承
final char value[]:用于存储字符串中的每个字符
字符串是一个不可变的
常量池中不会将相同的数据保存多份的
常量池中的字符串数据也是不可变的
通过字面量形式定义的字符串变量,变量里面保存的是字符串数据在常量池中的地址
通过new形式创建的字符串变量,变量保存的是在堆区创建的String对象地址,对象中value属性指向字符串数据在常量池中的地址
常用API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| int length() !! char charAt(int index) !! boolean isEmpty() String toLowerCase() String toUpperCase() String trim() boolean equals(Object obj) !! boolean equalsIgnoreCase(String anotherString) String concat(String str) int compareTo(String anotherString) String substring(int beginIndex) !! String substring(int beginIndex, int endIndex)
boolean endsWith(String suffix) :判断字符串是否已什么结尾
boolean startsWith(String prefix):判断字符串是否已什么开头 boolean contains(CharSequence s):是否包含什么 !!
int indexOf(String str) :获取指定str在字符串中的起始下标 !!
String replace(CharSequence target, CharSequence replacement) String replaceAll(String regex, String replacement)
String[] split(String regex):按照指定的regex进行字符串分割,进分割后的子串以字符串数组返回 !!
|
字符串和数组之间的相互转换
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Test public void t3() throws UnsupportedEncodingException { String str = "I love 中国"; byte[] bytes = str.getBytes("utf8"); for (byte b : bytes) { System.out.println(b); } String str1 = new String(bytes, "utf8"); System.out.println(str1); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Test public void t2() { String str = "I love 中国"; byte[] bytes = str.getBytes(); for (byte b : bytes) { System.out.println(b); } String str1 = new String(bytes); System.out.println(str1); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Test public void t1() { String str = "I love 中国"; char[] array = str.toCharArray(); for (char c : array) { System.out.println(c); } String str1 = new String(array); System.out.println(str1); }
|
StringBuffer
String因为是不可变的,有的时候会带来一些性能问题
StringBuffer & StringBuilder是可变的,就是说可以在原有基础上进行更改,不用频繁创建字符串对象
日期类
Date
Date:日期
可以获取相关日期信息,年月日时分秒
1 2 3 4 5
| Date date = new Date();
System.out.println(date.getTime());
System.out.println(date);
|
日期格式化类 format:可以将date对象转成指定格式的字符串 parse:将字符串再转回成date对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| SimpleDateFormat sdf = new SimpleDateFormat(); String time = sdf.format(new Date()); System.out.println(time);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss E"); String time2 = sdf2.format(new Date()); System.out.println(time2);
Date date = sdf2.parse("2019-09-29 16:16:16 星期日"); System.out.println(date);
|
Calendar
Calendar:日历类
1 2 3 4 5 6 7 8 9 10 11 12 13
| Calendar calendar = Calendar.getInstance();
System.out.println(calendar.get(Calendar.YEAR)); System.out.println(calendar.get(Calendar.MONTH)); System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); System.out.println(calendar.get(Calendar.HOUR)); System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); System.out.println(calendar.get(Calendar.MINUTE)); System.out.println(calendar.get(Calendar.SECOND));
Date time = calendar.getTime(); System.out.println(time);
|
算数类
Math
Math:数学类,提供了很多数学相关的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| System.out.println(Math.PI); System.out.println(Math.abs(-12));
System.out.println(Math.ceil(12.1)); System.out.println(Math.floor(12.6)); System.out.println(Math.round(12.5));
System.out.println(Math.pow(2, 3)); System.out.println(Math.sqrt(4));
System.out.println(Math.sin(Math.PI/6));
System.out.println(Math.max(2, 5)); System.out.println(Math.min(2, 5));
|
Random
Random:随机数类
1 2 3 4
| Random random = new Random(); System.out.println(random.nextInt()); System.out.println(random.nextInt(10));
|
超级数字
BigInteger
1 2 3 4 5 6 7 8
| BigInteger bint1 = new BigInteger("10000000000000000000"); BigInteger bint2 = new BigInteger("20000000000000000000");
BigInteger data1 = bint1.add(bint2); BigInteger data2 = bint1.subtract(bint2); BigInteger data3 = bint1.multiply(bint2); BigInteger data4 = bint1.divide(bint2);
|
BigDecimal
1 2 3 4 5 6 7 8 9
|
BigDecimal x = new BigDecimal("0.1"); BigDecimal y = new BigDecimal("0.2");
BigDecimal data = x.add(y); BigDecimal data2 = x.subtract(y); BigDecimal data3 = x.multiply(y); BigDecimal data4 = x.divide(y);
|
Arrays
1 2 3 4 5 6 7 8 9 10 11 12
| int[] data = {52,96,85,45,86,25}; String[] data2 = {"dfg","sfg","gre","sdfgr"}; Arrays.sort(data); for(int i : data) { System.out.println(i); }
String str1 = Arrays.toString(data);
int[] copy = Arrays.copyOf(data, 10);
|
Comparable
排序的对象必须实现Comparable接口
内部调用了重写的compareTo方法进行对象间的比较
1 2 3 4 5 6 7 8 9 10 11
| public class Goods implements Comparable<Goods> {
@Override public int compareTo(Goods goods) { return price - goods.price; } }
|
1 2 3 4 5 6 7 8 9 10
|
Arrays.sort(goods, new Comparator<Goods>() { @Override public int compare(Goods o1, Goods o2) { return o1.xiaoLiang - o2.xiaoLiang; } });
|