Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the term "canonical form" or "canonical representation" in Java mean?

Tags:

java

I have often heard this term being used, but I have never really understood it.

What does it mean, and can anyone give some examples/point me to some links?

EDIT: Thanks to everyone for the replies. Can you also tell me how the canonical representation is useful in equals() performance, as stated in Effective Java?

like image 942
Shivasubramanian A Avatar asked Nov 11 '08 05:11

Shivasubramanian A


People also ask

What is the meaning of canonical form?

Definition of canonical form : the simplest form of something specifically : the form of a square matrix that has zero elements everywhere except along the principal diagonal.

Why is canonical form representation used?

Canonical form is a term commonly used among computer scientists and statisticians to represent any mathematical object that has been reduced down as far as possible into a mathematical expression.

Why is it called canonical form?

According to OED and LSJ, the term canonical stems from the Ancient Greek word kanonikós (κανονικός, "regular, according to rule") from kanṓn (κᾰνών, "rod, rule"). The sense of norm, standard, or archetype has been used in many disciplines.

What is canonical form in syntax?

​the most basic form of a grammatical structure or expression, for example the infinitive in the case of a verb.


2 Answers

I believe there are two related uses of canonical: forms and instances.

A canonical form means that values of a particular type of resource can be described or represented in multiple ways, and one of those ways is chosen as the favored canonical form. (That form is canonized, like books that made it into the bible, and the other forms are not.) A classic example of a canonical form is paths in a hierarchical file system, where a single file can be referenced in a number of ways:

myFile.txt                                   # in current working dir ../conf/myFile.txt                           # relative to the CWD /apps/tomcat/conf/myFile.txt                 # absolute path using symbolic links /u1/local/apps/tomcat-5.5.1/conf/myFile.txt  # absolute path with no symlinks 

The classic definition of the canonical representation of that file would be the last path. With local or relative paths you cannot globally identify the resource without contextual information. With absolute paths you can identify the resource, but cannot tell if two paths refer to the same entity. With two or more paths converted to their canonical forms, you can do all the above, plus determine if two resources are the same or not, if that is important to your application (solve the aliasing problem).

Note that the canonical form of a resource is not a quality of that particular form itself; there can be multiple possible canonical forms for a given type like file paths (say, lexicographically first of all possible absolute paths). One form is just selected as the canonical form for a particular application reason, or maybe arbitrarily so that everyone speaks the same language.

Forcing objects into their canonical instances is the same basic idea, but instead of determining one "best" representation of a resource, it arbitrarily chooses one instance of a class of instances with the same "content" as the canonical reference, then converts all references to equivalent objects to use the one canonical instance.

This can be used as a technique for optimizing both time and space. If there are multiple instances of equivalent objects in an application, then by forcing them all to be resolved as the single canonical instance of a particular value, you can eliminate all but one of each value, saving space and possibly time since you can now compare those values with reference identity (==) as opposed to object equivalence (equals() method).

A classic example of optimizing performance with canonical instances is collapsing strings with the same content. Calling String.intern() on two strings with the same character sequence is guaranteed to return the same canonical String object for that text. If you pass all your strings through that canonicalizer, you know equivalent strings are actually identical object references, i.e., aliases

The enum types in Java 5.0+ force all instances of a particular enum value to use the same canonical instance within a VM, even if the value is serialized and deserialized. That is why you can use if (day == Days.SUNDAY) with impunity in java if Days is an enum type. Doing this for your own classes is certainly possible, but takes care. Read Effective Java by Josh Bloch for details and advice.

like image 170
Dov Wasserman Avatar answered Oct 02 '22 16:10

Dov Wasserman


Wikipedia points to the term Canonicalization.

A process for converting data that has more than one possible representation into a "standard" canonical representation. This can be done to compare different representations for equivalence, to count the number of distinct data structures, to improve the efficiency of various algorithms by eliminating repeated calculations, or to make it possible to impose a meaningful sorting order.

The Unicode example made the most sense to me:

Variable-length encodings in the Unicode standard, in particular UTF-8, have more than one possible encoding for most common characters. This makes string validation more complicated, since every possible encoding of each string character must be considered. A software implementation which does not consider all character encodings runs the risk of accepting strings considered invalid in the application design, which could cause bugs or allow attacks. The solution is to allow a single encoding for each character. Canonicalization is then the process of translating every string character to its single allowed encoding. An alternative is for software to determine whether a string is canonicalized, and then reject it if it is not. In this case, in a client/server context, the canonicalization would be the responsibility of the client.

In summary, a standard form of representation for data. From this form you can then convert to any representation you may need.

like image 32
Brian Gianforcaro Avatar answered Oct 02 '22 17:10

Brian Gianforcaro