在阅读了c#的隐藏特性之后,我想知道Java的隐藏特性有哪些?
当前回答
这是我的清单。
我最喜欢的(也是最可怕的)隐藏特性是,您可以从没有声明抛出任何东西的方法中抛出检查异常。
import java.rmi.RemoteException;
class Thrower {
public static void spit(final Throwable exception) {
class EvilThrower<T extends Throwable> {
@SuppressWarnings("unchecked")
private void sneakyThrow(Throwable exception) throws T {
throw (T) exception;
}
}
new EvilThrower<RuntimeException>().sneakyThrow(exception);
}
}
public class ThrowerSample {
public static void main( String[] args ) {
Thrower.spit(new RemoteException("go unchecked!"));
}
}
此外,你可能想知道你可以抛出'null'…
public static void main(String[] args) {
throw null;
}
猜猜这打印了什么:
Long value = new Long(0);
System.out.println(value.equals(0));
猜猜这返回什么:
public int returnSomething() {
try {
throw new RuntimeException("foo!");
} finally {
return 0;
}
}
优秀的开发人员不应该对上述情况感到惊讶。
在Java中,你可以用以下几种有效的方式来声明数组:
String[] strings = new String[] { "foo", "bar" };
// the above is equivalent to the following:
String[] strings = { "foo", "bar" };
所以下面的Java代码是完全有效的:
public class Foo {
public void doSomething(String[] arg) {}
public void example() {
String[] strings = { "foo", "bar" };
doSomething(strings);
}
}
是否有任何有效的理由说明下面的代码不应该是有效的?
public class Foo {
public void doSomething(String[] arg) {}
public void example() {
doSomething({ "foo", "bar" });
}
}
我认为,上述语法可以有效地替代Java 5中引入的可变参数。并且,与之前允许的数组声明更加一致。
其他回答
从Java 1.5开始,Java现在有了更清晰的语法来编写变量函数。不只是传递一个数组,现在您可以执行以下操作
public void foo(String... bars) {
for (String bar: bars)
System.out.println(bar);
}
Bars自动转换为指定类型的数组。这不是一个巨大的胜利,但仍然是一场胜利。
一个优化技巧,使您的代码更容易维护,更不容易受到并发性错误的影响。
public class Slow {
/** Loop counter; initialized to 0. */
private long i;
public static void main( String args[] ) {
Slow slow = new Slow();
slow.run();
}
private void run() {
while( i++ < 10000000000L )
;
}
}
$ time java慢 真正的0 m15.397s $ time java慢 真正的0 m20.012s $ time java慢 真正的0 m18.645s
平均:18.018秒
public class Fast {
/** Loop counter; initialized to 0. */
private long i;
public static void main( String args[] ) {
Fast fast = new Fast();
fast.run();
}
private void run() {
long i = getI();
while( i++ < 10000000000L )
;
setI( i );
}
private long setI( long i ) {
this.i = i;
}
private long getI() {
return this.i;
}
}
$ time java快速 真正的0 m12.003s $ time java快速 真正的0 m9.840s $ time java快速 真正的0 m9.686s
平均:10.509秒
引用类作用域变量比引用方法作用域变量需要更多的字节码。在关键循环之前添加方法调用几乎不会增加开销(而且编译器可能会内联调用)。
这种技术(总是使用访问器)的另一个优点是它消除了Slow类中的潜在错误。如果第二个线程要不断地将i的值重置为0(通过调用slow. xml)。setI(0),例如),Slow类永远不能结束它的循环。调用访问器并使用局部变量消除了这种可能性。
在Linux 2.6.27-14上使用J2SE 1.6.0_13进行测试。
几个月前当我第一次发现它时,双括号初始化让我感到惊讶,以前从未听说过它。
作为存储每个线程状态的一种方式,ThreadLocals通常并不广为人知。
由于JDK 1.5 Java已经有了非常好的实现和健壮的并发工具,而不仅仅是锁,它们存在于Java .util.concurrent中,一个特别有趣的例子是Java .util.concurrent.atomic子包,它包含线程安全的原语,实现比较和交换操作,并可以映射到这些操作的实际本机硬件支持版本。
前几天我对实例初始化器感到惊讶。我删除了一些代码折叠方法,最终创建了多个实例初始化器:
public class App {
public App(String name) { System.out.println(name + "'s constructor called"); }
static { System.out.println("static initializer called"); }
{ System.out.println("instance initializer called"); }
static { System.out.println("static initializer2 called"); }
{ System.out.println("instance initializer2 called"); }
public static void main( String[] args ) {
new App("one");
new App("two");
}
}
执行main方法将显示:
static initializer called
static initializer2 called
instance initializer called
instance initializer2 called
one's constructor called
instance initializer called
instance initializer2 called
two's constructor called
我想,如果您有多个构造函数并且需要通用代码,那么这些将很有用
它们还提供了初始化类的语法糖:
List<Integer> numbers = new ArrayList<Integer>(){{ add(1); add(2); }};
Map<String,String> codes = new HashMap<String,String>(){{
put("1","one");
put("2","two");
}};
我喜欢方法的静态导入。
例如,创建以下util类:
package package.name;
public class util {
private static void doStuff1(){
//the end
}
private static String doStuff2(){
return "the end";
}
}
然后像这样使用它。
import static package.name.util.*;
public class main{
public static void main(String[] args){
doStuff1(); // wee no more typing util.doStuff1()
System.out.print(doStuff2()); // or util.doStuff2()
}
}
静态导入适用于任何类,甚至数学…
import static java.lang.Math.*;
import static java.lang.System.out;
public class HelloWorld {
public static void main(String[] args) {
out.println("Hello World!");
out.println("Considering a circle with a diameter of 5 cm, it has:");
out.println("A circumference of " + (PI * 5) + "cm");
out.println("And an area of " + (PI * pow(5,2)) + "sq. cm");
}
}
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap