Data Types
Strings
- What are the different ways Strings can be implemented in programming languages?
- Array of
char
(i.e., nothing too special) - Fundamental, built-in data type
- Just another class
- A very special class.
- Array of
- Example language for each type?
- What are the relative advantages and disadvantages of each?
- Making the string class special means things like
=
just work. - Similarly, string ops can be done using built-in operators (e.g.,
+
) instead of more verbose method calls
- Making the string class special means things like
- 2nd key question: Should strings have static or dynamic length
- Java strings are immutable. Can’t modify them, you have to get a new one
Enumerations
- What’s bad about just using constants?
- (e.g.,
check_for_win(....., 1)
)
- (e.g.,
- Why not just set up
const int HORIZONTAL = 1
?- It solves some problems, but allows others (like setting month to 100)
- Enums are often just integers “under the hood”. With that in mind, do you
- Allow assignments like
DayOfWeek dueDate = 4
(if there are at lest 4 valid values) - Allow
dueDate += 1
? - Allow users to choose the underlying integers? (
enum colors {red = 1, blue = 1000, green = 100000})
- Allow assignments like
- What are the main language criteria affected by the design and use of enums?
- Readability, reliability.
- Java Enums are interesting because they are an enumerated list of class instances. Thus, they are objects and can have methods and instance data https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
Strong Typing
- Note: The concept of strong typing is not formal, and not everybody agrees on what exactly it means.
- A language is strongly typed if “type errors are always detected” (either at
compile-time or run time).
- (Notice that this is not the same as statically typed.)
- Is C strongly typed? Why/why not? (Give examples.)
- Is C++ strongly typed? Why/why not? (Give examples.)
- Is Java strongly typed?
* Mostly. Consider
float x = y + z
where y is a float and z is an int. This is almost always what the programmer wants; but, it could be an error in rare cases. - Is JavaScript strongly typed?
- No. It will do all kinds of type conversions automatically:
10 + "some string"
- No. It will do all kinds of type conversions automatically:
- In general coercion reduces reliability because it reduces the number and types of mistakes that can be automatically detected. Consider the following:
int a;
int b
double c;
double d;
c = a + b
Suppose the programmer meant to type a + d
. A language without coercion would catch the error.