public class ValExample { public static void main(String[] args){ val array = new ArrayList<String>(); array.add("val.."); for(val obj: array){ System.out.println(obj); } } }
var
var和val类似,区别在于不再有final进行修饰.
@NonNull
该注解可以用于参数的空指针检查,避免出现讨厌的空指针问题。示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import lombok.NonNull;
public class NonNullExample {
private Person person;
public NonNullExample(@NonNull Person person){ this.person = person; }
public class CleanupExample { public static void main(String[] args) throws IOException { @Cleanup InputStream in = new FileInputStream(args[0]); @Cleanup OutputStream out = new FileOutputStream(args[1]); byte[] b = new byte[10000]; while (true) { int r = in.read(b); if (r == -1) break; out.write(b, 0, r); } } }
public class CleanupExample { public static void main(String[] args) throws IOException { InputStream in = new FileInputStream(args[0]); try { OutputStream out = new FileOutputStream(args[1]); try { byte[] b = new byte[10000]; while (true) { int r = in.read(b); if (r == -1) break; out.write(b, 0, r); } } finally { if (out != null) { out.close(); } } } finally { if (in != null) { in.close(); } } } }
public class SynchronizedExample { private static final Object $LOCK = new Object[0]; private final Object $lock = new Object[0]; private final Object readLock = new Object();
public static void hello() { synchronized($LOCK) { System.out.println("world"); } }
public int answerToLife() { synchronized($lock) { return 42; } }
public void foo() { synchronized(readLock) { System.out.println("bar"); } } }