In languages like Java and C++, each variable must be declared and given a type. In Smalltalk, every variable must be declared, but it is not given a type. Smalltalk has an unusual syntax for variable declarations: the variables are simply listed enclosed in vertical bars.
int x, y, z; // Java/C++
| x y z | "Smalltalk"
One might reasonably ask, why bother to declare variables at all, if there is no typing information? The primary answer is that every variable must be declared so that the compiler can set aside space for each variable. Every variable will be allocated 32 bits and can hold either a pointer to an object or a SmallInteger.
A second reason for declaring variables is so that the compiler can guard against misspellings of variable names by the programmer. Every variable must be declared. If, in the body of executable statements, the programmer misspells a variable name, the compiler will complain that the variable has not been declared.
In languages like Java and C++, each variable must be declared and given a type. In Smalltalk, every variable must be declared, but it is not given a type. Smalltalk has an unusual syntax for variable declarations: the variables are simply listed enclosed in vertical bars.
int x, y, z; // Java/C++
| x y z | "Smalltalk"
One might reasonably ask, why bother to declare variables at all, if there is no typing information? The primary answer is that every variable must be declared so that the compiler can set aside space for each variable. Every variable will be allocated 32 bits and can hold either a pointer to an object or a SmallInteger.
A second reason for declaring variables is so that the compiler can guard against misspellings of variable names by the programmer. Every variable must be declared. If, in the body of executable statements, the programmer misspells a variable name, the compiler will complain that the variable has not been declared.
การแปล กรุณารอสักครู่..