// javac java -jar
// byte char short int long float double boolean
// Byte Character Short Integer Long Float Double Boolean
// 支持 自动装箱/自动拆箱 valueOf缓存池装箱 typeValue装箱 parseType转类型
// private < default < protected < public
// static final abstract interface
// 接口属性均常量 编译器自动加 public static final 必须现场赋值
// throws写在方法签名上往外抛 throw现场抛出异常
// Throwable - Error[程序无法处理] / Exception [受检] / RuntimeException [非受检]
int[] arr = new int[10];
int[] arr2 = {1, 2, 3, 4, 5, 6};
String str = "HelloJava";
char[] charArray = str.toCharArray();
str.length();
str.substring(0, 5);
str.contains("Java");
StringBuilder sb = new StringBuilder("");
sb.append("a").append("b");
sb.delete(start_idx, end_idx); // [start_idx, end_idx)
int len = sb.length();
String s = sb.toString();
BigDecimal number = new BigDecimal("0");
BigDecimal otherNumber = new BigDecimal("0");
number.add(otherNumber);
number.subtract(otherNumber);
number.multiply(otherNumber);
number.divide(otherNumber);
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timeStr = now.format(dtf);
Collection (单列集合接口)
|—— List 有序可重复
| |__ ArrayList 数组实现 查快
| |__ LinkedList 链表实现 增删快
|__ Set 无序不可重复
|__ HashSet 纯无序
|__ LinkedHashSet 维护插入顺序 (内部使用双链表)
|__ TreeSet 自动排序 自然序
Map (双列键值对接口)
|__ HashMap 纯无序
|__ LinkedHashMap 维护插入顺序
|__ TreeMap 自动按 key 排序 自然序
for (Object item : collection) {}
Iterator iter = collection.iterator();
while (iter.hasNext()) { Object item = iter.next() }
ListIterator listIter = list.listIterator();
while (listIter.hasNext() && listIter.hasPrevious()) {
Object item = listIter.next();
Object item = listIter.previous();
}
List<Integer> res = List.of(1, 2, 3, 4, 5).stream()
.filter(n -> n % 2 == 0)
.map(n -> n * 2)
.toList();
// 方法引用
list.forEach(System.out::println);
// 字节流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("path"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("path", true)); // true = 追加
// 字符流
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream("in.txt"), StandardCharsets.UTF_8));
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream("out.txt", true), StandardCharsets.UTF_8));
// Hutool
// in / out 使用以上对应 输入流 和 输出流 即可
List<String> lines = IoUtil.readLines(in, "UTF-8", new ArrayList<>());
String content = IoUtil.readUtf8(in); // 读完 文本类文件
byte[] bytes = IoUtil.readBytes(in); // 读完 二进制文件
// 写入
IoUtil.writeUtf8(content, false, out); // 内容, 是否追加, 输出流
IoUtil.copy(in, out); // 拷贝流
IoUtil.close(in); // 关闭流
enum ErrorCode {
// 一行就是一条预设好的记录
SUCCESS(0, "ok", ""),
PARAMS_ERROR(40000, "请求参数错误", ""),
NULL_ERROR(40001, "请求数据为空", ""),
NOT_LOGIN(40100, "未登录", ""),
NO_AUTH(40101, "无权限", ""),
SYSTEM_ERROR(50000, "系统内部异常", "");
// 三个"格子",用来存上面每一行的3个信息
private final int code;
private final String message;
private final String description;
// 构造:就是把括号里的三个值,放进上面三个格子里
ErrorCode(int code, String message, String description) {
this.code = code;
this.message = message;
this.description = description;
}
}
// AOP
@Aspect
@Component
public class MyAnnotationAspect {
@Pointcut("@annotation(com.example.project.annotation.MyAnnotation)")
public void pointcut() {}
@Around("pointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
MyAnnotation annotation = signature.getMethod().getAnnotation(MyAnnotation.class);
String attr = annotation.attr(); // 读注解属性
long start = System.currentTimeMillis(); // 前置增强
Object result = joinPoint.proceed(); // 执行目标方法
long cost = System.currentTimeMillis() - start;
System.out.println("方法耗时:" + cost + "ms");
return result;
}
}
// JUnit
// 单元测试 --> 集成测试
public class UserServiceTest {
@BeforeAll
public static void testBeforeAll() {
System.out.println("所有测试开始");
}
@AfterAll
public static void testAfterAll() {
System.out.println("所有测试结束");
}
@BeforeEach
public void testBefore() {
System.out.println("测试开始");
}
@AfterEach
public void testAfter() {
System.out.println("测试结束");
}
@Test // @Test 标记的方法必须为 public void 修饰 底层原因与规范
@DisplayName("测试获取用户年龄") // 指定测试名字
public void testGetAge() {
Integer age = new UserService().getAge("id");
Assertions.assertEquals(18, age, "两个值相等");
Assertions.assertNotEquals(16, age, "两个值不相等");
Assertions.assertNull(age, "值为 null");
Assertions.assertNotNull(age, "值不为 null");
Assertions.assertTrue(age > 16, "大于 16"); // 检查条件是否为true
Assertions.assertFalse(age < 16, "小于 16"); // 检查条件是否为false
Assertions.assertSame(age, age, "两个对象引用相等"); // 检查两个对象引用地址是否相等
}
}
@SpringBootTest
class UserServiceTest {
@MockBean
UserDao userDao; // 自动替换掉容器里真实的 UserDao
@Autowired
UserService userService; // 自动注入,里面已经用的是假 UserDao
}