Data Types

2014-03-10

I took a programming languages class as part of my degree program in college. In that class, we covered the strongly and weakly classifications of languages as well as the static and dynamic classifications of languages. This was a topic that was barely covered by the textbook, so I did not give it much thought at the time.

Since then, I have given the topic of data types much thought.

Static vs Dynamic

Understanding the difference between static and dynamic languages is easy. Static variables are explicitly defined. Dynamic variables are implicitly defined.

C++ – Static

    String bookTitle; // will only allow strings and will treat its contents
                      // as strings.
    bookTitle = "Great Expectations"; // only allows strings
    bookTitle = 47; // error

Dart – Dynamic

    /** Technically, Dart is "optionally typed" in that variables can be
    either static, if defined with a specific type, or dynamic, if defined
    without a specific type, as below. */

    var bookTitle; // no idea what this is yet.
    bookTitle = "Great Expectations" // ah, it’s a String!

Strong vs Weak

Definitions are hard to come by for strong and weak typing. Some definitions say that these are defined at run-time or compile-time; but I have not been able to find consensus about which is which. In my research, this appears to change from language to language. In other words, there does not appear to be a formal definition of strong or weak typing. Yet, weakly typed languages appear to be those that allow the mixing of data types that make implicit assumptions regarding the intention of the developer. Strongly typed languages only allow the mixing of data types when they are explicitly cast to the same data type.

PHP – Weak

    $num = "4"; // this is an implicitly defined String
                // (dynamic typing in action)
    print( $num + 2 ); // Allowed: 6

Java – Strong

    String num1 = "4";
    int num2 = 2;

    // Not allowed. Throws "Breaking on exception: object of type TypeError"
    print( num1 + num2 );

    // Allowed since num1 is a String and num2 is cast to a String: "42"
    print( num1 + num2.toString());

Strong vs. Weak vs. Static vs. Dynamic

I tend to think of these four types of typing not as discrete definitions but as inclusive adjectives to help describe a language.

  STRONG WEAK
DYNAMIC Dart PHP
STATIC C++, Dart, Java C

“Strong” and “weak” are loaded terms that can be used as a political cudgel by speakers against what they think of as lesser languages. Yes, PHP is a weakly typed language, but it also has type-hinting within OOP, which adds some strength to its weak typing system.

Use “strong” and “weak” with a grain of salt. “Dynamic” and “static” are much more useful terms.

Categories: programming

Tags: php, dart, java, javascript, cpp