制表符: \t 经常忘记,这个东西可以让表格更规整
long类型的数据后面要加一个大写的L,告诉编译器
键盘录入
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
逻辑异或^
JDK12的case新特性:case1 -> 执行方法, 用箭头省略了break;
生成随机数:
Random r = new Random();
int number = r.nextInt(x) //表示0到x-1
数组:int[] arr = new int[] 数组的地址值表示数组在内存中的位置
数组可以通过下标进行访问
java内存分配:以下所有都在JVM里面
栈:方法运行时使用的内存
堆:存储对象或者数组,new创建的都在堆
方法区(已经变为了元空间),存储可以运行的class文件
本地方法栈:JVM使用
寄存器:CPU使用
基本数据类型:数据值存储在自己的空间中
引用数据类型:数据值存储在其他空间中,自己空间中存储的是地址值
String类
字符数组转字符串
byte[] bytes = {,,,,};
String s = new String(bytes);
字符串内容比较:区分大小写用equals,不区分用equslsIgnoreCase
数组的长度:数组名.length, 字符串的长度:字符串对象.length();
取出字符串中的每个位置值用str.charAt(i)
StringBuilder:方便进行字符串的拼接,可以看成是一个容器,创建之后里面的内容是可变的
StringBuilder sb = new StringBuilder("abc")
sb.reverse(反转),sb.length()(获取长度) sb.append(x)(添加数据) sb.toString(变回字符串)
StringJoiner: 拼接字符串中间插入东西比较方便
StringJoiner sj = new StringJoiner(",", "[", "]")
static修饰静态变量,推荐类名调用。
子类不能继承父类的构造方法,但是可以通过super方法调用
子类构造方法的第一行,有一个默认的super()
默认先访问父类的无参构造,再执行自己
如果想要访问父类有参构造,必须手动书写
调用兄弟构造器:解决默认初始化参数问题:
public Student(String name, int age) {//默认学校黑马程序员 复用兄弟构造器里的代码//不能同时写super()和this() 会调用两次父类构造器this(name, age,"黑马程序员");}public Student(String name, int age, String schoolName) {this.name = name;this.age = age;this.schoolName = schoolName;}
强制类型转换检查:a instantof Cat c 先判断a是否是Cat类型,如果是就强转,强转以后变量名为c
局部代码块:写在方法中的:{} 变量只在所属的代码块中有效
构造代码块:{} 优于构造方法前执行
静态代码块:static{} 类加载而加载,只执行一次
接口新增方法:
默认方法:(public) default
静态方法:(public) static 只能通过接口名调用
私有方法:private
类的无法成员:属性,方法,构造方法,代码块,内部类
内部类:成员内部类、静态内部类、局部内部类+
匿名内部类:new 类名或者接口名(){重写方法;};
集合转成数组list.toArray(); 转换成指定类型的数组:toArray(new String[c.size()])
集合中的数据导入到另一个a.addAll(b)
只有单列集合和数组才能用增强for遍历
不可变集合:创建时加of------List.of
注:如果是map的话,键值对数量最多是十个,超过10个用Map.copyOf方法
Stream流的中间方法:
Filter:过滤;limit:获取前几个元素;skip:跳过前几个元素;distinct:元素去重;
concat:合并a和b两个流为一个流;map:转换流中的数据类型
Stream流的终结方法:
toArray:收集放到数组中;collect:收集放到集合中;forEach:遍历;count:统计
方法引用:
类名::静态方法------方法的形参和返回值要和抽象方法一样,功能也需要一样
对象::成员方法------
类型::方法
类名::new
单例设计模式:
懒汉式单例:
public class B {//懒汉式单例//把类的构造器私有private B() {}//定义一个类变量,用于存储这个类的一个对象private static B b;//定义一个类方法,这个方法保证第一次调用时才创建一个对象,后面调用都会用这同一个对象返回public static B getInstance() {if(b == null) {b = new B();}return b;}
}
饿汉式单例
//饿汉式单例 在获取类的对象时 对象已经创建好了
public class A {//2定义一个类变量记住类的一个对象 类变量属于这个类共享 所以只会加载一次private static final A a = new A();//1私有类的构造器private A() {}//3定义一个类方法返回类的对象public static A getObject() {return a;}
}
枚举类的额外API:A.values();拿到全部对象 A.valueOf()通过名字创建
基本数据类型转换:基本转为对象Integer.valueOf 字符串转换为基本类型:Integer.parseInt
基本转字符串 a + ‘‘’’/a.toString
valueOf可以转换很多东西:将double转为bigdecimal --- BigDecimal.valueOf()
jdk8新增的时间:修改用with,指定用of
合并Date和Time用of
public class Test4_ZoneId_ZoneddateTime {public static void main(String[] args) {//获取系统默认逝去ZoneId zoneId = ZoneId.systemDefault();System.out.println(zoneId.getId());System.out.println(zoneId); //一样的//获取全部时区idSystem.out.println(ZoneId.getAvailableZoneIds());//把时区id封装成zoneid对象ZoneId zoneid1 = ZoneId.of("America/New_York");//带时区的时间ZonedDateTime now = ZonedDateTime.now(zoneid1);System.out.println(now);ZonedDateTime now1 = ZonedDateTime.now(Clock.systemUTC()); //获取世界标准时间ZonedDateTime now2 = ZonedDateTime.now(); //系统当前时间}
}
public class Test5_instant {public static void main(String[] args) {Instant now = Instant.now(); //不可变对象//获取总秒数long second = now.getEpochSecond();System.out.println(second);//不够一秒的纳秒数int nano = now.getNano();System.out.println(nano);Instant instant = now.plusNanos(111);//instant作用,程序性能分析Instant now1 = Instant.now();Instant now2 = Instant.now();}
}
日期格式化:先创建DateTimeFormatter.ofPattern(-----------)
formatter.format(now) / now.format(formatter)
解析时间LocalDateTime.parse(dateStr,formatter)
period:Period.between(a,b)
duration:加入了时分秒
Arrays:
得到数组内容:Arrays.toString
拷贝数组:Arrays.copyOfRange(arr,begin,end) 左闭右开
指定长度拷贝数组,多退少补:Arrays.copuOf(arr,num)
更新数组:Arrays.setAll(arr,方法)
数组排序:Arrays.sort(arr)
lambda表达式只能简化只有一个抽象方法的匿名内部类
参数类型可以省略 如果只有一个参数,小括号也可以省略 如果只有一行代码 可以省略大括号,分号也得省 return也得省
//1. 取绝对值 接收整数浮点数System.out.println(Math.abs(-12));//2. 向上取整System.out.println(Math.ceil(4.00001));//3. 向下取整System.out.println(Math.floor(4.999));//4. 四舍五入 只看第一位小数System.out.println(Math.round(3.499));//5. 最大最小值System.out.println(Math.max(1, 2));System.out.println(Math.min(4, 5));//6. 取次方System.out.println(Math.pow(2, 3)); //2的三次方//7. 取随机数 包前不包后 【0-1)System.out.println(Math.random());//1. 终止运行的虚拟机 java程序挂掉public static void main(String[] args) {//System.exit(0); //人为终止虚拟机 不要使用//2.取回当前系统的时间 毫秒值long time = System.currentTimeMillis();System.out.println(time);for (int i = 0; i < 1000000; i++) {System.out.println(i);}long time2 = System.currentTimeMillis();System.out.println((time2-time)/1000.0); //程序性能分析// Runtime 是一个单例类 通过调用方法得到对象(运行时对象)Runtime r = Runtime.getRuntime();//2. 终止当前运行的虚拟机//r.exit(0);//3. 获取虚拟机能用的处理器数System.out.println(r.availableProcessors());//4. 返回虚拟机中的内存数量System.out.println(r.totalMemory()/1024); //得到k//5.返回虚拟机中的可用内存量System.out.println(r.freeMemory()/1024);//6. 启动某个程序 返回该程序的对象Process p = r.exec("D:\\QQ音乐\\QQMusic\\QQMusic1942.08.54.16\\QQMusic.exe");Thread.sleep(5000); //暂停五秒//关闭程序p.destroy();
字节流:
字节输入流:FileInputStream
byte[] buffer = new byte[3];int len; //记录每次读取多少个字节while ((len = is.read(buffer)) != -1) {String rs = new String(buffer, 0, len);System.out.print(rs);}byte[] buffer = is.readAllBytes();System.out.println(new String(buffer));
字节输出流:FileOutputStream
byte[] bytes = "我爱你中国abc".getBytes();os.write(bytes);
字符输入流:FileReader
char[] buffer = new char[3];int len;while ((len=fr.read(buffer))!=-1){System.out.print(new String(buffer, 0, len));}
字符输出流:FileWriter
char[] buffer = {'h','8','k'};fw.write(buffer);
字节缓冲流:BufferedInputStream
字符缓冲输入输出流:BufferedReader/BufferedWriter----readLine/newLine
字节输入转换流:InputStreanReader
字节输出转换流:OutputStreamWriter
打印流:PrintStream:ps.println()
将系统默认的打印流对象改为自己设置的打印流:System.setOut(new PrintStream("D:\\code\\javasepromax\\io-app2\\src\\itheima01.txt"));
数据流:DateOutputStream
DataOutputStream dos = new DataOutputStream(new FileOutputStream("D:\\code\\javasepromax\\io-app2\\src\\itheima01.txt"));
dos.writeInt(1);
dos.writeDouble(9.4);
dos.writeUTF("ssdd");
dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream("D:\\code\\javasepromax\\io-app2\\src\\itheima01.txt"));int i = dis.readInt();System.out.println(i);double b = dis.readDouble();System.out.println(b);String rs = dis.readUTF();System.out.println(rs);dis.close();
特殊类:FileUtils,解决一切问题