Chapter 9. C types


Chapter 9. C# types

Object-oriented purists will argue that C# is 'more OO' than Java in a few ways. One can say that Java is not a pure OO language because it supports primitive types “ and primitive types such as Java's int , long , and double are not real objects. (There is, however, a really good reason for not making your int s and long s objects. [1] )

[1] Instantiating objects is an expensive process. When you want to perform a simple arithmetic addition, you want to perform the operation on the stack quickly and efficiently . Some programming languages do not have primitives; even integers are represented as objects. When two integers are to be added, two integer objects are actually created in the heap. The process is wasteful if all you want to do is to perform a simple arithmetic computation (in which case, there is no need to take advantage of the integer object's methods or properties).

So, we have a performance issue if we convert every little thing into objects. Anyway the Java core API includes nice wrapper classes for each of the eight Java primitive types if you really do need them. Read more in section 9.8.3.

In C#, everything can be treated like an object. Even your int s, char s, and long s “ primitive types in C# (primitive types are known officially as 'simple types') “ are also subclasses of System.Object together with all other C# classes.

Java types are grouped into two main categories:

  • primitive types (e.g. int i i is a primitive variable);

  • object reference types (e.g. Object o; o is an object reference variable).

In C#, all types are categorized into three groups:

  • pointer types

  • reference types

  • value types.

Table 9.1 shows a brief comparison between types in Java and C#. More detailed information about C# types follows .

Table 9.1. Comparing Java and C# types

Java types

C# types

Primitive type

  • Java has only eight primitive types: byte , short , int , long , float , double , boolean , char.

  • Primitive types are not objects, and cannot be manipulated as if they are objects.

  • Each primitive type variable stores its own copy of the value.

Value type

  • Types such as int , byte , short , float , etc. are known as 'simple types'. All simple types are value types in C#. Value types include other special types (such as enum types and struct types) which are not applicable in Java.

  • C# has thirteen simple value types. In addition to those of Java, C# supports unsigned types, and a new decimal type for high precision mathematical/financial calculations.

  • Value types will be converted into objects when necessary. They can be manipulated as objects and are direct subclasses of System.Object .

Reference type

Stores a reference to an object on the heap. Can also be assigned a null value. It is possible for many reference type variables to refer to the same object

Reference type

Similar to Java's reference type.

No equivalent “ pointer operations are absolutely impossible in Java.

Pointer type

Used for pointer operations only in unsafe codes. Pointer types allow the programmer to retrieve address locations of objects, and manipulate the bytes stored in memory directly.

Figure 9.1 shows how C# types are categorized.

Figure 9.1. C# type categorization. The shaded boxes are C# keywords. Besides object and string (which are aliases for System.Object and System.String , respectively), the other shaded boxes are simple value types.

graphics/09fig01.gif



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net