一个方法可以修改传递引用所对应的变量值,而不能修改传递值调用所对应的变量值。
public class Test {
public static void main(String[] args) {
int a = 10;
doSome(a);
System.out.println(a);
}
private static void doSome(int a) {
System.out.println(a);
a += 10;
System.out.println(a);
doSome2(a);
}
private static void doSome2(int a) {
a += 10;
System.out.println(a);
}
}
Java 父类强转子类
只有父类对象本身就是用子类new出来的时候, 才可以在将来被强制转换为子类对象.
为什么要为接口增加静态方法
public class Test {
public void ttt(Integer a){
Function<Integer,String> x = s->{
//com.cc.base.Test@1c20c684
System.out.println(this.toString());
return s.toString();
};
x.apply(a);
}
}
使用异常机制的技巧
1.只在异常情况下使用异常机制(捕获异常所花费的时间大大超过了前者)
2.不要过分地细化异常
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.put("name","css");
properties.put("pss","ccc");
File file = new File("D:/cc.txt");
OutputStream outputStream = new FileOutputStream(file);
properties.store(outputStream,"测试");
}
public static void main(String[] args) throws InterruptedException, ExecutionException, Exception {
ExecutorService executorService = Executors.newFixedThreadPool(4);
List<Callable<String>> list = new ArrayList<>();
for (int i = 0; i <100; i++) {
int j = i;
Callable<String> callable = ()->{
ThreadLocalRandom random = ThreadLocalRandom.current();
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
return "notok"+j;
}
return "ok"+j;
};
list.add(callable);
}
List<Future<String>> list2 = executorService.invokeAll(list,1,TimeUnit.SECONDS);
for (Future<String> future : list2) {
if(!future.isCancelled()){
System.out.println(future.get());
}
}
Callable<String> callable = ()->{
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
//e.printStackTrace();
return "notok";
}
return "ok";
};
ExecutorCompletionService<String> completionService = new ExecutorCompletionService<String>(executorService);
completionService.submit(callable);
System.out.println(completionService.take().get());
executorService.shutdownNow();
}