Category: Java

Beware of System.nanoTime() in Java

25 April, 2008 (16:45) | Java, Programming

The documentation for System.nanoTime() (and the function’s name itself) leads developers to believe it’s a much more accurate timer than anything else Java provides. Depending on which set of documentation you read, it claims to use “the most precise available system timer”. While I can’t speak for any architecture other than x86, I […]

The difference between Java’s “final” and C#’s “const”

15 February, 2008 (20:01) | .Net, Java, C#, Computer Science

Final in Java is used to declare a class that can’t be subclassed, a method that can’t be overridden, or …
A final variable means the value won’t change, but the value can still be determined at run time:

final int i;
if ( j > 0 ){
i =1;
} else {
i = […]

Passing immutable types by reference in Java and C#

12 October, 2007 (18:26) | Java, C#, Computer Science

To many, it should be obvious what the following code prints:

public static void main(String[] args){
int x=0;
SomeMethod(x);
System.out.println(x);
}

protected static void SomeMethod(int x){
x=1;
}

The code prints 0, because “int” is a native type and is passed by value. […]

MD5 in a few lines of Java

12 March, 2007 (10:58) | Cryptography, Java, Security, Snippets

1 import java.security.*;
2 import java.math.*;
3
4 public class MD5 {
5 public static void main(String args[]) throws Exception{
6 String s="This is a test";
[…]