Source -- Compile -- Bytecode -- Virtual Machine
一个Hello World的例子:
public class MyFirstApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
保存为MyFirstApp.java, 类名要和文件名保持一致。
编译:javac MyFirstApp.java
生成MyFirstApp.class bytecode文件
运行: java MyFirstApp
java 的条件判断(conditional test)必须是boolean类型,即true或者false。1和0不能作为boolean的条件判断。
数组声明与定义:
String[] pets = {"Fido", "Zeus", "Bin"};
int[] array = new int[2]; 数组初始化为0. 这点和单个变量声明不一样!
boolean[] array = new boolean[2]; 初始化为false
String[] array = new String[3]; 初始化为null
E | e |
PI | pi |
abs | 绝对值 |
ceil | 最小不小于 |
floor | 最大不大于 |
round | 四舍五入 |
random | 0.0~小于1.0 |
sqrt | 平方根 |
cbrt | 立方根 |
hypot(x,y) | sqrt(x^2+y^2) 过程不溢出 |
pow(a,b) | a^b |
exp(a) | e^a |
expm1(a) | e^a-1 |
scalb(d,s) | dx2^s |
log | ln |
log10 | log10 |
log1p(x) | ln(x+1) |
addExact | 加法,溢出异常 |
subtractExact | 减法,溢出异常 |
multiplyExact | 乘法,溢出异常 |
negateExact | 相反数,溢出异常 |
incrementExact | 加1,溢出异常 |
decrementExact | 减1,溢出异常 |
生成range Random:
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
nextInt(bound)生成的是0 到 bound, 且 不含bound. bound要大于等于0。
对三个数进行排序:
public static String getKey(int a, int b, int c) {
int max;
int mid;
int min;
max = Math.max(Math.max(a, b), Math.max(a, c));
min = Math.min(Math.min(a, b), Math.min(a, c));
mid = a + b + c - max - min;
return "" + min + mid + max;
}
以2为底的对数:
public static double log2(int num) {
return Math.log(num)/Math.log(2);
}
A class is a blueprint for an object.
An object has instance variables (state of object) and methods. An object is an instance.
Each object can have its own values for the instance variables.
Object lives in Garbage-Collectible Heap. If there is no reference to the object, this object is eligible for garbage collection.
Variable is a container, it holds something. A primitive variable or a reference variable.`
Primitive represents the actual value.
boolean | true/false | |
char | 16 bits | 0 to 65535 |
byte | 8 bits | -128 to 127 (2^7) |
short | 16 bits | -32768 to 32767 (2^15) |
int | 32 bits | -2 billions to 2 billions |
long | 64 bits | |
float | 32 bits | |
double | 64 bits |
2^3 | 8 | 10 | 10^1 | Ten |
2^10 | 1,024 | 1,000 | 10^3 | Thousand |
2^20 | 1,048,576 | 1,000,000 | 10^6 | Million |
2^30 | 1,073,741,824 | 1,000,000,000 | 10^9 | Billion |
Variable Name:
It must start with a letter, underscore or $. You can't start a name with a number.
Object reference. All references for a given JVM will be the same size.
There is actually no object variable. There's only an object reference variable. An object reference variable holds bits that represent a way to access an object.
1. Declare a reference variable. Tells the JVM to allocate memory space for a reference variable of type Dog.
Dog myDog;
2. Create an object and link the object with the reference. Tells the JVM to allocate memory space for a new Dog object on the heap. Assign the new Dog to the reference variable.
myDog = new Dog();
An array is an object, new an array.
A method uses parameters. A caller passes arguments.
Java is pass-by-value, which means pass-by-copy.
Variable types can be Implicitly promoted or explicit cast. Size matters for primitive. You can't put a larger type into a small type.
Apple A = new Apple(); 是A refer到了一个Apple上。
Apple B = A; 是B refer到了A refer的Apple上。此时可以通过dot来修改A refer的Object。
B = new Apple(); 是B refer 到了一个新的Apple上。此时对A没有影响,即A不会指向B的Object。B与A就没关系了。
将一个Variable和一个Object链接,Variable就可以访问这个Object,作为这个Object的代表。
Getters and Setters 是用来保护instance variables的。在Setters的函数里可以判断是否是有效的输入值(valid)。如果不是,则可以拒绝修改。
Instance variables always get a default value.
Integers | 0 |
Floating points | 0.0 |
Booleans | false |
Reference | null |
Local variables are declared within a method without default values, they must be initialized before use. 但是数组会自动初始化。
Compare two primitives if they are the same, use ==.
Compare two reference variables if they refer to a single object on the heap, use ==.
Compare two objects if they are equal, use .equals().
== is used to compare the bit pattern in two variables. Extra 0 doesn't matter. 数组的.equals()和==等价。