我在项目中有3个开发人员在写入日志时有不同的样式.哪种变体最好?
LOG.info("error = {}", errmsg);
LOG.info("error = ", errmsg);
LOG.info("error = " + errmsg);
Run Code Online (Sandbox Code Playgroud) 有人能解释一下这里发生了什么吗?
假设Car并且Bike是子类Vehicle.
它看起来像Vehicle v参考被投射到一个Bike.我知道这是非法的,确实是编译器吐出来的... Car cannot be cast to Bike.
但这不应该是Vehicle不能投的Bike吗?毕竟,Vehicle v是一个Vehicle参考.
public class Test {
public static void main(String[] args) {
Vehicle v = new Car();
Bike b = (Bike) v;
// some stuff
}
}
Run Code Online (Sandbox Code Playgroud) 谁能告诉我这里发生了什么?它看起来像myObj被强制转换为String []所以它可以在for循环中迭代.但它被构造为新的String [] - 为什么需要进行转换?
public static void main(String args[]) {
Object myObj = new String[]{"one", "two", "three"};
for (String s : (String[])myObj) {
System.out.print(s + ".");
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
实践测试中出现了这个问题:创建一个新的字符串数组,将其初始化为null,然后初始化第一个元素并打印它.为什么这会导致空指针异常?为什么不打印"一个"?它与字符串不变性有关吗?
public static void main(String args[]) {
try {
String arr[] = new String[10];
arr = null;
arr[0] = "one";
System.out.print(arr[0]);
} catch(NullPointerException nex) {
System.out.print("null pointer exception");
} catch(Exception ex) {
System.out.print("exception");
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
这四种方法签名有什么区别,为什么第四种方法不起作用?
public void main(String args[]) {... }
public void main(String[] args) {... }
public void main(String... args) {... }
public void main(String[] args[]) {... }
Run Code Online (Sandbox Code Playgroud) 我遇到过某人的旧代码,其中包含的变量如下:@ user_id @,@ reference_id @等等.维基百科说:"在某些JavaScript实现中,at符号(@)可用于标识符......"在JS实现中,这有用吗?我无法让它发挥作用.