CSC/ECE 517 Fall 2011/ch3 3h ss: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 94: Line 94:
     var s = "Hello"
     var s = "Hello"
     var len = s.length;                                  // assigns value 5 to len
     var len = s.length;                                  // assigns value 5 to len
=== C# ===
C# is a strongly typed language, where it is necessary to declare the data type of a variable and also be aware of the data type conversion. C# provides a significant number of primitive data types. [http://www.java2s.com/Tutorial/CSharp/0040__Data-Type/PrimitivesinC.htm See [1]]
Because C# represents all primitive data types as objects, it is possible to call an object method on a primitive data type. For example:
    static void Main()
    {
        int x = 7;
        object o = x;
        System.Console.WriteLine(o.ToString());
    }
Some data types (e.g. Decimal and String) can appear like primitives at first sight, but are actually not. So it is important to exercise caution before making such assumptions. To test whether a particular type is a primitive or not you can use the property Type.IsPrimitive.
Consider the following example:
    if (t.IsPrimitive)    // where t is the type
    {
        // Is Primitive
    } else if (t == typeof(Decimal))
    {
        // Is Decimal
    } else if (t == typeof(String))
    {
        // Is String
    } else
    {
        // Other type
    }

Revision as of 23:48, 6 October 2011

Introduction

In any programming language, the data type refers to the class of data which contains specific type or range of values. Data types are used along with variables used in the program. The data type tells us what kind of values the variable can store, what is the range of the values and how much space the values take in memory etc.

The primitive data types refer to the built-in data types which are supported by the programming language. Often 'data types' and 'primitive data types' are used interchangeably.

Many object-oriented programming languages provide support for primitive data types while some object-oriented programming languages provide support for primitive objects along with primitive types. Primitive objects refer to the objects of built-in classes which provide the same functionality or rather more functionality than the primitive types. Some object-oriented programming languages provide support for only primitive objects.

The common built-in data types are integers, floating point numbers, characters, strings and booleans.

Different object-oriented programming languages implement these primitive data types and primitive objects in different manner.


Primitive objects in different OO languages

Ruby

Since Ruby is a pure object oriented language, everything in Ruby is an object. Hence, all primitive types such as integers, floating point numbers, strings, are objects of a built-in class. All integers are primitive objects of either class Fixnum or Bignum. e.g. 10.class returns Fixnum. This indicates that 10 is converted into an object of type Fixnum. A numeric literal with a decimal point and/or an exponent is a primitive object of Float. e.g. 7.45.class returns Float. This indicates that 7.45 is converted into an object of type Float. Single quoted literals and double quoted literals are primitive objects of String. e.g. 'hi'.class and "hello".class both return String. This indicates that 'hi' and "hello" are both converted into an object of type String.

Since all primitive types in Ruby are objects, we should be able to call methods of the Object class on them. Let us demonstarte the same for integer and float using the following example:

   a=10
   puts a.to_f  
   b=20.5
   puts b.to_i
   Output:
   10.0
   20


Java

In Java, there are 8 primitive types: byte, short, int, long, float, double, char and boolean

For each of these primitive types, Java provides wrapper classes to create primitive objects which wrap the primitive types. On primitive objects, not only the operations supported by primitive values can be invoked but also the operations which cannot be invoked on primitive values. In Java, the primitve values are not implicitly converted to primitive objects. Instead, methods are provided for doing explicit conversion. The primitive objects are stored on heap in memory while the variables containing primitive values are stored on heap.

Primitive Type Wrapper Class Range
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Example:

   int i = 10;
   int ii = 20;
   Integer I = new Integer(i);
   Integer II = new Integer(ii);
   System.out.println(I+II);            // prints 30
   System.out.println(I.equals(II));    // prints false

JavaScript

There are 5 primitive data types in JavaScript: string, number, boolean, null, undefined

For string, number and boolean values, there are corresponding Classes just like in Java to create primitive objects which wrap the primitive values.

Primitive Type Wrapper Class Range
string String
number Number
boolean Boolean

In JavaScript, whenever a variable of a primitive type is defined, it is implicitly converted to a primitive object, so that a property can be accessed or a method can be invoked on that variable as if it is an object. After the method is processed, the primitive object is no longer needed and hence discared.

Example:

   var upperCaseString = "APPLE";
   var lowerCaseString = upperCaseString.toLowerCase();  // assigns string "apple" to lowerCaseString 
   var s = "Hello"
   var len = s.length;                                   // assigns value 5 to len


C#

C# is a strongly typed language, where it is necessary to declare the data type of a variable and also be aware of the data type conversion. C# provides a significant number of primitive data types. See [1]

Because C# represents all primitive data types as objects, it is possible to call an object method on a primitive data type. For example:

   static void Main()
   {
       int x = 7;
       object o = x;
       System.Console.WriteLine(o.ToString());
   }

Some data types (e.g. Decimal and String) can appear like primitives at first sight, but are actually not. So it is important to exercise caution before making such assumptions. To test whether a particular type is a primitive or not you can use the property Type.IsPrimitive.

Consider the following example:

   if (t.IsPrimitive)    // where t is the type
   {
       // Is Primitive
   } else if (t == typeof(Decimal))
   {
       // Is Decimal
   } else if (t == typeof(String))
   {
       // Is String
   } else
   {
       // Other type
   }