M/F 2:30-3:20 |
CS 1130: Transition to OO Programming Spring 2016 |
Main
About: Overview Announcements Staff Consultants Calendar Materials: Texts DrJava Terminology Lectures: In-Class Web-Based VideoNote Assessment: Grading Assignments Labs Resources: CMS Piazza (link) Piazza (about) Java API Style Guide Academic Integrity |
Strong versus Weak TypingSafetyOne property that is sometimes looked for in a programming language is safety. There is some confusion as to what safety means, but here is a definition used in the year 2000 by an ad hoc committee that recommended that the Advanced Placement (AP) test in programming be based on the programming language Java:
C and C++ are not safe. An array index operation In the hands of a professional, the "unsafe" features can be used to advantage. But for most programmers, a language that provides safety can be of immense value. Especially for beginning programmers, safety helps. Strong typingMuch (but not all) of safety is enforced by strong typing —this term, too, is not well defined, so let's explain what we mean by it here. First, the type of every variable and every expression is a syntactic property —known by looking at the program, without resorting to executing it. Second, a variable may be used only in ways that respect its type —for example, it is impossible to perform double operations on an int value (the int value must first be cast to double format), or to use a boolean value as an integer. Thus, syntactically, it is impossible to have an operation disregard the types of its operands, to interpret a value as something that it is not. Runtime safety featuresThe strong typing of Java is backed up by strong guarantees on runtime
behavior, the goal being to ensure that no value can be interpreted as
something that it is not. With a String value, it is impossible to index
an character of the String that does not exist. For example Benefits of safety and strong typingSome errors arise from lack of understanding; others are logical errors caused by inadequate thinking and design; and some are simply typos. In all cases, finding errors early, at compile time, can save immense amounts of time. Safety and strong typing make possible the early detection of many errors. The beginning programmer will benefit from safety and strong typing more than the professional programmer, simply because the beginner tends to make more of each type of error. But the professional programmer will also gain from a safe and strongly type language. When a program consists of hundreds of files, it is too easy to get confused and make mistakes, and the more checks we have on our programs, the better off we are. Finally, with a language like Java, whose programs can be accessed on web pages (by writing applets), safety and strong typing aid the user as well, for these properties make it more difficult for malicious people to hack into systems through such applets. |