常用类
Math
1@Test
2 public void randomHeroTesting() {
3
4 //计算平⽅根
5 System.out.println(Math.sqrt(16));
6 //计算⽴⽅根
7 System.out.println(Math.cbrt(8));
8 //两个数的最⼤,⽀持int, long, float,double
9 System.out.println(Math.max(2.9,4.5));
10 System.out.println(Math.min(2.9,4.5));
11 //ceil向上取整,更⼤的值⽅向靠拢, 中⽂是天花板
12 System.out.println(Math.ceil(19.7));
13 System.out.println(Math.ceil(-20.1));
14 //floor向下取整,更⼩的值⽅向靠拢,中⽂是地板意思
15 System.out.println(Math.floor(19.7));
16 System.out.println(Math.floor(-20.1));
17
18 List<String> list = Arrays.asList("Pandarenbrewer",
19 "Flame Lord",
20 "Goblin Alchemist",
21 "Pit Lord",
22 "Dark Ranger",
23 "BEASTMASTER",
24 "Tinker",
25 "Naga Seawitch");
26
27 Random random = new Random();
28 //该方法生成从0(包括)到n(不包括)之间的随机整数,是一个伪随机数,并不是真正的随机数。
29 System.out.println(list.get(random.nextInt(8)));
30
31 //System.out.println(Math.random()); //⼩于1⼤于0的double类型的数
32 //产⽣1到10的随机数,int⽅法进⾏转换它会去掉⼩数掉后⾯的数字即只获取整数部分,不是四五⼊
33 //int random=(int)(Math.random()*10+1);
34 }
String
创建字符串对象的两种方法
1// 始终会在堆中开辟新的内存空间
2String str= new String("abc");
3
4//字面量(会引用常量池中已存在的地址)
5String str = "abc";
字符串比较内容是否相等
- == 是 ⽐较地址
- equals()内容是否相等需要 ⽤
常用 API
1@Test
2 public void stringTesting() {
3 String str = "abc";
4 String str2 = "dfg";
5 String str3 = "ABC";
6 String str4 = "a,b,c,d";
7 String str5 = " a b c d ";
8
9 //获取字符串的长度
10 System.out.println(str.length());//3
11
12 //通过下标获取字符
13 char c = str.charAt(1);
14 System.out.println(c);//b
15
16 //字符串比较
17 boolean result = str.equals(str2);//false
18 System.out.println(result);
19
20 //字符串比较忽略大小写
21 boolean result2 = str.equalsIgnoreCase(str3);//true
22 System.out.println(result2);
23
24 //查找字符串出现的位置
25 int index = str.indexOf("b");//1
26 System.out.println(index);
27
28 //字符串截取(从指定下标开始截取到末尾)
29 String substring = str.substring(1);//bc
30 System.out.println(substring);
31
32 //字符串截取(指定范围,包括开头不包括结尾)
33 String substring1 = str.substring(1, 2);//b
34 System.out.println(substring1);
35
36 //字符串拆分 特殊字符注意转义 ("\\.")
37 String[] split = str4.split(",");
38 for (String s : split) {
39 System.out.println(s);// a b c d
40 }
41
42 //字符串替换
43 String replace = str.replace("c", "x");//abx
44 System.out.println(replace);
45
46 //大写转换
47 String s = str.toUpperCase();//ABC
48 System.out.println(s);
49
50 //小写转换
51 String s1 = s.toLowerCase();//abc
52 System.out.println(s1);
53
54 //去除空格
55 String trim = str5.trim();//a b c d
56 System.out.println(trim);
57
58 //其他类型和字符串互相转换
59 boolean aBoolean = Boolean.getBoolean("false");
60 Integer integer = Integer.parseInt("20");
61 long aLong = Long.parseLong("1024");
62 float f = Float.parseFloat("3.14159");
63 double aDouble = Double.parseDouble("1.423123123");
64 }
System
位于 java.lang 包中,它是系统类,代表程序所在系统,提供了对应的 ⼀些系统属性信息和系 统操作。
1//输⼊输出包含三个成员变量,分别是in,out,err
2System.out //常⽤调试
3System.in //⽤于数据读取,少⽤
4System.err //⽤在错误输出,少⽤
5
6//获取系统当前毫秒值
7System.currentTimeMillis()
8
9//获取系统环境的属性
10System.getProperties()
11 Properties properties = System.getProperties();
12 Set<Map.Entry<Object, Object>> entrySet = properties.entrySet();
13 for (Map.Entry<Object, Object> entry : entrySet) {
14 System.out.println(entry.getKey() + "=" + entry.getValue());
15 }
16
17//根据指定key获取系统属性
18System.getProperties(key)
19 System.out.println(System.getProperty("os.name")); //Windows 10
包装数据类型
Java 是 ⼀个 ⾯相对象的编程语 ⾔,但基本类型并不具有对象的性质,为了让基本类型也具有 对象的特征,就出现了包装类型。
集合框架 ⾥ ⾯需要存储对象,不能存储基本数据类型,所以需要存储包装类型。
对照表
1基本类型 包装器类型
2boolean Boolean
3char Character
4int Integer
5byte Byte
6short Short
7long Long
8float Float
9double Double
基本数据类型与包装类型互相转换
1@Test
2 public void wrapperTesting(){
3 //int => Integer
4 int i = 0;
5 Integer integer = new Integer(i);
6 //Integer => int
7 Integer integer2 = new Integer(0);
8 int i1 = integer2.intValue();
9
10 //boolean => Boolean
11 Boolean aBoolean = new Boolean(false);
12 //Boolean => boolean
13 boolean booleanValue = aBoolean.booleanValue();
14 }
基本数据类型和包装数据类型区别
- 基本数据类型不 ⽤ new, 包装类型需要使 ⽤ new 关键字来在堆中分配存储空间。
- 存储 ⽅式及位置不同,基本类型是直接将变量值存储在栈中,包装类型是将对象放在堆中, 然后通过引 ⽤来使 ⽤。
- 初始值不同,基本类型的初始值如 int 为 0,boolean 为 false,包装类型的初始值为 null。
System.in、Scanner
位于 java.util 包下,可以通过 Scanner 类来获取 ⽤户的输 ⼊。
常用 API
1//获取输⼊的字符串,以回⻋换⾏符为结束标识
2public String nextLine()
3
4//获取输⼊的整数
5public int nextInt()
6
7//还有nextShort, nextFloat, nextDouble
用法
1//构造函数传⼊输⼊流
2public Scanner(InputStream source)
3
4//传⼊键盘输⼊流
5Scanner s = new Scanner(System.in);
示例
1public class ScannerTesting {
2 public static void main(String[] args) {
3 Scanner scanner = new Scanner(System.in);
4
5 while (true) {
6 System.out.println("请输入字符串:");
7 String input = scanner.nextLine();
8 if ("886".equalsIgnoreCase(input)) {
9 System.out.println("再见!");
10 break;
11 } else {
12 System.out.println("您输入的是:"+ input);
13 }
14 }
15 scanner.close();
16 }
17}