Software Engineering

Retrieving original hashcode of a Java object

For debugging Java applications, it is sometimes useful to know if two references point to the same or to different objects – this can be easily checked by evaluating the return value of hashCode() (the default implementation from java.lang.Object returns distinct integers for distinct objects). However, this might not work if hashCode() is overridden – in that case, hashCode() might return the same value for different objects to fulfill the equals() contract. In that case, it is still possible to retrieve the same hashCode() value as it would be returned by java.lang.Object if the hashCode() method was not overridden, by using System.identityHashCode() on this object:

public class Value {
    private int theValue;

    public Value(int val) {
        theValue = val;
    }

    @Override
    public int hashCode() {
        return theValue;
    }
	
    public static void main(String[] args) {
        Value value = new Value(42);
        System.err.println(value.hashCode());
        System.err.println(System.identityHashCode(value));
    }
}

Output:

42
1024180077

Retrieving original hashcode of a Java object Read More »

Enabling unrestricted security algorithms in Java

While I tried to reproduce this question on StackOverflow, I learned that it is required to install additional policy files in order to use strong encryption algorithms with the Java Cryptography Architecture (JCA). This article shows a sample encryption/decryption application and how to enable AES-256 support by installing the additional policy files: Using strong encryption in Java

Enabling unrestricted security algorithms in Java Read More »