Monday, December 07, 2009

Basic/Primitive Types


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

In a previous post Identifiers, I have discussed identifiers that we can write in the source. Type restricts the identifier to size & kind of data and in addition, the operations that can be performed on that data. Both Java and C# support almost all basic data types of C++ but change characteristics of some. C++/Java/C# are strongly typed languages meaning each object must have a predefined data type.

Type Modifier:
C++ has 7 basic data types. They are int, float, double, bool, char, wchar_t, void. It uses four type modifiers to restrict the signedness and size of data that can be stored on a data type identifier -
  1. signed: Defines a positive (+ve) or negative (-ve) type. All types are implicitly positive.
  2. unsigned: A positive (+ve) type. Variable of this type can't hold negative values.
  3. short: Types are implicitly short if not otherwise specified.
  4. long: Extends the size of basic type to holder larger value.
.Net programs are compiled into Microsoft Intermediate Language (MSIL). Similar to Java Byte Code, it is  independent of hardware architecture and is translated to machine code during or before execution. But unlike Java, it brings a common type system (CTS) which means the actual program can be written in arbitrary language which can compile for .Net and the respective compiler is responsible for translating it to MSIL. MSIL defines a set of data types and each .Net compatible compiler must translate its own data types to the corresponding data types in MSIL. In other words, each data type in C# resembles a data type in MSIL.

In Java and C#, the primitive types are value types (more in Value Type vs. Reference Type and Boxing vs. Unboxing) which are allocated in the stack and copied during assignment. There are corresponding reference types in both the languages with implicit conversion from each other. The boxed types of Java are listed in "Java Boxed" and that of C# are in ".Net Type" columns.

Following is a list of the primitive data types in these languages. The sign "-" means the type is not directly supported by some particular language and the type in bracket beside it shows what can be used as a replacement.


C++
Java
Java Boxed
C#
.Net
- (unsigned char)
- (byte)
- (Byte)
byte (8)
Byte
- (signed char)
byte
Byte
sbyte (8)
SByte
int
int(32)
Integer
int (32)
Int32
unsigned int
-
-
uint (32)
UInt32
short
(short int)
short
Short
short (16)
Int16
unsigned short
-
-
ushort (16)
UInt16
long (long int)
long (64)
Long
long (64)
Int64
unsigned long
-
-
ulong (64)
UInt64
float
float
Float
float (32)
Single
double
double
Double
double (64)
Double
wchar_t
char
Character
char (16)
Char
bool
boolean
Boolean
bool (8)
Boolean
- (void*)
Object
Object
object
Object
-
-
decimal (128)
Decimal
- (std::wstring)
java.lang.String
java.lang.String
string
String

Type Qualifiers

Further keywords are used to guarantee data safety or specify data vulnerability. const

C++
Java
C#
.Net
const
final
const
-
-
-
readonly
-
volatile
-
volatile
-

Storage Class Specifiers
Further keywords are used to guarantee data safety or to specify data vulnerability.

C++
Java
C#
.Net
auto
-
-
-
register
-
-
-
static
-
-
-
extern
-
extern
-

Variables

A variable has a name and we keep some information in it. The information can be altered afterward, hence the name variable. C++/Java/C# are strongly typed languages which means each variable has a defined type i.e. you cannot assign value of a type to a variable of another type.

int textLength = 0; //defines a variable of integer type
float average = 1.0; // defines a variable of float type 

Function (aka method/routine/subroutine/operation/procedure) Names

A function or method defines a particular responsibility of the program. It has a return type and a set of parameters.

/*Add two integers and return the result*/
int
 doSum(int x, int y)
{

    return
 x + y;
}


No comments:

Post a Comment