Java tips
Annotation
Annotation 提供了关于代码的元数据 (metadata), 但本身不会参与代码执行
- https://docs.oracle.com/javase/tutorial/java/annotations/index.html
- https://chrysanthium.com/spring-annotation-composition
Declare. @Retention
-> When, @Target
-> Where
@Retention(RetentionPolicy.RUNTIME)
@Target({Type})
@interface Foo {
boolean value() default true;
}
Use
@Foo(false)
class Bar {
}
Get annotation
Bar.class.getAnnotation(Foo.class).value();
Type Signatures
Java log 会用一些简化的符号来表示方法的参数和返回值类型. 比如 大写字母 I, J, F, D, V, ...等表示基础类型, L<class>;
表示类,
The JNI uses the Java VM’s representation of type signatures.
Java VM Type Signatures
Type Signature | Java Type |
---|---|
V | void |
Z | boolean |
B | byte |
C | char |
S | short |
I | int |
J | long |
F | float |
D | double |
L fully-qualified-class; | fully-qualified-class |
[ type | type[] |
( arg-types ) ret-type | method type |
For example, the Java method:
long f (int n, String s, int[] arr);
has the following type signature:
(ILjava/lang/String;[I)J
system properties & environment variables
-
system properties
-
Java command line
-Dname=value
-
System.setProperty(String key, String value)
-
System.getProperty(String key)
-
-
envrionment variables
System.getenv(String name)
Code conventions
Debug
-
Idea > Run / Debug
-
mvnDebug, Idea > Remote JVM Debug
Decompile .class file
-
Visit procyon: https://github.com/mstrobel/procyon
-
Download
procyon-decompiler-0.6.0.jar
from Release tab -
Run
java -jar procyon-decompiler-0.6.0.jar ~/Demo.class
Initialize a List
List<String> names = new ArrayList<>();
-
ArrayList 读取较快 a dynamically re-sizing array
-
LinkedList 修改较快 a doubly-linked list
UnsupportedOperation
Cause: Arrays.asList
returns a fixed-size list
List<Integer> list1 = Arrays.asList(1, 2, 3);
list1.add(4); // Error
Solution:
List<Integer> list1 = new ArrayList<>(Arrays.asList(1, 2, 3));
list1.add(4); // Pass
多个文件编译运行
可以只编译运行入口文件(包含main
的文件)。其它类,只要类名与文件名相同,会自动编译和引入这些类。
举例,有三个文件Deck.java
, Suit.java
, Rank.java
。Deck.java
中使用了类Suit
, Rank
javac Deck.java
java Deck
Run jar
java -jar example.jar
Stream Aggregate Operations
stream
, filter
, map
, forEach
, ...
persons
.stream()
.filter(p -> p.getGender() == Person.Sex.MALE)
.map(p -> p.getEmailAddress())
.forEach(email -> System.out.println(email));
Primitive data types and default values
Field variables
Data Type | Default Value (for fields) |
---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | '\u0000' |
String (or any object) | null |
boolean | false |
Local variables
Local variables are uninitialized if you cannot initialize them.
Operator Precedence
Operators | Precedence |
---|---|
postfix | expr++ expr-- |
unary | ++expr --expr +expr -expr ~ ! |
multiplicative | * / % |
additive | + - |
shift | << >> >>> |
relational | < > <= >= instanceof |
equality | == != |
bitwise AND | & |
bitwise exclusive OR | ^ |
bitwise inclusive OR | | |
logical AND | && |
logical OR | | |
ternary | ? : |
assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
Dependency Injection pattern
class Foo {
private Database database;
Foo() {
this.database = new Database("/path/to/my/data");
}
}
// Use dependency injection
class Foo {
private Database database;
Foo(Database database) {
this.database = database
}
}
可以使用guice,轻量级的 dependency injection framework.
Install java on ubuntu
JRE
<https://ubuntu.com/tutorials/install-jre#2-installing-openjdk-jre >
To install the OpenJDK JRE, we run
sudo apt update
sudo apt install default-jre
java --version
JDK
sudo apt install openjdk-11-demo openjdk-11-doc openjdk-11-jdk openjdk-11-source
Set JAVA_HOME
列出安装的 java 环境
sudo update-alternatives --config java
选择合适的,设置环境变量 JAVA_HOME
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Glossary
JRE
Java software for your computer, or the Java Runtime Environment, is also referred to as the Java Runtime, Runtime Environment, Runtime, JRE, Java Virtual Machine, Virtual Machine, Java VM, JVM, VM, Java plug-in, Java plugin, Java add-on or Java download.
JDK
Java Development Kit, contains JRE.