views:

504

answers:

7

Are there any other ways of changing a variable's type in a statically typed language like Java and C++, except 'casting'?

I'm trying to figure out what the main difference is in practical terms between dynamic and static typing and keep finding very academic definitions. I'm wondering what it means in terms of what my code looks like.

+3  A: 

In C++/Java you can't change the type of a variable.

Static typing: A variable has one type assigned at compile type and that does not change.

Dynamic typing: A variable's type can change while runtime, e.g. in JavaScript:

js> x="5" <-- String
5
js> x=x*5 <-- Int
25
Johannes Weiß
Doesn't casting change the type?
@Mark, You can change the type of a VALUE, but not a VARIABLE. int i = 42; // i cannot change to a float. However, the value of i (42) can. See my example.
strager
Not the type of a variable. It can change the type of a value/expression
Johannes Weiß
The runtime type of the object will always be the same. The type of the reference can be the op of the runtime object or anything up in the hierarchy.
John Ellinwood
+1  A: 

The main difference is that in dynamically typed languages you don't know until you go to use a method at runtime whether that method exists. In statically typed languages the check is made at compile time and the compilation fails if the method doesn't exist.

tvanfosson
A: 

You don't need explicit casting. In many cases implicit casting works.

For example:

int i = 42;
float f = i; // f ~= 42.0
int b = f;   // i == 42

class Base {
};

class Subclass : public Base {
};

Subclass *subclass = new Subclass();
Base *base = subclass;  // Legal

Subclass *s = dynamic_cast<Subclass *>(base); // == subclass.  Performs type checking.  If base isn't a Subclass, NULL is returned instead.  (This is type-safe explicit casting.)

You cannot, however, change the type of a variable. You can use unions in C++, though, to achieve some sort of dynamic typing.

strager
+1  A: 

I'm wondering what it means in terms of what my code looks like.

The type system does not necessarily have any impact on what code looks like, e.g. languages with static typing, type inference and implicit conversion (like Scala for instance) look a lot like dynamically typed languages. See also: What To Know Before Debating Type Systems.

Fabian Steeg
A: 

Lets look at Java for he staitically typed language and JavaScript for the dynamc. In Java, for objects, the variable is a reference to an object. The object has a runtime type and the reference has a type. The type of the reference must be the type of the runtime object or one of its ancestors. This is how polymorphism works. You have to cast to go up the hierarchy of the reference type, but not down. The compiler ensures that these conditions are met. In a language like JavaScript, your variable is just that, a variable. You can have it point to whatever object you want, and you don't know the type of it until you check.

For conversions, though, there are lots of methods like toInteger and toFloat in Java to do a conversion and generate an object of a new type with the same relative value. In JavaScript there are also conversion methods, but they generate new objects too.

John Ellinwood
So, toFloat in Java doesn't change an existing object. I'm looking at Ruby where you can say x="abc" and then x="100" - you said Javascript generates new objects but I thought Ruby just changes the existing object to a new type. Or am I wrong?
+8  A: 

Make sure you don't get static vs. dynamic typing confused with strong vs. weak typing.

  • Static typing: Each variable, method parameter, return type etc. has a type known at compile time, either declared or inferred.
  • Dynamic typing: types are ignored/don't exist at compile time
  • Strong typing: each object at runtime has a specific type, and you can only perform those operations on it that are defined for that type.
  • Weak typing: runtime objects either don't have an explicit type, or the system attempts to automatically convert types wherever necessary.

These two opposites can be combined freely:

  • Java is statically and strongly typed
  • C is statically and weakly typed (pointer arithmetics!)
  • Ruby is dynamically and strongly typed
  • JavaScript is dynamically and weakly typed

Genrally, static typing means that a lot of errors are caught by the compiler which are runtime errors in a dynamically typed language - but it also means that you spend a lot of time worrying about types, in many cases unnecessarily (see interfaces vs. duck typing).

Strong typing means that any conversion between types must be explicit, either through a cast or through the use of conversion methods (e.g. parsing a string into an integer). This means more typing work, but has the advantage of keeping you in control of things, whereas weak typing often results in confusion when the system does some obscure implicit conversion that leaves you with a completely wrong variable value that causes havoc ten method calls down the line.

Michael Borgwardt
Awesome. Thanks.
A: 

Your code should actally not look very much different, regardless if you are using a staticly typed language or not. Just because you can change the data type of a variable in a dynamically typed language, doesn't mean that it is a good idea to do so.

In VBScript, for example, hungarian notation is often used to specify the preferred data type of a variable. That way you can easily spot if the code is mixing types. (This was not the original use of hungarian notation, but it's pretty useful.)

By keeping to the same data type, you avoid situations where it's hard to tell what the code actually does, and situations where the code simply doesn't work properly. For example:

Dim id
id = Request.QueryString("id") ' this variable is now a string
If id = "42" Then
  id = 142 ' sometimes turned into a number
End If
If id > 100 Then ' will not work properly for strings

Using hungarian notation you can spot code that is mixing types, like:

lngId = Request.QueryString("id") ' putting a string in a numeric variable

strId = 42 ' putting a number in a string variable
Guffa