Visual Basic | Delphi | C++ Builder | Java

Data Types

  Bytes Basic C++ Pascal Java
String 0 - 2Gig
0 - 64K
0-256
Dim x As String
Dim x As String * length
Dim x$
char *x;
char x[256];
x: string;
y: ShortString;
z: AnsiString;
String x
(String must be capitalized)
String const   "test" "test" 'test' "test"
Character const 1 or 2 N/A 'a' - 8 bits 'a' - 8 bits
Chr(97)
'a' - 16 bits
Byte
0-255 (unsigned)
-128 to +127 (signed)
char is compiler dependent
1 Dim x As Byte char x;
unsigned char x;
signed char x;
x : Byte;
x : Shortint;
byte x;
Boolean - false=0
true =1 true =-1
true <> false
The value of true is language dependent
2 Dim x As Boolean bool x; x : Boolean;
true = 1
boolean x;
Integer
-32,768 to 32,767 (signed)
0 to 65535 (unsigned)
2 Dim x As Integer
Dim x%
short x;
unsigned short x;
int x; (16-bit compiler)
x : SmallInt
x : Word;
short x;
char x; (unicode)
Long Integer
-2,147,483,648 to
2,147,483,647
4 Dim x As Long
Dim x&
30000&
int x; (32-bit compiler)
unsigned int x;
long x;
unsigned long x;
x : Longint;
x : Integer;
x : Longword;
x : Cardinal;
int x;
Very Long Integer
-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
8 N/A N/A x : Int64; long x;
Single
1.401298E-45 to
3.402823E38
4 Dim x As Single
Dim x!
float x; x : Single; float x;
Double
4.94065645841247E-324 to
1.79769313486232E308
8 Dim x As Double
Dim x#
double x;
long double x;
x : Double;
x : Real;
double x;
Extended
19 significant digits
10 N/A N/A x: Extended; N/A
Date/Time
Jan 1, 100 to Dec 31, 9999
8 Dim x As Date N/A x: TDateTime; N/A
Variants
Do not use this
16 or 22 Dim x, y N/A V1: Variant;
V1: OleVariant;
N/A
Arrays   Dim x(5)
Dim x(5, 4) As Integer
Dim x(1 to 10)
Dim x()
int x[5];
int x[5][4];
N/A
N/A
x : Array[1..20] of Byte;
x : Array[1..40,1..6] of Byte
Same as C++
Type Cast N/A (newType)var1 newType(var1)
Sender as TButton
Constants

&HA5A5 (hex)
\b (backspace)
\010 (octal)
\x008 (hex)


$a000 (hex)
Pointers 4 AddressOf X void* ptr=0;
char* str;
P: ^Integer;
P := @X;
Y := P^;


Visual Basic 6.0

Variants can contain almost any data type. Conversion between different variant types is noramally automatic. However, this usage leads to poorly designed applications. Using variants in loops is about 6 times slower than integers. Basically, Variants should never be used. Unfortunately, many functions require variants :(

Always use Option Explicit to require that all variables are defined.

There is no way to initialize variables when they are declared.

   Dim flag As Boolean
   flag = false
Const EM_LINESCROLL = &HB6
[Public | Private] Const constname [As type] = expression

Important Constants

    VisualBasic  3.0          VisualBasic  6.0
         RED                       vbRed 
         KEY_RIGHT                 vbKeyRight
  ESC$ = Chr$(KEY_ESCAPE)   ESC$ = Chr$(vbKeyEscape)
   cr$ = Chr$(KEY_RETURN)    cr$ = Chr$(vbKeyReturn)

   Constant.txt is a part of VB 3.0; it is definitely not in VB 6.0. 
   It appears that I copied Constant.txt to create globcons.bas 
   and then included globcons.bas in my programs. (I can't remember.)
   At any rate, the constant names are now different in VB 6.0 
   (big supprise) :(
Be careful working with constants. Short integers must be less than 32K.
    l& = 30000  + 30000  'fails because it tries to compute a short int 
                         '  and then converts that to a long int
    i& = 30000& + 30000& 'works because the constants are long ints
This would also fail if 2 integer variables were being added.

The following

    Dim iA, iB, iC as Integer 
declares 2 Variants and 1 Integer. In Pascal, it would be 3 Integers. If you want 3 integers, use
    Dim iA as Integer, iB as Integer, iC as Integer 

Hex Constants - &HA5A5 'Short --- &H0B00C004& 'Long

The AddressOf operator is not available in most versions of VBA (VisualBasic for Applications). Specifically, it is not available in MS Access 97. Without this operator, there is no way to call windows API functions which require a pointer to a function.


Delphi / Pascal

Delphi is not case-sensitive.
  var
    X, Y, Z: real;
    I, J, K: Integer;
    I      : Integer = 7; // Initialization only works with global variables
  const
    fileName = 'test.ini';

Hex Constants - $a000

To place a single quote in a string, use 2 consecutive single quotes.

   'You can''t stop'
String names can not contain a dollar sign.

The variable types Integer (signed) and Cardinal (unsigned) can be either 16 or 32 bits, depending on the compiler.

In Delphi, the string variable type can declare either a long (AnsiString) or short (ShortString) string depending on a compiler flag.

Real valued constants are always of type extended


C++

Hex Constants - \x008

Most variables are allocated on the stack. To allocate variables on the heap, use new. Any time new is used to allocate memory, delete must be called before the program exits. Otherwise, the program will leak memory (make it unavailable for other programs). Arrays must be deallocated using delete[].

When declaring a variable,

To cause the compiler to treat a variable as another type, or to convert from one type to another, use the cast operator.
   a = (float)i; 
char *x; Allocates a variable which can point to a string but has no storage allocated -
x = new char[20]; would work.
char x[256]; Allocates a variable which points to a string and the memory to hold the string. The string actually contains all the bytes up to the first null '\0' byte. It is up to the programmer to be sure not to write beyond the end of the allocated space.
int x; This can be various sizes depending on the compiler. Use short or long if you want control.
char x; Whether char is signed or unsigned is compiler dependent
#define EM_LINESCROLL 0x00B6
long i = 0;


Java

Java is similar to C++ but without pointers. Variables can be initialized when they are defined.
  int n=0;
  int j=n;                         // Can initialize to the value of a variable
  String FileName="autoexec.bat";  // Note that String must be capitalized
  char SingleCharacter = 'a' ;     // In Java, 'a' is 16 bits, in C it is 8 bits
\n = New line works in some Java programs but is a problem using awt.


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / DataTypes.htm