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
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 intsThis would also fail if 2 integer variables were being added.
The following
Dim iA, iB, iC as Integerdeclares 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
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++
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,
a = (float)i;
|
Escape Sequences \a Bell (alert) \b Backspace \f Formfeed \n New line \r Carriage return \t Horizontal tab \v Vertical tab \' Single quotation mark \" Double quotation mark \\ Backslash (Warning: Required in DOS file paths) \? Question mark \ooo Octal constant \xhhh Hexadecimal constant
#define EM_LINESCROLL 0x00B6 long i = 0;
Java
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.