在阅读了c#的隐藏特性之后,我想知道Java的隐藏特性有哪些?
当前回答
一个优化技巧,使您的代码更容易维护,更不容易受到并发性错误的影响。
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进行测试。
其他回答
使用此关键字从内部类访问包含类的字段/方法。在下面的例子中,我们希望使用匿名内部类的容器类的sortAscending字段。使用ContainerClass.this.sortAscending代替this。sortAscending就是这样。
import java.util.Comparator;
public class ContainerClass {
boolean sortAscending;
public Comparator createComparator(final boolean sortAscending){
Comparator comparator = new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
if (sortAscending || ContainerClass.this.sortAscending) {
return o1 - o2;
} else {
return o2 - o1;
}
}
};
return comparator;
}
}
如果你做了很多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模板,从而自动创建方法。
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).
当你不需要StringBuilder中包含的同步管理时,使用StringBuilder代替StringBuffer。它将提高应用程序的性能。
Java 7的改进甚至比任何隐藏的Java特性都要好:
菱形语法:Link
不要在实例化时使用那些infinite <>语法:
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
// Can now be replaced with this:
Map<String, List<String>> anagrams = new HashMap<>();
switch中的字符串:Link
在switch中使用String,而不是old-C int:
String s = "something";
switch(s) {
case "quux":
processQuux(s);
// fall-through
case "foo":
case "bar":
processFooOrBar(s);
break;
case "baz":
processBaz(s);
// fall-through
default:
processDefault(s);
break;
}
自动资源管理Link
这段旧代码:
static void copy(String src, String dest) throws IOException {
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dest);
try {
byte[] buf = new byte[8 * 1024];
int n;
while ((n = in.read(buf)) >= 0)
out.write(buf, 0, n);
} finally {
out.close();
}
} finally {
in.close();
}
}
现在可以用更简单的代码代替:
static void copy(String src, String dest) throws IOException {
try (InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest)) {
byte[] buf = new byte[8192];
int n;
while ((n = in.read(buf)) >= 0)
out.write(buf, 0, n);
}
}
您选择的编码中的属性文件如何?过去,当你加载你的属性,你提供了一个InputStream和load()方法解码为ISO-8859-1。你实际上可以用其他编码来存储文件,但你必须在加载后使用像这样令人作呕的黑客来正确解码数据:
String realProp = new String(prop.getBytes("ISO-8859-1"), "UTF-8");
但是,从JDK 1.6开始,有一个load()方法接受Reader而不是InputStream,这意味着您可以从一开始就使用正确的编码(还有一个store()方法接受Writer)。对我来说,这似乎是一件相当大的事情,但它似乎是悄无声息地潜入JDK的。我几周前才偶然发现它,快速搜索谷歌,只发现了一个提及。
推荐文章
- 在流中使用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