Thursday, December 01, 2011

Strong vs weak typing in programming

Example


Weak Typing Strong Typing
Pseudocode
a = 2
b = "2"
 
concatenate(a, b) # Returns "22"
add(a, b)         # Returns 4
a = 2
b = "2"
 
concatenate(a, b)     # Type Error
add(a, b)             # Type Error
concatenate(str(a), b) # Returns "22"
add(a, int(b))         # Returns 4
Languages Perl, PHP, Rexx, JavaScript, BASIC Java, C, C++, Python, C#, Vala

No comments: