public static void main(String[] args) {
Integer a = 1;
Integer b = 2;
Integer c = null;
Integer d = a > b ? a + b : c;
System.out.println(d);
}
null
Exception in thread "main" java.lang.NullPointerException
at DesignPattern.single.dfghj.main(dfghj.java:16)
Process finished with exit code 1
原因
解决办法:保持“:”两边一致,
参考博客:三目运算符的空指针问题
public class ljtest{
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread name:" + Thread.currentThread().getName() + ",i:" + i);
}
});
}
}
}
thread name:Thread-0,i:0
thread name:Thread-1,i:1
thread name:Thread-2,i:2
thread name:Thread-4,i:4
thread name:Thread-6,i:6
thread name:Thread-8,i:8
thread name:Thread-9,i:9
thread name:Thread-3,i:3
thread name:Thread-5,i:5
thread name:Thread-7,i:7
编译期都过不了,会在输出语句哪一行的 i 哪里显示红线。
public class ljtest{
private static ExecutorService executorService = Executors.newSingleThreadExecutor();
static class t1 implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println("i am t1");
return "t1===";
}
}
static class t2 implements Callable<String> {
@Override
public String call() throws Exception {
System.out.println("i am t2");
Future<String> submit = executorService.submit(new t1());
return "t2===" + submit.get();
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
t2 t = new t2();
Future<String> submit = executorService.submit(t);
System.out.println(submit.get());
System.out.println("la~la~la~");
executorService.shutdown();
}
}
i am t2
i am t1
t2===t1===
la~la~la~
i am t2
常规写法:
public static void main(String[] args) {
int a = 5;
int b = 6;
a = a + b;
b = a - b;
a = a - b;
System.out.println("a:" + a + "b:" + b);
}
异或写法
public static void main(String[] args) {
int a = 8;
int b = 8;
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("new--->" + "a:" + a + ",b:" + b);
}
常规写法
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
防计时攻击写法
public boolean equals(String s1, String s2) {
if (s1 == null || s2 == null) {
return false;
}
if (s1 == s2) {
return true;
}
if (s1.length() != s2.length()) {
return false;
}
int res = 0;
for (int i = 0; i < s1.length(); i++) {
res |= s1.charAt(i) ^ s2.charAt(i);
}
return res == 0;
}
public class ljtest {
public final int value = 4;//5.注释掉本行
public void dop(){
int value = 6;//4.注释掉本行
Runnable r = new Runnable() {
public final int value = 9;//3.注释掉本行
@Override
public void run() {
int value = 10;//2.注释掉本行
System.out.println(this.value);//1.去掉this
}
};
r.run();
}
public static void main(String[] args) {
ljtest f = new ljtest();
f.dop();
}
}
9
看代码的注释部分,考虑下列情形,输出分别是什么?
输出结果
结论: