Today I Learned

Some of the things I've learned every day since Oct 10, 2016

Category Archives: programming

105: Loitering Objects (Java)

In Java (and possibly other languages, not sure), loitering objects refers to objects in memory which are no longer desired or will no longer be used in the future but still have pointers pointing to them, preventing garbage collection from freeing their place in memory. It’s generally good form to not waste memory in this fashion, but in extreme cases loitering objects can even lead to unwanted program termination due to a \texttt{java.lang.OutOfMemoryError}.

102: Kernel Space vs. User Space

Kernel space and user space are separate regions of virtual memory distinguished from each other in an operating system for purposes of security, stability, and centralizing control. Typically, user space is the space which user applications have direct access to and can work with, while kernel space is a privileged space which only especially stable and trustworthy programs, such as the kernel, can access. This barrier essentially insulates possibly insecure or unstable applications from power, preventing them from doing things like crashing the system or messing with other applications’ memory.

100: Primitive Types vs. Reference Types (Java)

In the Java programming language, values either have a primitive type or a reference type. The primitive types are:

  • boolean
  • byte
  • short
  • char
  • int
  • long
  • float
  • double

All other types not in this list are reference types.

A value with a primitive type stores the information determining the value itself, such as the binary code for a given integer or character. By contrast, a value with a reference type stores the address of the location in memory where this information can be found. For instance, after declaring and instantiating a variable

\texttt{String s = "string";}

the variable \texttt{s} doesn’t store the code for the String itself, but rather a ‘pointer’ to where this code can be found.

This has important consequences for how primitive values are assigned to variables or passed as parameters, as opposed to reference values. A primitive value passed or assigned will have the bits describing the value itself copied to the target, whereas a reference value will merely have the address of these bits copied.

87: Fuzzing (Software Testing)

In software testing, fuzzing is testing a program by running it on randomly-generated input. A fuzzer can generate input by mutating given examples of input, or by simply generating instances of input based on a model.

On the plus side, fuzzing can often find problems that the testers wouldn’t think to check for. At the same time, though, fuzzing often only finds very simple faults (e.g., input which produces ‘completely wrong’ behavior, but possibly not ‘deeper’ program faults). As always, fuzzing is not a substitute for other testing methods and should be viewed as a complement to them.

71: Ropes (Data Structure)

rope is a data structure which stores long strings as a type of binary tree rather than a ‘monolithic’ list of characters. The nodes of the tree contain weights, and the leaves of the tree contain small substrings of the larger string.

A rope is advantageous over the latter structure with respect to speed of destructive concatenation, insertion, deletion, as well as extra memory required during operations, but is disadvantageous in speed of splitting, appending, and extra memory required while the structure is not being operated on.

70: Corecursion

Corecursion, a sort of dual procedure to recursion, starts with a base case and builds upon it iteratively, as opposed to recursion which breaks down towards a base case.

A good example of corecursion is the evaluation of a stream, which starts from a base case (the first item in the stream as well as a method for computing the rest of the stream), and uses it to iteratively produce the rest of the infinite data structure.

63: Mock Objects

In programming with object-oriented design, mock objects are objects which simulate in tests or experiments the behavior of other objects of interest which themselves can’t practically be used.

Examples of objects which may lend themselves to substitution by mock objects include those which have states that are hard to recreate (such as ones following non-deterministic processes) and those that simply do not yet exist.

62: Homoiconicity

In programming, homoiconicity a property of languages in which programs are represented as instances of a data structure of a primitive type of the language. This takes advantage of the fact that the information of programs and data can be carried through the same medium by storing a program’s code in a form that the language knows how to access and manipulate.

This can make metaprogramming much easier than it otherwise would be. A good example of a language exhibiting homoiconicity is LISP, where everything is represented as a list (S-expression). Since both programs and data are stored as lists, a LISP program can manipulate another program as easily as it would any data.

53: Boxing (Java)

In the Java programming language, boxing refers to the conversion of a primitive value to some equivalent object wrapping that value, which allows the object containing the value to be passed by reference. (There are probably other benefits/motivations that I’m unaware of.) The conversion back to a primitive value is called unboxing. When boxing and unboxing are done automatically by the Java compiler, it is referred to as autoboxing.

A common example is the autoboxing between \texttt{int}s (a primitive) and \texttt{Integer}s (a class). For instance, when

\texttt{Integer x = 1}

is executed, there is no error, even though \texttt{1} is an \texttt{int}. The Java compiler autoboxes the \texttt{int}   \texttt{1} into an equivalent  \texttt{Integer} object (\texttt{Integer(9)}) and binds \texttt{x} to that object.