23 Enumeration and String class

1. Enumeration

(1) Definition format: use enum instead of class
(2) Write the constant value directly in the class
(3) Enum modified classes are all static constants
(4) The new object cannot be directly assigned, it must be assigned directly

2. Packaging

(1) Each basic data type corresponds to a packaging class
(2) The int packaging class is integer, the char packaging class is character, and the
others are capitalized
(3) Construction method: each packaging class provides itself Corresponding data type and string (string must be parseable into corresponding type) as parameter construction method. (
Except for the char type, only supports the construction method of the symbol as a parameter)
(4) ***Value converts the reference data type to the basic data type, the prefix is ​​the name of the basic data type
(5) the toString method, which converts the corresponding packaging type a string
(6) parse *** convert a string to the corresponding basic data types, the string must be truly resolved to convert the case of a numerical value can, otherwise it reported Number the
FormatException, digital conversion abnormal usage: string a = "12345"; int b =
Integer.parseInt(a); System.out.println(b+1); Result: 12346
(7) valueOf
converts basic data types to reference data types
(8) automatically unboxing and packing Boxes, starting from jdk1.5, support mixed operations of packaging classes and basic data types. At present, it is convenient for us to write
① directly assign basic data types to objects, and automatically box ② directly assign reference data types to basic data types, and automatically disassemble Box Integer aaInteger =
Integer.valueOf(29);//Box Integer aaInteger = 29||integer aaInteger =
new Integer(29); int v = aaInteger.intValue();//拆箱 int v = aaInteger;

3. Math class
(1) Math class, which provides us with some mathematical formulas, two static constants, PI E
(2) The method of obtaining random numbers Math.Random() gets a double type 0.* decimal.

double random = Math.random();
		System.out.println((int)(random*10));

Note: random*10 must be enclosed, otherwise int random will convert the 0.* decimal of random to 0 so that no random number will be obtained

Interview questions:

Math.ceil() round up
Math.floor
() round down Math.round() round up

4. Random class

获取随机数
Random random = new Random();
System.out.println(random.nextInt(10));
System.out.println((int)(ran1.nextDouble()*10));//0-1的double数
System.out.println(ran1.nextFloat());//0-1的float数
System.out.println(ran1.nextBoolean());//true

5. String class // After modification, a new object is created, and the original object will not be changed unless StringBuffer is used

length() 获取字符串长度
equals() 比较字符串内容,重写与Object类
indexOf(String str ) 查找某个字符串第一次出现的位置
indexOf(int num) 根据ascii码表或者unicode码表查找对应数值所对应的字符串在
源字符串中的位置
lastIndexOf(String str) 跟indexOf一样,但是是查找的最后一个字符串
lastIndexOf(int num) 跟indexOf一样,但是是查找的最后一个字符串
subString(int beginIndex)表示从指定位置截取到字符串末尾
subString(int beginIndex , endIndex)表示从开始截取到指定结束位置,包前不包后
trim()去除字符串首位的空格
split(String str)根据指定的内容分割字符串,返回值是一个字符串数组,str不输出
contains(String str)查看源字符串是否包含某一个字符串,是返回true,否返回false
endsWith(Strinig str)判断字符串是否已str结尾
startsWith(String str)判断字符串是否已str开头
replace(String oldStr,String newStr)将oldStr替换为newStr
replaceAll(String regx,String newStr)根据正则表达式将匹配到的内容替换为newStr
concat(String str)将源字符串与str拼接,与 + 号拼接字符串效果一样
toLowerCase()将字符串转为小写
toUpperCase()将字符串转为大写
equalsIgnoreCase() 忽略大小写比较字符串内容
toCharArray()将字符串转换为char数组
charAt(int index)返回index下标对应的字符,char
getBytes()将字符串转换为byte数组

6. Constant pool

Integer packaging class and Character packaging class values ​​will not generate new objects in byte, so we useIf the comparison is true, it is also the comparison address. The String
type is directly assigned to create an object for the first time, and the string content will be stored in the constant pool. Creating a string with the same content for the second time will not generate a new object, so
use
Compare two strings that are directly assigned and have the same content, which is also true

byte(-128~127)
    Character ch3 = 128;
		Character ch4 = 128;
		System.out.println(ch3 == ch4);

7.StringBuffer&StringBuilder

The String class is an immutable object, so when we need to frequently modify the content of the string, it is recommended to use StringBuffer or StringBuilder

Interview question: What is the
difference between StringBuffer and StringBuilder?

The former is thread-safe and low-efficiency since JDK1.0, the latter is thread-safe and efficient, and later JDK1.5

StringBuffer sb1 = new StringBuffer();
		System.out.println("逻辑容量" + sb1.capacity());
		System.out.println("实际长度" + sb1.length());
		sb1.append(false);//添加内容
		sb1.append("abcd");
		sb1.append(20);
		sb1.append(20.0);
		System.out.println("改变内容以后的sb1" + sb1);
逻辑容量16
实际长度0
改变内容以后的sb1falseabcd2020.0
8.System类
1.System.err.println("打印出来的是红色的字体")2.System.currentTimeMillis();//获取系统时间毫秒数
3.System.gc();//垃圾回收机制
4.System.exit(0);//0表示正常退出,非零表示非正常退出

9.Runtime

// Runtime 运行时
// 此类提供了一些关于程序运行过程中信息获取的方法
// 比如 内存大小等 
System.out.println("JVM最大内存" + Runtime.getRuntime().maxMemory() / 1024 / 1024 );
		
System.out.println("JVM空闲内存" + Runtime.getRuntime().freeMemory() / 1024 / 1024);
		
System.out.println("JVM总内存" + Runtime.getRuntime().totalMemory() / 1024 / 1024);

10. java.util.Date class

The Date class provides some methods for obtaining time-related information, most of which have been deprecated and are not recommended. You can use

  Date date = new Date();
		System.out.println(date);
输出结果:Mon Aug 24 15:38:03 CST 2020
System.out.println("一个月中的第几天" + date3.getDate());
		System.out.println("一周中的第几天" + date3.getDay());
		System.out.println("月份,要加1" + date3.getMonth());
		System.out.println("小时" + date3.getHours());
		System.out.println("分钟" + date3.getMinutes());
		System.out.println("秒钟" + date3.getSeconds());

11.SimpleDateFormat class//The method of using the class is very simple, directly name the new class, such as the person class

     person person = new person()
本类可以将日期格式化
Date date = new Date();
SimpleDateFormat   sdf  = new SimleDateFormat("yyyy/MM/dd  HH:mm:ss");
System.out.println(sdf.format(date));

12.Calendar class (calendar)

本类也可以获取时间的相关信息,本类不能直接实例化

// Calendar 日历
		Calendar cal = Calendar.getInstance();
		System.out.println("一个月中的第几天" + cal.get(Calendar.DATE));
		System.out.println("一周中的第几天" + cal.get(Calendar.DAY_OF_WEEK));
		System.out.println("第几个周几" + cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
		System.out.println("月份" + cal.get(Calendar.MONTH));
		System.out.println("小时" + cal.get(Calendar.HOUR));
		System.out.println("分钟" + cal.get(Calendar.MINUTE));
		System.out.println("秒钟" + cal.get(Calendar.SECOND));

Guess you like

Origin blog.csdn.net/zyf_fly66/article/details/114091747