安装完JDK后,需要设置一个JAVA_HOME
的环境变量,它指向JDK的安装目录。在Windows下,它是安装目录,类似:
C:\Program Files\Java\jdk-15
把JAVA_HOME
的bin
目录附加到系统环境变量PATH
上。在Windows下:
Path=%JAVA_HOME%\bin;<现有的其他路径>
把JAVA_HOME
的bin
目录添加到PATH
中是为了在任意文件夹下都可以运行java
。
javac
是编译器,而可执行文件java
就是虚拟机,给虚拟机传递的参数是我们定义的类名,虚拟机自动查找对应的class文件并执行。final
修饰符,这个变量就变成了常量:
final double PI = 3.14; // PI是一个常量
常量在定义时进行初始化后就不可再次赋值,再次赋值会导致编译错误。根据习惯,常量名通常全部大写。
var sb = new StringBuilder();
编译器会根据赋值语句自动推断出变量sb
的类型是StringBuilder
。对编译器来说,该语句实际上会自动变成:
StringBuilder sb = new StringBuilder();
System.out.println(7/2); //结果为2
0111 1111 1111 1111 1111 1111 1111 1000 + 0000 0000 0000 0000 0000 0000 0000 1111 ----------------------------------------- 1000 0000 0000 0000 0000 0000 0000 0111
short
和int
计算,结果总是int
,原因是short
首先自动被转型为int。
float f = 4.5f + 12 / 5; System.out.println(f); //结果为6.5
整数运算在除数为0
时会报错,而浮点数运算在除数为0
时,不会报错,但会返回几个特殊值:
double d1 = 0.0 / 0; // NaN double d2 = 1.0 / 0; // Infinity double d3 = -1.0 / 0; // -Infinity
NaN
表示Not a NumberInfinity
表示无穷大-Infinity
表示负无穷大char
保存一个Unicode字符。char
类型表示,它们都占用两个字节。要显示一个字符的Unicode编码,只需将char
类型直接赋值给int
类型即可。还可以直接用转义字符\u
+Unicode编码来表示一个字符。
// 注意是四位十六进制: char c3 = ‘\u0041‘; // ‘A‘,因为十六进制0041 = 十进制65 char c4 = ‘\u4e2d‘; // ‘中‘,因为十六进制4e2d = 十进制20013
"""..."""
表示多行字符串(Text Blocks)。
public class Main { public static void main(String[] args) { String s = """ SELECT * FROM users WHERE id > 100 ORDER BY name DESC """; System.out.println(s); } } /*
输出:
SELECT * FROM
users
WHERE id > 100
ORDER BY name DESC
*/
public class JavaTest { public static void main(String[] args) { int a = 72; int b = 105; int c = 65281; // FIXME: String s = "" + (char)a + (char)b + (char)c; System.out.println(s); } }
System.out.printf()
,通过使用占位符%?
,printf()
可以把后面的参数格式化成指定格式。由于%表示占位符,因此,连续两个%%表示一个%字符本身。==
判断不靠谱。
public class Main { public static void main(String[] args) { double x = 1 - 9.0 / 10; System.out.println(9.0 / 10); if (x == 0.1) { System.out.println("x is 0.1"); } else { System.out.println("x is NOT 0.1"); } } } 0.9 x is NOT 0.1
正确的方法是利用差值小于某个临界值来判断:
public class Main { public static void main(String[] args) { double x = 1 - 9.0 / 10; if (Math.abs(x - 0.1) < 0.00001) { System.out.println("x is 0.1"); } else { System.out.println("x is NOT 0.1"); } } }
switch
时,如果遗漏了break
,就会造成严重的逻辑错误,而且不易在源代码中发现错误。从Java 12开始,switch
语句升级为更简洁的表达式语法,使用类似模式匹配(Pattern Matching)的方法,保证只有一种路径会被执行,并且不需要break
语句.
case "apple" -> System.out.println("Selected apple");
注意新语法使用->
,如果有多条语句,需要用{}
括起来。不要写break
语句,因为新语法只会执行匹配的语句,没有穿透效应。使用新的switch
语法,不但不需要break
,还可以直接返回值。把上面的代码改写如下:
public class Main { public static void main(String[] args) { String fruit = "apple"; int opt = switch (fruit) { case "apple" -> 1; case "pear", "mango" -> 2; default -> 0; }; // 注意赋值语句要以;结束 System.out.println("opt = " + opt); } }
for each
循环打印也很麻烦。幸好Java标准库提供了Arrays.toString()
,可以快速打印数组内容。
import java.util.Arrays; public class Main { public static void main(String[] args) { int[] ns = { 1, 1, 2, 3, 5, 8 }; System.out.println(Arrays.toString(ns)); } }
原文:https://www.cnblogs.com/fjnuczq/p/14087500.html