当前位置:首页 > java > 正文

最简单的100个java代码

  • java
  • 2024-05-01 13:19:20
  • 3269

1. 创建和打印字符串: String str = "Hello World"; System.out.println(str);
2. 定义和初始化整数: int num = 10;
3. 使用加法运算符: int sum = num + 5;
4. 使用除法运算符: double avg = num / 2.0;
5. 使用比较运算符: boolean isGreater = num > 5;
6. 使用 if-else 语句: if (num % 2 == 0) { ... } else { ... }
7. 使用 switch-case 语句: switch (num) { case 1: ...; case 2: ...; }
8. 使用 for 循环: for (int i = 0; i < 5; i++) { ... }
9. 使用 while 循环: while (num > 0) { ... }
10. 使用 do-while 循环: do { ... } while (num > 0);
11. 使用数组: int[] arr = {1, 2, 3, 4, 5};
12. 遍历数组: for (int element : arr) { ... }
13. 定义类: class MyClass { ... }
14. 创建类对象: MyClass obj = new MyClass();
15. 访问类成员: obj.name = "John Doe";
16. 调用类方法: obj.printName();
17. 继承类: class ChildClass extends ParentClass { ... }
18. 覆盖父类方法: @Override public void myMethod() { ... }
19. 使用接口: interface MyInterface { ... }
20. 实现接口: class MyClass implements MyInterface { ... }
21. 使用匿名类: Runnable runnable = new Runnable() { @Override public void run() { ... } };
22. 使用 lambda 表达式: Runnable runnable = () -> { ... };
23. 使用线程: Thread thread = new Thread(runnable);
24. 启动线程: thread.start();
25. 使用同步块: synchronized (lock) { ... }
26. 使用死锁检测: ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
27. 处理异常: try { ... } catch (Exception e) { ... }
28. 抛出异常: throw new Exception("Message");
29. 使用 finally 块: finally { ... }
30. 使用集合(列表): List list = new ArrayList<>();
31. 使用集合(集合): Set set = new HashSet<>();
32. 使用集合(映射): Map map = new HashMap<>();
33. 使用泛型: List list = new ArrayList<>();
34. 使用自动装箱和拆箱: int num = 5; Integer obj = num;
35. 使用 var 关键字: var name = "John Doe";
36. 使用记录类: record Person(String name, int age) { ... }
37. 使用 switch 表达式: int result = switch (num) { case 1 -> 10; case 2 -> 20; default -> 0 };
38. 使用模式匹配: if (obj instanceof String str) { ... }
39. 使用文本块: String str = """ Hello
World! """;
40. 使用 null 合并运算符: String name = user.getName() == null ? "Unknown" : user.getName();
41. 使用 ternary 操作符: boolean isEven = (num % 2 == 0) ? true : false;
42. 使用 assert 关键字: assert num > 0 : "Number must be greater than 0";
43. 使用 static 导入: import static java.lang.Math.;
44. 使用 diamond 运算符: List list = new ArrayList<>();
45. 使用 lambda 作为参数: Runnable runnable = () -> { ... }; thread.start(runnable);
46. 使用方法引用: Runnable runnable = MyClass::myMethod;
47. 使用 Optional 类: Optional name = user.getName();
48. 使用 Stream API: list.stream().filter(s -> s.startsWith("A")).forEach(System.out::println);
49. 使用并行流: list.stream().parallel().forEach(System.out::println);
50. 使用 Collectors 类: List names = list.stream().map(String::toUpperCase).collect(Collectors.toList());
51. 使用 Logger 类: Logger logger = Logger.getLogger(MyClass.class.getName());
52. 使用反射: Class clazz = Class.forName("MyClass");
53. 使用注解: @Override public void myMethod() { ... }
54. 使用枚举: enum MyEnum { A, B, C }
55. 使用资源管理: try (Scanner scanner = new Scanner(System.in)) { ... }
56. 使用本地变量类型推断: var name = "John Doe";
57. 使用 switch 增强: switch (num) { case 1, 2, 3 -> { ... } }
58. 使用模式匹配增强: if (obj instanceof String str && str.startsWith("A")) { ... }
59. 使用记录类增强: record Person(String name, int age) { ... } public static void main(String[] args) { Person person = new Person("John Doe", 30); }
60. 使用文本块增强: String str = """ Hello
World! """;
61. 使用 sealed 类: sealed class Shape permits Circle, Rectangle, Square {}
62. 使用非密封类: non-sealed class Triangle extends Shape { ... }
63. 使用 switch 表达式增强: int result = switch (num) { case 1 -> 10; case 2, 3 -> 20; default -> 0 };
64. 使用 lambda 作为方法参数: Runnable runnable = () -> { ... }; thread.start(runnable);
65. 使用方法引用增强: Runnable runnable = MyClass::myMethod;
66. 使用模式匹配 lambda: if (obj instanceof String str) { System.out.println(str); }
67. 使用 labeled break: outerLoop: for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (condition) { break outerLoop; } } }
68. 使用 instanceof 模式匹配: if (obj instanceof String str) { ... }
69. 使用 lambda 模式匹配: if (obj instanceof String str) { System.out.println(str); }
70. 使用 switch 表达式增强: int result = switch (num) { case 1 -> 10; case 2 -> 20; case 3 -> 30; default -> 0 };
71. 使用模式匹配 switch: switch (obj) { case String str -> {...} case Integer num -> {...} }
72. 使用 instanceof 运算符: if (obj instanceof String) { ... }
73. 使用 lambda 表达式: Runnable r = () -> { ... }
74. 使用方法引用: Runnable r = MyClass::myMethod
75. 使用文本块: String s = """ ... """
76. 使用 switch 表达式: int result = switch (num) { case 1 -> 10; case 2 -> 20; default -> 0 };
77. 使用记录类: record Person(String name, int age) { ... }
78. 使用 switch 表达式增强: int result = switch (num) { case 1, 2 -> 10; case 3 -> 20; default -> 0 };
79. 使用模式匹配