安装 Chocolatey Chocolatey 安装
$choco install jdk8
安装 HomeBrew HomeBrew 安装
$brew update
$brew cask install java
Ubuntu/Debain
$sudo apt-get update
$sudo apt-get install default-jdk
Fedora/Redhat
$su -c "yum install java-1.8.0-openjdk"
public class Hello {
// Java main 方法,作为程序的入口
public static void main(String[] args) {
// 输出: Hello, world!
System.out.println("Hello, world!");
}
}
使用命令行编译和运行
$ javac Hello.java
$ java Hello
Hello, world!
// 单行注释
/*
多行
注释
*/
/**
* 文档注释
*/
int num = 5;
float floatNum = 5.99f;
char letter = 'D';
boolean bool = true;
String site = "cheatsheets.zip";
String first = "John";
String last = "Doe";
String name = first + " " + last;
System.out.println(name);
See: Strings
String word = "CheatSheets";
for (char c: word.toCharArray()) {
System.out.print(c + "-");
}
// Outputs: C-h-e-a-t-S-h-e-e-t-s-
See: Loops
char[] chars = new char[10];
chars[0] = 'a'
chars[1] = 'b'
String[] letters = {"A", "B", "C"};
int[] mylist = {100, 200};
boolean[] answers = {true, false};
See: Arrays
// Widening conversion (宽化转型)
// byte<short<int<long<float<double
int i = 10;
long l = i; // 10
// Narrowing (窄化转型)
double d = 10.02;
long l = (long)d; // 10
// 截尾和四舍五入
double a = 20.5;
long i1 = (long)a; // 20
long i2 = Math.round(a); // 21
String.valueOf(10); // "10"
Integer.parseInt("10"); // 10
Double.parseDouble("10"); // 10.0
int j = 10;
if (j == 10) {
System.out.println("I get printed");
} else if (j > 10) {
System.out.println("I don't");
} else {
System.out.println("I also don't");
}
See: Conditionals
Scanner in = new Scanner(System.in);
String str = in.nextLine();
System.out.println(str);
int num = in.nextInt();
System.out.println(num);
// 换行打印
System.out.println("Hello");
// 不换行打印
System.out.print("World");
// 格式化打印
System.out.printf(" %d\n", 2023);
// Hello
// World 2023
public class App {
public static void main(String[] args) {
// 使用 new 关键字创建对象
TestCase testCase = new TestCase();
testCase.setValue(10);
testCase.printValue();
// 静态方法可以直接通过类名访问
TestCase.printClass();
}
}
class TestCase {
// 字段
private int value;
// 方法
public void setValue(int value) {
this.value = value;
}
public void printValue() {
System.out.println(value);
}
// 静态方法
public static void printClass() {
System.out.println(
TestCase.class.getSimpleName()
);
}
}
// 10
// TestCase
| 基本类型 | 大小 | 默认值 | 范围 | 包装类 |
|---|---|---|---|---|
byte |
1 byte | 0 | -128 ~ 127 | Byte |
short |
2 byte | 0 | -215 ~ 215-1 | Short |
int |
4 byte | 0 | -231 ~ 231-1 | Integer |
long |
8 byte | 0 | -263 ~ 263-1 | Long |
float |
4 byte | 0.0f | IEEE754 | Float |
double |
8 byte | 0.0d | IEEE754 | Double |
char |
2 byte | \u0000 | 0 ~ 65535 | Char |
boolean |
N/A | false | true / false | Boolean |
void |
N/A | N/A | N/A | Void |
int a = 1, b = 2;
System.out.println(a + b); // 3
System.out.println(a - b); // -1
System.out.println(a * b); // 2
System.out.println(a / b); // 0
System.out.println(a % b); // 1
int a = 1, b = 2;
System.out.println(a * (+b));
System.out.println(a * (-b));
// 2
// -2
int a = 1;
System.out.println((a++) + " " + a); // 1 2
System.out.println((++a) + " " + a); // 3 3
System.out.println((a--) + " " + a); // 3 2
System.out.println((--a) + " " + a); // 1 1
int a, b, c;
a = 1; b = 2;
c = a += b;
System.out.println(a + " " + c);
a = 1; b = 2;
c = a -= b;
System.out.println(a + " " + c);
a = 1; b = 2;
c = a *= b;
System.out.println(a + " " + c);
a = 1; b = 2;
c = a /= b;
System.out.println(a + " " + c);
a = 1; b = 2;
c = a %= b;
System.out.println(a + " " + c);
// 3 3
// -1 -1
// 2 2
// 0 0
// 1 1
int i1 = 0x2f; // 16 进制
int i2 = 0x2F; // 16 进制
int i3 = 0177; // 8 进制
int i4 = 0b010101; // 2 进制, jdk 7 新增
int i5 = 0B010101; // 2 进制, jdk 7 新增
int i6 = 10; // 10 进制,不加前后缀
long l1 = 100l; // Long 类型
long l2 = 100L; // Long 类型(推荐)
float f1 = 0.1f; // float 类型
float f2 = 0.1F; // float 类型
double d1 = 0.1; // double 类型(默认)
double d2 = 0.1d; // double 类型
double d3 = 0.1D; // double 类型
Long a = 100L;
System.out.println(Long.toBinaryString(a));
// 1100100
Long a = 1_000_000_000L;
double a = 1e-2;
double b = 1E2;
System.out.println(a); // 0.01
System.out.println(b); // 100.0
String str1 = "value";
String str2 = new String("value");
String str3 = String.valueOf(123);
String s = 3 + "str" + 3; // 3str3
String s = 3 + 3 + "str"; // 6str
String s = "3" + 3 + "str"; // 33str
String s = "3" + "3" + "23"; // 3323
String s = "" + 3 + 3 + "23"; // 3323
String s = 3 + 3 + 23; // Incompatible types
StringBuilder sb = new StringBuilder(10);
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| | | | | | | | | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
sb.append("QuickRef");
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| Q | u | i | c | k | R | e | f | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
sb.delete(5, 9);
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| Q | u | i | c | k | | | | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
sb.insert(0, "My ");
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| M | y | | Q | u | i | c | k | |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
sb.append("!");
┌───┬───┬───┬───┬───┬───┬───┬───┬───┐
| M | y | | Q | u | i | c | k | ! |
└───┴───┴───┴───┴───┴───┴───┴───┴───┘
0 1 2 3 4 5 6 7 8 9
String s1 = new String("cheatsheets.zip");
String s2 = new String("cheatsheets.zip");
s1 == s2 // false
s1.equals(s2) // true
"AB".equalsIgnoreCase("ab") // true
String str = "Abcd";
str.toUpperCase(); // ABCD
str.toLowerCase(); // abcd
str.concat("#"); // Abcd#
str.replace("b", "-"); // A-cd
" abc ".trim(); // abc
"ab".toCharArray(); // {'a', 'b'}
String str = "abcd";
str.charAt(2); // c
str.indexOf("a") // 0
str.indexOf("z") // -1
str.length(); // 4
str.toString(); // abcd
str.substring(2); // cd
str.substring(2,3); // c
str.contains("c"); // true
str.endsWith("d"); // true
str.startsWith("a"); // true
str.isEmpty(); // false
String str = "hello";
str.concat("world");
// Outputs: hello
System.out.println(str);
String str = "hello";
String concat = str.concat("world");
// Outputs: helloworld
System.out.println(concat);
Once created cannot be modified, any modification creates a new String
int[] a1;
int[] a2 = {1, 2, 3};
int[] a3 = new int[]{1, 2, 3};
int[] a4 = new int[3];
a4[0] = 1;
a4[2] = 2;
a4[3] = 3;
int[] a = {1, 2, 3};
System.out.println(a[0]); // 1
a[0] = 9;
System.out.println(a[0]); // 9
System.out.println(a.length); // 3
int[] arr = {1, 2, 3};
for (int i=0; i < arr.length; i++) {
arr[i] = arr[i] * 2;
System.out.print(arr[i] + " ");
}
// Outputs: 2 4 6
String[] arr = {"a", "b", "c"};
for (String a: arr) {
System.out.print(a + " ");
}
// Outputs: a b c
int[][] matrix = { {1, 2, 3}, {4, 5} };
int x = matrix[1][0]; // 4
// [[1, 2, 3], [4, 5]]
Arrays.deepToString(matrix);
int[][] a = matrix;
for (int i = 0; i < a.length; ++i) {
for(int j = 0; j < a[i].length; ++j) {
System.out.println(a[i][j]);
}
}
// Outputs: 1 2 3 4 5 6 7
char[] chars = {'b', 'a', 'c'};
Arrays.sort(chars);
// [a, b, c]
Arrays.toString(chars);
int k = 15;
if (k > 20) {
System.out.println(1);
} else if (k > 10) {
System.out.println(2);
} else {
System.out.println(3);
}
int month = 3;
String str;
switch (month) {
case 1:
str = "January";
break;
case 2:
str = "February";
break;
case 3:
str = "March";
break;
default:
str = "Some other month";
break;
}
// Outputs: Result March
System.out.println("Result " + str);
int a = 10;
int b = 20;
int max = (a > b) ? a : b;
// Outputs: 20
System.out.println(max);
for (int i = 0; i < 10; i++) {
System.out.print(i);
}
// Outputs: 0123456789
// 逗号表达式
for (int i = 0,j = 0; i < 3; i++,j--) {
System.out.print(j + "|" + i + " ");
}
// Outputs: 0|0 -1|1 -2|2
int[] numbers = {1,2,3,4,5};
for (int number: numbers) {
System.out.print(number);
}
// Outputs: 12345
Used to loop around array's or List's
int count = 0;
while (count < 5) {
System.out.print(count);
count++;
}
// Outputs: 01234
int count = 0;
do {
System.out.print(count);
count++;
} while (count < 5);
// Outputs: 01234
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue;
}
System.out.print(i);
}
// Outputs: 0124
int i = 0;
label1:
for (;;) {
for (; i < 10; i++) {
System.out.print(i);
if (i == 3) {
i += 2;
continue label1;
}
}
break;
}
// 012356789
for (int i = 0; i < 5; i++) {
System.out.print(i);
if (i == 3) {
break;
}
}
// Outputs: 0123
int i = 0;
label1:
for (;;) {
for (; i < 10; i++) {
System.out.print(i);
if (i == 3) {
i += 2;
break label1;
}
}
break;
}
// 0123
class TestCase {
/**
* 无参构造器
*/
TestCase() {
System.out.println("TestCase");
}
/**
* 带有参数的构造器
*/
TestCase(String str) {
System.out.println(str);
}
}
TestCase testCase = new TestCase();
TestCase testCase2 = new TestCase("Hello");
// TestCase
// Hello
class TestCase {
public void printValue(int value) {
System.out.println("method1: " + value);
}
public void printValue(String value) {
System.out.println("method2: " + value);
}
public void printValue(int value1, int value2) {
System.out.println("method3: ");
System.out.println(value1);
System.out.println(value2);
}
}
TestCase testCase = new TestCase();
testCase.printValue(1);
testCase.printValue("Hello");
testCase.printValue(1, 2);
// method1: 1
// method2: Hello
// method3:
// 1
// 2
// 注意:不能通过返回值来区分重载
class TestCase {
private int value = 10;
public void printValue(int value) {
System.out.println(value);
System.out.println(this.value);
}
}
TestCase testCase = new TestCase();
testCase.printValue(1);
// 1
// 10
class TestCase {
private SubTestCase subTestCase = new SubTestCase("定义时初始化1");
TestCase() {
SubTestCase subTestCase = new SubTestCase("构造函数中初始化");
}
private SubTestCase subTestCase2 = new SubTestCase("定义时初始化2");
static {
SubTestCase subTestCase = new SubTestCase("静态代码块初始化1");
}
private static SubTestCase subTestCase3 = new SubTestCase("静态变量初始化");
static {
SubTestCase subTestCase = new SubTestCase("静态代码块初始化2");
}
{
SubTestCase subTestCase3 = new SubTestCase("非静态实例初始化");
}
}
class SubTestCase {
SubTestCase(String str) {
System.out.println(str);
}
}
TestCase testCase = new TestCase();
// 静态代码块初始化1
// 静态变量初始化
// 静态代码块初始化2
// 定义时初始化1
// 定义时初始化2
// 非静态实例初始化
// 构造函数中初始化
public class App {
public static void main(String[] args) {
printArgs("arg1", "arg2");
}
private static void printArgs(String... args) {
for (String arg : args) {
System.out.println(arg);
}
}
}
// arg1
// arg2
// 定义包
// 定义包
package com.ans20xx;
// 导入包
import java.util.Date;
public class App {
public static void main(String[] args) {
System.out.println(new Date());
// 指定要导入的类位置
System.out.println(
new java.util.Date()
);
}
}
// Mon Jan 02 19:56:51 CST 2023
// Mon Jan 02 19:56:51 CST 2023
public class TestCase {
SubTestCase subTestCase =
new SubTestCase("包访问权限,默认为包访问,同一个包下的类都可以访问");
private SubTestCase subTestCase2 =
new SubTestCase("私有访问权限,只能被本类方法访问");
public SubTestCase subTestCase3 =
new SubTestCase("公开/接口访问权限,可以被所有类访问");
protected SubTestCase subTestCase04 =
new SubTestCase("继承访问权限,只能被该类以及该类的子类访问");
}
class TestCase {
String value;
TestCase(String value) {
this.value = value;
}
protected void printValue() {
System.out.println("value: " + value);
}
}
/**
* 使用 extends 关键字来表示继承关系
* 注意 Java 中一个类不支持同时继承多个类
*/
class SubTestCase extends TestCase {
SubTestCase(String value) {
// 使用 super 来调用父类构造器
super(value);
}
/**
* 使用 @Override 注解表示该方法重写了父类的某个方法
* 被该注解标注的方法会在编译器被检查命名和参数列表是否正确
*/
@Override
public void printValue() {
super.printValue();
System.out.println("SubTestCase value: " + value);
}
}
/**
* final 关键字用在类上表示该类不能被继承
*/
final class TestCase {
/**
* 空白 final
* 只能在构造器中被初始化一次
*/
final String value;
/**
* 修饰基本类型变量
* 表示该变量的值不能被改变
*/
final int num = 10;
/**
* final 修饰对象引用,表示该引用不能修改
*/
final String str = "finalStr";
TestCase(String value) {
this.value = value;
}
/**
* final 用来修饰方法参数,
* 表示该方法参数是只读的
*/
public void printFinalArg(
final String arg
) {
System.out.println(arg);
}
/**
* final 用在方法上表示该方法不能被重写
*/
public final void printValue() {
System.out.println(
"value: " + value
);
}
}
public class App {
public static void main(String[] args) {
TestCase testCase = new SubTestCase1();
testCase.printClass();
testCase = new SubTestCase2();
testCase.printClass();
}
}
class TestCase {
public void printClass() {
System.out.println(TestCase.class.getSimpleName());
}
}
class SubTestCase1 extends TestCase{
@Override
public void printClass() {
System.out.println(SubTestCase1.class.getSimpleName());
}
}
class SubTestCase2 extends TestCase{
@Override
public void printClass() {
System.out.println(SubTestCase2.class.getSimpleName());
}
}
// SubTestCase1
// SubTestCase2
| Collection | Interface | Ordered | Sorted | Thread safe | Duplicate | Nullable |
|---|---|---|---|---|---|---|
| ArrayList | List | Y | N | N | Y | Y |
| Vector | List | Y | N | Y | Y | Y |
| LinkedList | List, Deque | Y | N | N | Y | Y |
| CopyOnWriteArrayList | List | Y | N | Y | Y | Y |
| HashSet | Set | N | N | N | N | One null |
| LinkedHashSet | Set | Y | N | N | N | One null |
| TreeSet | Set | Y | Y | N | N | N |
| CopyOnWriteArraySet | Set | Y | N | Y | N | One null |
| ConcurrentSkipListSet | Set | Y | Y | Y | N | N |
| HashMap | Map | N | N | N | N (key) | One null (key) |
| HashTable | Map | N | N | Y | N (key) | N (key) |
| LinkedHashMap | Map | Y | N | N | N (key) | One null (key) |
| TreeMap | Map | Y | Y | N | N (key) | N (key) |
| ConcurrentHashMap | Map | N | N | Y | N (key) | N |
| ConcurrentSkipListMap | Map | Y | Y | Y | N (key) | N |
| ArrayDeque | Deque | Y | N | N | Y | N |
| PriorityQueue | Queue | Y | N | N | Y | N |
| ConcurrentLinkedQueue | Queue | Y | N | Y | Y | N |
| ConcurrentLinkedDeque | Deque | Y | N | Y | Y | N |
| ArrayBlockingQueue | Queue | Y | N | Y | Y | N |
| LinkedBlockingDeque | Deque | Y | N | Y | Y | N |
| PriorityBlockingQueue | Queue | Y | N | Y | Y | N |
List<Integer> nums = new ArrayList<>();
// Adding
nums.add(2);
nums.add(5);
nums.add(8);
// Retrieving
System.out.println(nums.get(0));
// Indexed for loop iteration
for (int i = 0; i < nums.size(); i++) {
System.out.println(nums.get(i));
}
nums.remove(nums.size() - 1);
nums.remove(0); // VERY slow
for (Integer value : nums) {
System.out.println(value);
}
Map<Integer, String> m = new HashMap<>();
m.put(5, "Five");
m.put(8, "Eight");
m.put(6, "Six");
m.put(4, "Four");
m.put(2, "Two");
// Retrieving
System.out.println(m.get(6));
// Lambda forEach
m.forEach((key, value) -> {
String msg = key + ": " + value;
System.out.println(msg);
});
Set<String> set = new HashSet<>();
if (set.isEmpty()) {
System.out.println("Empty!");
}
set.add("dog");
set.add("cat");
set.add("mouse");
set.add("snake");
set.add("bear");
if (set.contains("cat")) {
System.out.println("Contains cat");
}
set.remove("cat");
for (String element : set) {
System.out.println(element);
}
Deque<String> a = new ArrayDeque<>();
// Using add()
a.add("Dog");
// Using addFirst()
a.addFirst("Cat");
// Using addLast()
a.addLast("Horse");
// [Cat, Dog, Horse]
System.out.println(a);
// Access element
System.out.println(a.peek());
// Remove element
System.out.println(a.pop());
| Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
| public | Y | Y | Y | Y |
| protected | Y | Y | Y | N |
| no modifier | Y | Y | N | N |
| private | Y | N | N | N |
String text = "I am learning Java";
// Removing All Whitespace
text.replaceAll("\\s+", "");
// Splitting a String
text.split("\\|");
text.split(Pattern.quote("|"));
See: Regex in java
abstractcontinuefornewswitchassertdefaultgotopackagesynchronizedbooleandoifprivatethisbreakdoubleimplementsprotectedthrowbyteelseimportpublicthrowscaseenuminstanceofreturntransientcatchextendsintshorttrycharfinalinterfacestaticvoidclassfinallylongstrictfpvolatileconstfloatnativesuperwhile| Method | Description |
|---|---|
Math.max(a,b) |
Maximum of a and b |
Math.min(a,b) |
Minimum of a and b |
Math.abs(a) |
Absolute value a |
Math.sqrt(a) |
Square-root of a |
Math.pow(a,b) |
Power of b |
Math.round(a) |
Closest integer |
Math.sin(ang) |
Sine of ang |
Math.cos(ang) |
Cosine of ang |
Math.tan(ang) |
Tangent of ang |
Math.asin(ang) |
Inverse sine of ang |
Math.log(a) |
Natural logarithm of a |
Math.toDegrees(rad) |
Angle rad in degrees |
Math.toRadians(deg) |
Angle deg in radians |
try {
// something
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("always printed");
}