Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe there are two related uses of canonical: forms and instances.</p> <p>A <strong>canonical form</strong> 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 <em>canonized</em>, 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:</p> <pre><code>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 </code></pre> <p>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 <em>the aliasing problem</em>).</p> <p>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.</p> <p>Forcing objects into their <strong>canonical instances</strong> 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.</p> <p>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 (<code>equals()</code> method).</p> <p>A classic example of optimizing performance with canonical instances is collapsing strings with the same content. Calling <code>String.intern()</code> 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</p> <p>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 <code>if (day == Days.SUNDAY)</code> with impunity in java if <code>Days</code> is an enum type. Doing this for your own classes is certainly possible, but takes care. Read <em>Effective Java</em> by Josh Bloch for details and advice.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload