import java.math.BigDecimal; import java.math.MathContext; public class BigDecimalTest { public static void main(String[] args) { System.out.println(new BigDecimal(0).equals(BigDecimal.ZERO)); System.out.println(new BigDecimal("0").equals(BigDecimal.ZERO)); System.out.println(new BigDecimal(0.0d).equals(BigDecimal.ZERO)); System.out.println(new BigDecimal(0.0f).equals(BigDecimal.ZERO)); //WTF System.out.println(new BigDecimal("0.0").equals(BigDecimal.ZERO)); // false!!! System.out.println(new BigDecimal("0.0").equals(new BigDecimal("0"))); // false!!! System.out.println(new BigDecimal("0.0").equals(new BigDecimal(0.0))); // false!!! //same is with "1" and "1.0", and so on //That's not a precision problem MathContext mc = new MathContext(2); System.out.println(new BigDecimal("0.0", mc).equals(new BigDecimal(0.0, mc))); // false!!! //So, the proper way to compare two big decimals is: System.out.println(new BigDecimal("0.0").compareTo(BigDecimal.ZERO) == 0); }The answer lies in API docs: BigDecimal#equals.
Monday, 18 January 2010
Proper way to compare java.math.BigDecimal
Wednesday, 13 January 2010
Make Eclipse faster by limiting number of open editor tabs
Eclipse can close tabs automatically. This improves performance, saves memory and keeps your workspace cleaner.
- Open Preferences
- Go to General > Editors
- Set "Number of opened editors before closing" to something small. I use 8.
- Apply and enjoy
Friday, 8 January 2010
Importing self signed certificate into Java's cacerts keystore
Symptoms
Possible solution
- Getting an exception:
javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
- Self-signed SSL certificate is used on remote server.
Possible solution
- Open remote page with Firefox. You should see something like a warning page.
- Click on "I Understand the Risks". You should really understand them.
- Click "Add Exception".
- Click "View" to view certificate.
- Go to "Details" tab and click "Export".
- Save certificate as X.509 Certificate (PEM) somewhere to your file system
- Don't forget to click "Confirm Security Exception" in Firefox.
- Execute command:
keytool -keystore $JAVA_HOME/jre/lib/security/cacerts -import -file /path/to/cert.cer
- When you are prompted for a password and you haven't changed it, use the default:
changeit
Thursday, 7 January 2010
Introduction
Java WTF is...
- a collection of short and concrete tips about Java development
- a collection of Java developer's problems along with short and concrete solutions
- no bullshit
Subscribe to:
Posts (Atom)