标签云

微信群

扫码加入我们

WeChat QR Code

After reading Hidden Features of C# I wondered, What are some of the hidden features of Java?


Note that it's not always a great idea to use these hidden features; often times they are surprising and confusing to others reading your code.

2018年08月20日43分31秒

You (/someone) should probabbly sum up the answers neatly in the question body like the C# question.

2018年08月20日43分31秒

hidden feature? how so?

2018年08月20日43分31秒

Not exactly a "feature", but definitely "hidden".

2018年08月20日43分31秒

hehe a non-feature!

2018年08月20日43分31秒

Not just a non-feature, an anti-feature! (I also didn't mention that goto is the same--reserved but unimplemented).

2018年08月20日43分31秒

Peter Lawrey: I know, but it had already been posted.

2018年08月20日43分31秒

but Fast is not the same as Slow: the value of member "Fast.i" is NOT changed by/after the loop. If you call the run() method a second time, Slow will be much faster (increments "i" only once) and Fast will be as slows before since "Fast.i" is still zero.

2018年08月20日43分31秒

You are correct, Carlos. To make Fast and Slow have the same behaviour (in a single-threaded environment), the instance variable "i" would have to be updated at the end of the "run" method, which would not significantly impact performance.

2018年08月20日43分31秒

also got a "strange" result using System.currentTimeMillis() around the call to calculate the runtime: slow is faster than fast (slow=40.6s, fast=42.9s) for 1.6.0_13-b03 on WindowsXP

2018年08月20日43分31秒

Carlos: Try four runs for both classes without any potentially CPU-intensive programs running (e.g., virus checker, system update, browser). Also, throw out the first run in both test volleys. That Fast is slower than Slow by ~2 seconds leads me to believe something interfered with the runs. (That is, it should not take 2 seconds to get and set a variable via its accessor.)

2018年08月20日43分31秒

"Premature optimization" is a phrase used to describe a situation where a programmer lets performance considerations affect the design of a piece of code. This can result in a design that is not as clean as it could have been or code that is incorrect, because the code is complicated by the optimization and the programmer is distracted by optimizing. [ref: en.wikipedia.org/wiki/Optimization_%28computer_science%29]

2018年08月20日43分31秒

Do you mean StringBuilder instead of StringWriter? The APIs for StringBuffer and StringBuilder are the same however using StringWriter would require some code changes.

2018年08月21日43分31秒

Is the automatic resource management actually available?

2018年08月21日43分31秒

He actually meant StringBuilder which is not thread-safe but faster. StringBuffer is thread safe, but slower. Should be used only if you need thread safety while building your string buffer.

2018年08月20日43分31秒

Don't do your hack, please - it messes up all \uXXXX escape sequences. Better use native2ascii to convert the file after editing and before deploying, or use the reader variant of the load() method.

2018年08月20日43分31秒

This is also useful for custom serialization for objects with non-Serializable members.

2018年08月21日43分31秒

It’s only surprising if you fail to read the documentation.

2018年08月20日43分31秒

and you can access it like \u00dcberschrift = "OK";

2018年08月20日43分31秒

Not good style in my opinion. You know what I mean if you ever had to work with some code with comments and identifiers in a language you do not understand. Code should not be localized.

2018年08月20日43分31秒

deepc - "Hidden Features" may be no good style IMO...

2018年08月20日43分31秒

non-ASCII Strings should be externalized and build scripts and/or pre-commit verifiers should be configured to fail immediately upon detecting such clueless non-sense. Our Ant build scripts here will fail and put any developer trying to do that on the wall of shame.

2018年08月20日43分31秒

Thats so cool if some noob doesnt use UTF-8.

2018年08月21日43分31秒

In my opinion, this is not a hidden feature. The Scanner is a library.

2018年08月20日43分31秒

The setAccessible call can be forbidden by the Security manager, though.

2018年08月20日43分31秒

You can call clone on any Object. You just need to be careful that the Object implements a deep clone.

2018年08月20日43分31秒

Finbarr: Quite the reverse. It does a shallow clone; the inner objects just get another reference to them. The “simplest” way to deep clone is to serialize and deserialize, or to actually understand what you're making a copy of.

2018年08月20日43分31秒

Finbar: one can't call clone on any Object, only on objects from classes which made this method public, or are from the current class.

2018年08月20日43分31秒

Sun Java 6. Doesn't work with IBM Java.

2018年08月20日43分31秒

If you're using Spring to tool up your annotated JMX beans to work with jconsole and jvisualvm, you need to manually start the management interface rather than relying on the default VM-created one. If you don't, you'll run into problems with the presence of multiple clashing “singletons” and your JMX beans will neatly end up in the wrong one…

2018年08月21日43分31秒

WeakHashMap is not suitable for building caches.

2018年08月20日43分31秒

Of course, because you can't access non-finals.

2018年08月20日43分31秒

I knew that you can't acces non-finals, but I was surprised that you can access finals.

2018年08月20日43分31秒

It is a closure!

2018年08月20日43分31秒

How is that hidden?

2018年08月20日43分31秒

Great book, but it doesn't really deal so much with hidden features.

2018年08月20日43分31秒

that is both surprising and scary at the same time...

2018年08月21日43分31秒

Why is this surprising?

2018年08月20日43分31秒

It's surprising if you have exposure to an OO language other than C++.

2018年08月20日43分31秒

It's awful. Don't do it. It's not because I'm human that another human has the right to look into my bowels ;-)

2018年08月20日43分31秒

But how would you do a proper hash/equals implementation if you cant access its innards?

2018年08月20日43分31秒

...it's silly if you have a syntax highlighter.

2018年08月20日43分31秒

duplicated answer

2018年08月20日43分31秒

The exact limit is implementation dependent (has only some minimum size), so in some VMs the second test could be true, too.

2018年08月20日43分31秒

Thanks Paŭlo for this note

2018年08月20日43分31秒

Is it true to say that the third test will be false regardless of the VM ?

2018年08月20日43分31秒

Pacerier to be honest. not sure. but it worked at the time I was posting this ... just do not depend on the == operator ...

2018年08月20日43分31秒

So, they use the pointer to store small integer values, is that it?

2018年08月20日43分31秒

Your second link is broken

2018年08月20日43分31秒

I had the trailing comma break on a certain version of javac, 1.6.0_13 or 17 I think

2018年08月20日43分31秒

Usage example: java2s.com/Code/Java/JDK-6/…

2018年08月20日43分31秒

Not impossible, merely impossibly verbose.

2018年08月20日43分31秒

Isnt this a feature of opps than java specific??

2018年08月21日43分31秒