Master thesis - rules for italicizing
I'd say the typeface should match the one used in the equation or listing, so that the entity always appears the same no matter where it is located.

If you're using Latex, variables from equations should be put in math mode, i.e. $n$. Class names and data types should be inline code, e.g. \lstinline if you're using the package listings.