类 正则表达式
Java类的方法
类的方法可以分为两种,一种是类方法,就是用static修饰的方法,在调用时可以直接用类名调用;另一种是对象方法,没有用static修饰方法,在调用时用对象调用,在同一个类一个方法调用另外一个方法时,如果调用的是非static方法,那默认使用this作为调用者
- 方法不能独立定义,只能在类里定义
- 从逻辑意义上看,方法要么属于该类本身,要不属于该类的一个对象
- 永远不能独立执行方法,执行方法使用类(static方法)或者对象作为调用者
变参方法,在形参的类型后加三点…
public class way {
//变长参数,在形参的类型后加三点...
private static void print(String... strs){
for(String str : strs){
System.out.println(str);
}
}
public static void main(String[] args) {
print("one","two","three","four");
}
}
Java类的初始化
class A{
static {
System.out.println("A static code");
}
{
System.out.println("A non-static code");
}
public A() {
System.out.println("A constructor");
}
}
public class B extends A{
static {
System.out.println("B static code");
}
{
System.out.println("B non-static code");
}
public B() {
System.out.println("B constructor");
}
public static void main(String[] args) {
new B();
}
}
执行结果:
A static code
B static code
A non-static code
A constructor
B non-static code
B constructor
初始化执行顺序
父类的静态代码块->子类的静态代码块->父类的代码块->父类的构造方法->子类的代码块->子类的构造方法
为什么是上面的顺序哪?我自己的想法是,前面两个很容易理解,静态方法在类加载阶段就已经执行了(cinit方法中),后面的四个方法是在创建类的对象时才开始,但是为什么父类的代码块和构造方法一起执行?在执行创建类的对象时会执行init方法,(init方法有代码块和构造方法合并产生)
常用类
Runtime类
public static void main(String[] args) throws IOException {
System.gc();//启动垃圾回收
Runtime runtime = Runtime.getRuntime();
//下面的不是硬件的内存大小,是虚拟机从操作系统得到的大小
System.out.println("cpu核数"+runtime.availableProcessors());
System.out.println("空闲内存"+runtime.freeMemory()/1024/1024 +"MB");
System.out.println("总内存数"+runtime.totalMemory()/1024/1024 +"MB");
System.out.println("最大可用内存数"+runtime.maxMemory()/1024/1024 +"MB");
//可以允许windows下的一些命令,比如notepad打开记事本
runtime.exec("notepad");
}
ThreadLocalRandom
/**
* 当前线程的随机数字生成器孤立,在并发情况下代替Random
* */
public static void main(String[] args) {
ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
System.out.println(threadLocalRandom.nextInt());
System.out.println(threadLocalRandom.nextFloat());
System.out.println(threadLocalRandom.nextBoolean());
byte[] buffer = new byte[16];
threadLocalRandom.nextBytes(buffer);
System.out.println(Arrays.toString(buffer));
}
正则表达式
public class PatternDemo {
public static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
public static void main(String[] args) {
String mailOne = "1768031232@qq.com";
Pattern pattern = Pattern.compile(REGEX_EMAIL,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(mailOne);
boolean ok = matcher.matches();
System.out.println(mailOne+" : "+ok);
}
}
/**
* 正则:手机号(简单)
*/
public static final String REGEX_MOBILE_SMIPLE = "^[1]\\d{10}$";
/**
* 正则:手机号(精确)
* <p>移动:134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188</p>
* <p>联通:130、131、132、145、155、156、175、176、185、186</p>
* <p>电信:133、153、173、177、180、181、189</p>
* <p>全球星:1349</p>
* <p>虚拟运营商:170</p>
*/
public static final String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}$";
/**
* 正则:身份证号码15位
*/
public static final String REGEX_ID_CARD15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
/**
* 正则:身份证号码18位
*/
public static final String REGEX_ID_CARD18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9Xx])$";
/**
* 正则:邮箱
*/
public static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
/**
* 正则:URL
*/
public static final String REGEX_URL = "[a-zA-Z]+://[^\\s]*";
/**
* 正则:QQ号
*/
public static final String REGEX_TENCENT_NUM = "[1-9][0-9]{4,}";