在阅读了c#的隐藏特性之后,我想知道Java的隐藏特性有哪些?
当前回答
它并不是完全隐藏的,但反射是非常有用和强大的。使用简单的class . forname("…"). newinstance()是很好的,其中类类型是可配置的。编写这种工厂实现很容易。
其他回答
The power you can have over the garbage collector and how it manages object collection is very powerful, especially for long-running and time-sensitive applications. It starts with weak, soft, and phantom references in the java.lang.ref package. Take a look at those, especially for building caches (there is a java.util.WeakHashMap already). Now dig a little deeper into the ReferenceQueue and you'll start having even more control. Finally grab the docs on the garbage collector itself and you'll be able to control how often it runs, sizes of different collection areas, and the types of algorithms used (for Java 5 see http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html).
如果你做了很多JavaBean开发,并且使用了属性更改支持,你通常会写很多这样的setter:
public void setFoo(Foo aFoo){
Foo old = this.foo;
this.foo = aFoo;
changeSupport.firePropertyChange("foo", old, aFoo);
}
我最近无意中发现了一篇博客,它提出了一个更简洁的实现,使代码更容易编写:
public void setFoo(Foo aFoo){
changeSupport.firePropertyChange("foo", this.foo, this.foo = aFoo);
}
它实际上简化了事情,以至于我能够在Eclipse中调整setter模板,从而自动创建方法。
一些控制流的技巧,最后围绕一个return语句:
int getCount() {
try { return 1; }
finally { System.out.println("Bye!"); }
}
确定赋值规则将检查最终变量总是通过简单的控制流分析进行赋值:
final int foo;
if(...)
foo = 1;
else
throw new Exception();
foo+1;
Javadoc——在正确编写的情况下(不幸的是,一些开发人员并不总是这样),它会为您提供一个清晰、连贯的描述,说明代码应该做什么,而不是它实际做什么。然后可以将其转换为一组不错的可浏览的HTML文档。如果你使用持续集成等方法,它可以定期生成,这样所有开发者都可以看到最新的更新。
您可以使用string .format()构建sprintf样式的字符串。
String w = "world";
String s = String.format("Hello %s %d", w, 3);
当然,您也可以使用特殊的说明符来修改输出。
更多信息请点击:http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax
推荐文章
- 在流中使用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