CSC/ECE 517 Fall 2012/ch1 1w21 aa: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 277: Line 277:
The various disadvantages of using the primitive data types are as follows:
The various disadvantages of using the primitive data types are as follows:
* Most of the object oriented programming languages do not allow the behavior or capabilities of primitive data types to be modified by programs.For example, the primitive data types in languages such as Java cannot be inherited to create further subtypes. An exception to this includes Smlltalk. It allows all data types to be extended within a program. It allows the programmer to modify the operations that can be performed on them and also to redefine the built in operations.
* Most of the object oriented programming languages do not allow the behavior or capabilities of primitive data types to be modified by programs.For example, the primitive data types in languages such as Java cannot be inherited to create further subtypes. An exception to this includes Smlltalk. It allows all data types to be extended within a program. It allows the programmer to modify the operations that can be performed on them and also to redefine the built in operations.
* Most of the object oriented languages have certain utility functions associated with non primitive data types that eases their use as compared to primitive data types that do not have such functions. An exception to this is Ruby. It has utility functions associated with all the data types.


== Conclusion ==
== Conclusion ==

Revision as of 23:44, 9 September 2012

Primitive Objects

Introduction

Primitive datatypes are those that are not defined in terms of other data types[1]. It is a basic or in-built type that a programming language provides for its users. These data types can be used for building other complex and composite data types [2]. In most languages, primitive types usually consist of basic value types for representing the data to be stored. As these data types are built-in, the compilers for these languages usually provide inbuilt support for these data types and so would support most basic operations. In dynamically typed languages like Ruby, these are represented as objects and are referred to as primitive objects. This article discusses the various primitive data types/objects that are present in various objected oriented programming languages and their benefits.

Some of the common data types that are present in most programming languages are

  • Integer - used to hold whole numbers. Can be signed or unsigned and include various types like byte, short, int, long, etc.
  • Character - used to hold single character, digit or symbol or a sequence of any combination of them that is stored in numeric coding. Usually stored in ASCII encoding. '
  • Floating-point - used to represent real numbers with limited precision. They usually comprise of an exponent part and a fractional part. Can be signed or unsigned .
  • Fixed-point - similar to floating-point, but has fixed number of digits after the decimal point [3]. They are used in processors or representing base 2 and base 10 fractional values where there is no Floating point unit.
  • Boolean - used to represent logical values used for comparisons. Typically hold either of 2 values - true/false. Can also be represented as integers. Can be stored in a single bit, but typically stored as a byte.

Primitive data types in C++

C++ is a statically typed, objected oriented programming language. It is widely used on a lot of hardware and software platforms. As C++ just adds object oriented features and a few other enhancements to C, the primitive data types that it provides are same as those provided by C[4]. They are described below [5].

Name Description Size Range
int Basic numerical data. Can have modifiers that vary size and range. 4 bytes -2,147,483,648 to 2,147,483,647
char Can hold one character of data like an alphabet, number, or symbol represented in ASCII 1 byte -128 to 127
wchar_t used for storing compiler-defined wide characters and unicode characters [6] compiler defined compiler defined
float single precision floating-point 4 bytes ± 3.402,823,4 * 10^38
double double precision floating-point 8 bytes +/–1.7 * 10^308 [7]
bool represents logical values 1 byte true/false
void generic identifier that does not identify type [8] Does not exist Does not exist

C++ allows variables of any of these data types to be created and also supports using these data types to construct complex data types like structures and classes. C++ also provides pointers that can be used to store the address of a variable of any simple or complex data type.

Primitive data types in Java

There is a certain group of data types that is used very frequently by the programmer. These types are included in the Java language as the primitive data types.These data types have a fixed size which does not change from one system to another and adds to the portability feature of java. There are 8 primitive data types defined in java which are as follows:[9]

Name Description Size(in bits) Range Usage Default Value
byte Signed two's complement integer 8 bits -128 to 127 This datatype can be used in arrays where there is a space constraint to save memory. 0
short Signed two's complement integer 16 bits -32,768 to 32,767 This datatype can also be used to save memory in large arrrays. 0
int signed two's complement integer 32 bits -2,147,483,648 to 2,147,483,647 This datatype is generally the default datatype for all the numbers we use in our program. 0
long signed two's complement integer 64 bits -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807 This type is used when we require a value that is outside the range of values provided by int. 0L
float Single-precision IEEE 754 floating point 32 bits 32-bit IEEE 754 floating-point numbers. Use this datatype to save memory in large arrays. 0.0f
double Double-precision 64-bit IEEE 754 floating point 64 bits 64-bit IEEE 754 floating-point numbers. This data type is generally the default for decimal values. 0.0d
boolean Boolean 1- bit false, true Use this data type for flags that track a true or false condition. false
char a char is a single 16-bit character encoded using Unicode 16 bit Unicode character \u0000(0) through unicode character \uffff(65,535) This type is used to define single characters. '\u0000'

Besides these 8 primitive data types java provides support to characterstrings through Java.lang.String class. String is not a primitive datatype but its usage and functionality makes us think of it being so.

Primitive data types in C#

C# is a statically-typed object oriented programming language. C# provides all the data types that are available in Java, and adds support for unsigned numerals and a new 128-bit high-precision floating-point type. All primitive data types in C# are objects in the System namespace. The various primitive data types in C# are defined below:[10]

Name .NET Class Size Description Range
byte System.Byte 8 bits 8-bit unsigned integral type. 0 to 255
sbyte System.SByte 8 bits signed two's complement integer -128 to 127
short System.Int16 16 bits 16-bit signed integral type. -32,768 to 32,767
ushort System.UInt16 16 bits 16-bit unsigned integral type. 0 to 65,535
int Int32 32 bits signed two's complement integer -2,147,483,648 to 2,147,483,647
uint System.Int32 32 bits 32-bit signed integral type. 0 to 4,294,967,295
long System.Int64 64 bits 64-bit signed integral type. -9,223,372,036,854,775,808 to 9,223,373,036,854,775,807
ulong System.UIint64 64 bits 64-bit unsigned integral type. 0 to 18,446,744,073,709,551,615
float System.Single 32 bits Single-precision floating-point type. -3.402823e38 to 3.02823e38
double System.Double 64 bits Double-precision floating-point type. -1.79769313486232e308 to 1.79769313486232e308
bool System.Boolean 1 bit Logical Boolean type false, true
char System.Char 16 bits A 16-bit Unicode character. Unicode character \u0000 through unicode character \uffff
object System.Object N/A Ultimate base type of all other types. N/A
string System.String N/A A sequence of Unicode characters. N/A
decimal System.Decimal 128 Precise decimal with 28 significant digits. ±1.0 × 10e−28 to ±7.9 × 10e28

Primitive data types/objects in Ruby

Ruby is a multi-paradigm programming language. It is dynamically typed, and allows to be programmed procedural, object-oriented or functional[11]. As it is also a pure object oriented programming language, even the basic inbuilt types like integer and string and even constants are represented as objects. There are a lot of inbuilt classes that are provided by Ruby[12]. These classes differ in some extent to their counterparts in other languages. Having all the basic types as classes allows a lot of methods to be executed on them that reduces the amount of code that needs to be newly written. Some of the basic classes are:

Name Description Range
Integer base class for FixNum and BigNum [13] depends on FixNum and BigNum
FixNum store integer values that fit in a native machine word [14] (for 32-bit systems) ± 2^30
BigNum store Integer values larger than FixNum[15] (for 32-bit systems) values > 2^30
String sequence of characters/bytes [16] Does not exist.
Float real numbers with double precision floating point precision[17] ± 1.7976 * 10^308
TrueClass represents logical true value[18] true
FalseClass represents logical false value[19] false

There are a various other inbuilt classes that are provided by Ruby. Those classes can be constructed by combining one or more of the above given primitive classes. As these are existing classes, there are a lot of helper methods that allow easy calculations and operations on the objects of these classes. Some of these classes also have inbuilt iterator methods that allow the values to be modified automatically for using in loops. As with many other programming languages, some of these objects take up different size (and hence range) on different architectures. These existing classes are also editable and allows to override or add new functionality to them for custom use in the application.

Advantages and Disadvantages

In this section we discuss the various advantages and disadvanges of using the primitive data types over the corresponding object types.

Advantages

The various advantages that use of primitive data type offers are as follows:

  • Ease of use: Using primitive data types is much more efficient way of programming then programming using objects. For example, in Java when we use boxing in a loop it causes certain performance issues and is inefficient compared to using the primitive types directly. For more details and a sample program see [20].
  • Simplicity: Working with primitive data types is more intuitive.

Disadvantages

The various disadvantages of using the primitive data types are as follows:

  • Most of the object oriented programming languages do not allow the behavior or capabilities of primitive data types to be modified by programs.For example, the primitive data types in languages such as Java cannot be inherited to create further subtypes. An exception to this includes Smlltalk. It allows all data types to be extended within a program. It allows the programmer to modify the operations that can be performed on them and also to redefine the built in operations.
  • Most of the object oriented languages have certain utility functions associated with non primitive data types that eases their use as compared to primitive data types that do not have such functions. An exception to this is Ruby. It has utility functions associated with all the data types.

Conclusion

Primitive objects that are provided in object oriented programming languages have their own advantages and disadvantages. Though using primitive data types in most languages reduces the degree of customization possible, they provide a better performance and ease of use to compensate. The extent to which a primitive data type can be used efficiently depends on the application and the programming language used for the application.

  • Most of the object oriented languages have certain utility functions associated with non primitive data types that eases their use as compared to primitive data types that do not have such functions. An exception to this is Ruby. It has utility functions associated with all the data types.

References

  1. http://ece.uprm.edu/~ahchinaei/courses/2010jan/icom4036/slides/11icom4036DataTypes1.pdf
  2. http://en.wikipedia.org/wiki/Primitive_data_type
  3. http://en.wikipedia.org/wiki/Fixed-point_arithmetic
  4. http://en.wikipedia.org/wiki/C%2B%2B
  5. http://en.cppreference.com/w/cpp/language/types
  6. http://en.wikipedia.org/wiki/Wide_character
  7. http://msdn.microsoft.com/en-us/library/e02ya398(v=vs.80).aspx
  8. http://sparkcharts.sparknotes.com/cs/cplusplus/section2.php
  9. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
  10. http://msdn.microsoft.com/en-us/library/ms228360(v=vs.80).aspx
  11. http://www.cse.iitb.ac.in/~cs701/old/beamer/Closures.pdf
  12. http://ruby-doc.org/docs/ProgrammingRuby/html/builtins.html
  13. http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_integer.html
  14. http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_integer.html
  15. http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_bignum.html
  16. http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html
  17. http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_float.html
  18. http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_trueclass.html
  19. http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_falseclass.html
  20. http://today.java.net/pub/a/today/2005/03/24/autoboxing.html#performance%5Fissue