public class Hello {
public static void main(String[] args) { // 方法名是main
// 方法代码...
} // 方法定义结束
}
class
类名关键字public
访问修饰符, 表示公开; 修饰类, 也修饰方法void
, 表示没有任何返回值static
, 修饰符, 表示静态方法‘
号, 仅有一个字符final
进行修饰, 不可修改{}
作为自身范围int i = 12345;
short s = (short) i; // 12345
short
类型. (强制转换的结果可能是错的)char
是基本数据类型, 占用两个字符. 单引号表示String
是引用类型, 双引号表示\
进行转义.+
链接任何字符串和其他类型""...""
表示多行字符串null
类型[]
, 初始化new int[5]
必须确定需要包含的数量public static void main(String[] args) {
int[] ns = new int[] {1, 2, 3, 4, 5, 6};
System.out.println(ns[5]);
}
int[] ns = new int[] {1, 2, 3, 4, 5, 6};
ns = new int[] {1, 2, 4};
printf()
, 通过使用占位符%?
, 可以格式化后面的参数为指定格式%%
表示%
本身%
占位符, 必须传入两个参数System.in
代表标准输入流scanner
来方便输入==
并不靠谱==
用来判断是否指向同一个对象String s1 = "hello";
String s2 = "HELLO".toLowerCase();
System.out.println(s1.equals(s2));
equals()
判断值是否相等&&
break
会导致意想不到的结果default
, 不要忘记break
public static void main(String[] args) {
String fruit = "mango";
switch (fruit) {
case "apple" -> System.out.print("apple");
case "pear" -> System.out.print("pear");
case "mango" -> {
System.out.print("aaa");
System.out.print("bbbb");
}
default -> System.out.print("No fruit");
}
}
->
运算符public static void main(String[] args) {
String fruit = "apple";
int opt = switch (fruit) {
case "apple" -> 1;
case "pear" -> 2;
default -> {
int code = fruit.hashCode();
yield code;
}
};
System.out.print(opt);
}
do while
会少运算一次 int[] ns = {2, 199, 32, 4123};
for (int n : ns) {
System.out.println(n);
}
for each
可以遍历可迭代的数据类型. 例如List
, Map
等break
跳出循环break
只跳出自己的这层循环continue
提前结束本次循环, 继续执行下次循环Arrays.toString(ns)
可以打印出数组Arrays.sort(ns)
可以进行排序deepToString()
进行打印原文:https://www.cnblogs.com/zhangrunhao/p/12257139.html