在阅读了c#的隐藏特性之后,我想知道Java的隐藏特性有哪些?
当前回答
我喜欢方法的静态导入。
例如,创建以下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");
}
}
其他回答
也许最令人惊讶的隐藏特性是sun.misc.Unsafe类。
http://www.docjar.com/html/api/ClassLib/Common/sun/misc/Unsafe.java.html
你可以;
Create an object without calling a constructor. Throw any exception even Exception without worrying about throws clauses on methods. (There are other way to do this I know) Get/set randomly accessed fields in an object without using reflection. allocate/free/copy/resize a block of memory which can be long (64-bit) in size. Obtain the location of fields in an object or static fields in a class. independently lock and unlock an object lock. (like synchronize without a block) define a class from provided byte codes. Rather than the classloader determining what the byte code should be. (You can do this with reflection as well)
BTW:不正确地使用这个类会杀死JVM。我不知道哪个jvm支持这个类,所以它不能移植。
那么自JDK 1.5以来就存在的协变返回类型呢?它的宣传很差,因为它是一个不吸引人的附加,但据我所知,它对泛型工作是绝对必要的。
本质上,编译器现在允许子类将被覆盖方法的返回类型缩小为原始方法返回类型的子类。所以这是允许的:
class Souper {
Collection<String> values() {
...
}
}
class ThreadSafeSortedSub extends Souper {
@Override
ConcurrentSkipListSet<String> values() {
...
}
}
您可以调用子类的values方法并获得一个排序的线程安全的字符串集,而不必向下强制转换为ConcurrentSkipListSet。
"const"是一个关键字,但你不能使用它。
int const = 1; // "not a statement"
const int i = 1; // "illegal start of expression"
我猜编译器的作者认为将来可能会用到它,他们最好保留它。
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).
真正让我吃惊的是自定义序列化机制。
而这些方法是私有的!!,它们会在对象序列化期间被JVM“神秘地”调用。
private void writeObject(ObjectOutputStream out) throws IOException;
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;
通过这种方式,您可以创建自己的自定义序列化,使其更加“随便”(安全、快速、罕见、简单等)。
如果大量信息必须通过节点传递,这是真正应该考虑的事情。序列化机制可以更改为发送数据的一半。很多时候,瓶颈并不在平台上,而是在通过线路发送的数量上,这可能会为您节省数千个硬件dll。
这是一篇文章。 http://java.sun.com/developer/technicalArticles/Programming/serialization/
推荐文章
- 在流中使用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