CSC/ECE 517 Fall 2010/ch3 4b mt: Difference between revisions
Pseudonym1 (talk | contribs) No edit summary |
Pseudonym1 (talk | contribs) No edit summary |
||
Line 11: | Line 11: | ||
===Types=== | ===Types=== | ||
Time for an analogy? Imagine a variable to be a vacant house. It could have animals, humans, robots, or another smaller house, or whatever you can think of. There has to be a way, from a third party perspective, so as to be cognizant of the kind of things that reside in the house. | Time for an analogy? Imagine a variable to be a vacant house. It could have animals, humans, robots, or another smaller house, or whatever you can think of. There has to be a way, from a third party perspective, so as to be cognizant of the kind of things that reside in the house. Types and typing is a concept which realizes this concept. A typed language or a strongly typed language is one where the type of a variable has to be declared so as to avoid any wrong dealings of the value during run time. For example, Java is a strongly typed language. If I need to use 2 integers for addition, I would explicitly have to mention them as an integer:- | ||
<code><pre> | |||
Types and typing is a concept which realizes this concept. A typed language or a strongly typed language is one where the type of a variable has to be declared so as to avoid any wrong dealings of the value during run time. For example, Java is a strongly typed language. | int integer1; | ||
int integer2; | |||
If I need to use 2 integers for addition, I would explicitly have to mention them as an integer:- | </pre></code> | ||
int integer1; | |||
int integer2; | |||
However, we can’t do a character initialization in the course of a program. | However, we can’t do a character initialization in the course of a program. | ||
<code><pre> | |||
integer1 = “Hello World”; | |||
</pre></code> | |||
Is however, not allowed. | Is however, not allowed. | ||
whereas, in case of non-typed languages, say Ruby, or Visual Basic etc. for example, we don’t need to specify a type. | whereas, in case of non-typed languages, say Ruby, or Visual Basic etc. for example, we don’t need to specify a type. | ||
<code><pre> | |||
def variable1; | |||
def variable2; | |||
</pre></code> | |||
Now, it could be initialized with anything, | Now, it could be initialized with anything, | ||
variable1 = 3 | variable1 = 3 | ||
as well as | as well as | ||
Line 56: | Line 56: | ||
==Ruby== | |||
Ruby claims to be a strong object oriented language. It is object oriented to the extent that every thing is an object. Primitive datatypes are objects, and we can make use of the object oriented techniques on even the primitives. As mentioned before, Ruby is a dynamic typed language, as the type is bound at compile time. Everything in Ruby has a class. Every class in Ruby is inherited from class Class. | |||
Type in Ruby is well defined as well. Using self inspection (reflection), we cam | |||
==References== | ==References== |
Revision as of 22:58, 20 October 2010
Types vs Classes
Introduction
It is often that we talk about types, strongly typed, weakly typed, classes. To make it clear, let us look into it clearly. Every programming language has a way to define a variable. A variable is a named address location which stores a particular value.
Types
Time for an analogy? Imagine a variable to be a vacant house. It could have animals, humans, robots, or another smaller house, or whatever you can think of. There has to be a way, from a third party perspective, so as to be cognizant of the kind of things that reside in the house. Types and typing is a concept which realizes this concept. A typed language or a strongly typed language is one where the type of a variable has to be declared so as to avoid any wrong dealings of the value during run time. For example, Java is a strongly typed language. If I need to use 2 integers for addition, I would explicitly have to mention them as an integer:-
int integer1;
int integer2;
However, we can’t do a character initialization in the course of a program.
integer1 = “Hello World”;
Is however, not allowed.
whereas, in case of non-typed languages, say Ruby, or Visual Basic etc. for example, we don’t need to specify a type.
def variable1;
def variable2;
Now, it could be initialized with anything,
variable1 = 3
as well as
variable1 = “Good morning Vietnam” are allowed.
Classes
A class is a “type” of variable specification. The difference between a class and a regular primitive integer, character etc is that a class is:-
1)A class consists of one or more primitive or non-primitive variables 2)It is a pre defined structure
The Type of Class is “Class”, just like the Type of integer1 is “integer” in case of strongly typed languages.
For example, in case of Java:-
“The types of the Java programming language are divided into two categories: primitive types and reference types. The primitive types are the boolean type and the numeric types. The numeric types are the integral types byte, short, int, long, and char, and the floating-point types float and double. The reference types are class types, interface types, and array types. There is also a special null type. An object is a dynamically created instance of a class type or a dynamically created array. The values of a reference type are references to objects. All objects, including arrays, support the methods of class Object . String literals are represented by String objects .” [2]
So, as mentioned before, in Java, there are two “Types”:-
i)Primitive Type ii)Reference Type :- Classes, Interfaces, Arrays are all consolidations of Primitive types, so they just need to “point” to or reference the primitive ones, hence the name.
Ruby
Ruby claims to be a strong object oriented language. It is object oriented to the extent that every thing is an object. Primitive datatypes are objects, and we can make use of the object oriented techniques on even the primitives. As mentioned before, Ruby is a dynamic typed language, as the type is bound at compile time. Everything in Ruby has a class. Every class in Ruby is inherited from class Class.
Type in Ruby is well defined as well. Using self inspection (reflection), we cam
References
[1] http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
[2] Java:- Types, Values and Variables
http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html