我知道Immutable对象比可变对象提供了几个优点,比起它们比可变对象更易于推理,它们没有随时间变化的复杂状态空间,我们可以自由地传递它们,它们可以制作安全的哈希表键等.所以我的问题是不可变对象的缺点是什么?
我for comprehension喜欢这样的:
private[helper] def myMethod(name: String, index: Long) = {
for {
result1 <- httpUtilities.getLowIndexes(index)
result2 <- myFacade.queryMaterial(result1)
result3 <- httpUtilities.someMethod(result2)
} yield result3
}
Run Code Online (Sandbox Code Playgroud)
我想要的是<-在列中对齐特殊字符,以便上面的代码转换为:
private[helper] def myMethod(name: String, index: Long) = {
for {
result1 <- httpUtilities.getLowIndexes(index)
result2 <- myFacade.queryMaterial(result1)
result3 <- httpUtilities.someMethod(result2)
} yield result3
}
Run Code Online (Sandbox Code Playgroud)
我可以在设置 - >代码样式 - > Scala =>包装和大括号 - >'匹配'和'大小写'语句 - >在Intellij中对齐列'case'分支但是如何对其进行匹配案例语句<-里面的人物for-comprehension.
import static java.lang.Integer.*;
import static java.lang.Long.*;
public class StaticImortError {
public static void main(String args []) {
System.out.println(MAX_VALUE);
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释为什么这个程序显示编译时错误,如果我试图使用导入
import static java.lang.Integer.*;
import static java.lang.Long.MAX_VALUE;,它运行正常,并按预期显示长数据类型的最大值,但与上述导入其显示错误.
为什么我在这里得到NullPointerException
package Threads;
public class Test implements Runnable {
FF d;
public static void main(String[] args) {
new Test().go();
}
void go() {
System.out.println(Thread.currentThread());
d = new FF();
new Thread(new Test()).start();
new Thread(new Test()).start();
}
public void run() {
System.out.println(d);
System.out.println(Thread.currentThread());
d.chat(Thread.currentThread().getId());//RTE
}
}
class FF {
static long flag = 0;
void chat(long id) {
if(flag == 0) {
flag = id;
}
for(int x=1;x<3;x++) {
if(flag == id) {
System.out.println("Yo");
}else {
System.out.println("dude");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
//可以解释为什么我在这里得到NullPointerException.虽然 …
class A {
public static void main(String args []){
int val = (int) ((Math.random) * 5);
String res = new String [ ]{"Rahul","Javed","Kunal","Ram"}[val];
System.out.println(res);
}
}
Run Code Online (Sandbox Code Playgroud)
当val的值为1时,打印了"Javed" - 但是在"String res ="表达式的末尾如何以及什么是[val].