|
Operation |
Cobol |
Java |
VB.NET |
|
Program Definition |
|
Class ClassName{ int var1 = 5; String var2 = nes String(world); public MyProgram(){ ; } int getVar1(){ return var1; } } |
|
|
Variable name length |
30 characters |
|
|
|
Data types and variable limits |
Removing the S makes numbers unsigned V is implied decimal point External decimal - PIC S999 DISPLAY Binary - PIC S9999 BINARY - COMP - COMP-4 - COMP-5 (native binary 2,4, or 8 bytes) Internal Decimal - PIC S9999 PACKED-DECIMAL - PIC S9999 COMP-3 Internal Floating Point - COMP-1 (4 bytes) - COMP-2 (8 bytes) External Floating Point PIC S9(02)V9(02)E+99 Character PIC X(nn) |
boolean (1 bit) true or false char (16 bits) \uooo to \uFFFF (ISO Unicode char set) byte (8 bits) -128 to +127 short (16 bits) 32,768 to 32767 int (32 bits) 2147483648 to +2147483647 long (64 bits) 9,233,372,036,854,775,808 to 9,233,372,036,854,775,807 float (32 bits) -3.40292347E+38 to +3.40292347E+38 double (64 bits) -1.79769313486231570E+308 to +1.79769313486231570E+308 null undefined |
Boolean (16 bits) True or False Dim switch as Boolean = True Char (16 bits)
(1 unicode char) Dim character as Char = Ac Byte (8 bits) 0 to 255 Date (64 bits) 1Jan 0001 to Decimal (128 bits) 1.0E-28 to 7.9E+28 Short (16 bits) 32,768 to 32767 Integer (32 bits) 2147483648 to +2147483647 Long (64 bits) 9,233,372,036,854,775,808 to 9,233,372,036,854,775,807 Single (32 bits) +-1.5E-45 to +-3,4E+38 Double(64 bits) +5.0E-324 to +-1.7E+308 Object any datatype String 0 to ~2,000,000,000 Unicode characters A value of Nothing denotes an empty reference |
|
|
Numeric If the ARITH(COMPAT) compiler option An S in front of the size specification indicates a signed number PIC S9(nn)V9(nn) DISPLAY PIC 9(01 -> 04) BINARY(or BIN) (2 bytes storage) PIC 9(05 -> 09) BIN (4 bytes) PIC 9(10 -> 18) BIN (8 bytes) (COMP and COMP-4 are equivalent to BIN) (operational sign in leftmost bit) COMP-1 (4 bytes) floating point COMP-2 (8-bytes) double precision floating point PIC 9(xx)V9(XX) COMP-3 packed decimal (up to total of 31 digits of ARITH(EXTEND, COMP 3 same as PACKED-DECIMAL) PIC |
|
|
|
Special Output Characters |
|
\n Newline \t Horizontal tab \r Carriage return \\ - backslash \ - single quote \ double quote \u#### - Unicode char |
Tab vbTab Carriage return/line feed vbCrLf Output &= vbCrLf & text & vbTab & moreText |
|
Implicit data conversion |
|
|
Dim string1 as String Dim int1 as Integer String1 = 1 Int1 = String 1 VB.net does implicit data conversion) Or int1 = Console.ReadLine() and here also |
|
Writing to console |
|
System.out.println( .); |
Console.WriteLine(The sum is {0}, int1) {0} is replaced by value of int1. To write multiple values put in {1} {2}, and follow int1, by int2, int3, |
|
Creating a variable |
01 VAR-A PIC X(01) 01 VAR-NUM PIC 9(02). |
int i = 0; |
Dim var1 as Integer To give variable namespace scope replace Dim with Public |
|
Create a structure |
01 VAR-STRUCT. 05 VAR-A1 PIC X(01). 05 VAR-A2 PIC S9(08) COMP. |
Create a class that contains all the needed variables. class MyData{ char varA1; int varA2; public MyData(){ ; } void setVarA1(char varA1){ this.varA1 = varA1; } void getVarA1(){ return varA1; } void setVarA2(char varA2){ this.varA1 = varA2; } |
|
|
Array (a.k.a. Table) |
01 table-name 05 element-name OCCURS n times (depending on) 10 element1 PIC X(01). 10 element2 PIC S9(08) COMP. Up to 7 dimensions can be defined |
MyData[] myDataArray = new MyData[5]; A java array can be an array of primitives (eg. Integer, char, etc. or objects) |
Arrays have 0 based indexing Dim parmArray as Integer() ParmArray = New Integer(11) {} Dim parmArray = New Integer(){1,2,3,4,5} Dim parmArray1, parmArray2 as Double() Multidimensional Arrays Dim myArray As Integer(,) = new Integer(4,4) {} Dim myArray As Integer(,) = New Integer(,) {{1,1},{2,2}} Tagged Arrays Dim myArray As Integer()() MyArray = New Integer(1)() {} MyArray(0) = New Integer() {1,2} MyArray(1) = New Integer(){1,2,3} Determine length of each row with MyArray.getUpperBound(n) (n is 0, 1, ) {} contains the initialization list. If empty the array is initialized to the default value for the datatype (0s, spaces, False, Nothing (for references)) Arrays are really an object contain similar primitives (Integer, Char, etc. or other objects) For i = 0 to parmArray.GetUpperBound(0) Next |
|
Value assignment |
Move A to B Move A to B(1) (array element) Add A to B giving C (or Subtract, etc.) Add A to B Compute A = B / 2 |
b = a; b[1] = a; c = a + b b += a; ++a (preincrement, increment then use incremented value a++ (postincrement) --a (predecrement) a(post-decrement) |
Value += 3 The operator can be +, -, *, ^, &, /, \ If a equals 2 a += 5 (a will be 7) a -= 2 (a will be 0) a /=2 (a will be 1) b equals Hello b &= World (b will contain Hello World) |
|
Relational operators |
A Equal B or A = B A Not Equal B or A NOT = B A greater than B or A > B A Greater Than or Equal to B or A >= B A Less Than B or A < B A Less Than or Equal to B or A <= B |
A == B (for primitives) or A.equals(B) (for objects) A != B (for primitives) or ! (A.equals(B)) (for objects) A > B (for primitives) A >= B A < B A < = B |
Equal = Not equal <> Greater than > Less than < Greater or equal than >= Less than or equal <= |
|
Boolean expressions |
(condition1) AND (condition2) (condition1) OR (condition2) |
(condition1) && (condition2) or A && B (condition1) || (condition2) or A || B (A and B are Boolean values) |
If sex = M AndAlso age < 10 (short circuit eval) If sex = M And . If OrElse (short circuit eval) If Or If Xor If Not Short circuit eval - Evaluation will stop if first expression false (AndElse) or true (OrElse) AndAlso has higer precedence than OrElse Use of And / Or evaluates all expressions |
|
Arithmetic operations |
|
|
Addition + a + b Subtraction - c - d Multiplication * e * f Division (float) / a / b Division (integer) \ c \ d Modulus Mod a Mod s Exponentiation ^ a ^ b Unary Negative - -a Unary Positive + +g Floating point numbers in integer division are first rounded to closest integer then divided 7.7 / 4 = 2 |
|
Concatenation |
|
+ System.out.println (number = + 1); myString.concat(myOtherString); |
& concatenate this & with that |
|
Line continuation |
- (hypen in column 7) |
|
_ (underscore) |
|
IF THEN ELSE |
IF (condition)
ELSE
END-IF |
If (condition) { } else{ } {} only needed if more than 1 statement within if or else |
If (condition is true) Then
Else
End If A single line if/then does not need End If If (condition is true) Then .. If ( ) Then
ElseIf ( ) .. ElseIf ( )
Else
End If |
|
Evaluate |
Evaluate var When value1
When value2
When Other
End Evaluate Evalute condition (eg true) When value1
When value2
When Other
End-Evaluate |
switch (conditional) { case value1: statements1 break; case value2: statements2 break; default: statements3 } break causes the program to go to first statement after the switch structure without break program control would execute all subsequent case statements |
Select Case variable Case 10
Case 20 to 30
Case 0, 31 to 40 (multiple values separated by ,)
Case Is < 0
Case Else
End Select |
|
LOOP varying |
Perform varying ws-index from 1 by 1 until Ws-index > value
End-perform Perform until (condition is true)
End-perform |
for (int i=0; i<=4; ++i) { System.out.println( myData[I].getVarA1()); } using the break statement causes immediate exit from the for structure using a |
Dim counter as Integer For counter = 1 to 10 Step 1 Step is optional . Exit For Next Special array looping Dim grades as Integer(,) = New Integer(,){{30,60,90}},{40,20},{98,75,85}} Dim grade as Integer For Each grade in grades If grade < 75 Then
End If Next |
|
LOOP with test before |
PERFORM TEST BEFORE UNTIL conditional statementsEND-PERFORM |
while (conditional) { statements}
using the break statement causes immediate exit from the while structure |
While ( ) loops while condition true
Exit While End While Do While ( ) loops while condition true
Exit Do |
|
LOOP with test after |
PERFORM TEST AFTER UNTIL conditional statements END-PERFORM |
do { statements } while (conditional);
using the break statement causes immediate exit from the do structure |
Do execute at least once . Exit Do Loop While ( ) Do
Exit Do Loop Until ( ) |
|
Execute routine |
Perform paragraph-name Or CALL subroutine using var1, var2 (in quotes makes call static Call subroutine-name using var1, var2 (name of subroutine in subroutine-name variable makes call dynamic) |
routine(); routine(var1,
var2); or myclass.routine(var1,
var2) ; |
|
|
Comment |
* in column 7 |
// - comment to end of line /* Comment block */ /** * Javadoc */ |
(Single quote) to end of line |
|
Procedures |
|
|
Sub My_procedure(ByVal xx As dataytpe, ByRef yy As datatype)
End Sub |
|
Functions |
|
|
Function (ByVal xx As datatype, ByRef yy as dataType) as Datatype
return zz End Function zz datatype is that described in Function definition ToString() function is expected to be overridden to provide an object String representation |
|
Modules |
|
N/A |
Can contain procedures and functions When included in a project, procedures and functions have namespace scope Every console application consists of at least one module and one procedure Names must consist of characters of letters, digits and underscores (Tip begin each module name with mod) Names not case sensitive Each console app module must have a Main procedure |
|
Methods |
|
|
Same as a procedure but defined in a class A overridden base class method can be accesses from the derived class by preceding base class method name with keyword MyBase. Finalize is common overridable derived class method. MyBase.Finalize() should be last statement called to release any base class resources |
|
Passing arguments |
|
|
Byval -by value copy made and passed copy can be modified but original value will remain unchanged. ByRef (by reference - pointer to original value passed and original value can be modified Exceptions ByVal myString as String can not be modified directly. Enclose the calling routine parm in parentheses() to pass a copy of the value to a routine even if the variable in the procedure header is defined awith ByRef Reference type (non-primitive) variables passed with keyword ByVal are really passed by reference Sub MySub(ByVal myArray As Integer()) The sub can update values in myArray but can not change to different reference. Sub MySub(ByRef myArray As Integer()) The sub can create new array and assign it to myArray which is returned to calling routine |
|
Variable length parameter lists |
|
|
Use keyword ParamArray Sub VariableParmList(ByVal ParamArray myArray As Character) Eg. VariableParmList() VariableParmList(A,B,C) |
|
Strong data typing |
|
Inherent |
Optional Compile time options Option Explicit On (on is default) declare all variables explicitly before use Option Strict Off (off is default) when On compiler checks all conversions and flags errors where narrowing conversion that could cause data loss or invalid type conversion (eg, string to Integer) |
|
Scope rules |
|
|
Class scope Starts after keyword Class and ends at End Class Module scope Variables declared in a module have similar visibility as class scope Namespace scope Block scope Starts at the body of a procedure or the body of an If/then or other block structure and ends at the End, Next or equivalent statement. Similarly named block variables in nested blocks causes an error |
|
Procedure overloading |
|
|
Allowed Overloaded functions can have different return types |
|
Optional parameters |
|
|
Allowed Sub OptionalParms(ByVal parm1 as Integer, Optional ByVal parm2 as Long = 0) |
|
Variable and method visibility and scope |
|
public private package static |
Public (Static) Private (Static) Shared Protected Friend Shared (keyword) constructors are called before any Shared class members are used and any class objects intstantiated Private properties/methods are not visible to their subclasses. Protected members can be accessed only in that base class or any derived classes Friend members can be accessed by any part of the assembly in which Friend member is declared Base class methods must be declared with Overridable if that method is to be overridden in a derived class |
|
Set/Get properties |
|
|
Private roomSize as Integer Public Property Size() as Integer Get Return roomSize End Get Set (ByVal value as Integer) If (value not with limits) do something else roomSize = value End If End Set End Property A property can be used in the same way as a variable (assign values with = ) A property with only a Get accessor is called read-only and must be declared with the keyword ReadOnly. A property with only a Set accessor is called a write only property and must be decalred using keyword WriteOnly |
|
Constants and/or read only |
|
|
Const - variable declared as read only must be initialized in its declaration ReadOnly variable can be initialized either in its declaration or in the class constructor. Neither a Const or ReadOnly value can be modified once initialized |
|
Creating a class |
|
Every class derived from Object class |
Every class derived from Object class Concrete Class Class MyClass Inherits Object (or other class) Private var1 As Integer Public var2 As Boolean Public Sub New()
End Sub Public Sub (or Function)
End Sub Private Sub (or Function)
End Sub Protected Sub (or Function) End Sub Friend Sub (or Function) End Sub
End Class Public NotInheritable Class xxxx Inherits yyyy (can not be a base class) Public notOverridable Sub( ) (can not override in derived class) Abstract Class Public MustInherit Class AbsClass Public Overridable Function/Sub
End Function/Sub
Public MustOverride Property MyProp() as String (Note no End Property in definition) End Class An abstract class can have either concrete or abstract methods or properties |
|
Class/instance variable/method references to itself |
|
this. |
Me. |
|
Msg box |
|
|
MessageBox.Show( message_string, dialog_title, Types_of_buttons, Icon_type)
Types_of_buttons (may be incomplete) MessageBoxIcon.Exclamation MessageBoxIcon.Information MessageBoxIcon.Question MessageBoxIcon.Error Icon_type MessageBoxButtons.OK MessageBoxButtons.OKCancel MessageBoxButtons.YesNo MessageBoxButtons.YesNoCancel MessageBoxButtons.RetryCancel |
|
Code location |
|
package |
Namespace |
|
Executables Non-executables |
|
Must have a main() method Compiled to a .cls file Compiled to a .cls file |
Must have a main() method Compiled to an .exe Compiled to a .dll (code library/class files) Classes by default are placed in the .exe unless compiled as .dll and imported into a program |
|
Garbage collection |
|
|
System.GC.Collect() |
|
Delegates |
|
|
Passing method references as arguments (classes that encapsulate a set of references to methods)
An object sends the delegate instance |
|
Interface |
|
Defined as Interface Shape { double area(); double volume(); double getName(); } Implemented by public class Sphere implements Shape { public double area(){ return } public double volume(){ return }
The class that implements an interface will remain abstract if any method in interface undefined |
Defined as Interface Shape Function Area() as Double Function Volume() As Double ReadOnly Property Name() as String Implemented by Public Class Sphere implements Shape Public Overridable Function Area() As Double Implements IShape.Area
End Function Public Overridable Function Volume() As Double implements IShape.Volume
End Function Public Overridable Readonly Property Name() As String Implements IShape.Name Get . End Get End Property An interface can only be declared as Public or Friend. A class must define all methods and properties in the Interface Recommended convention is to start each interface name with an I |
|
Cast/instance of |
|
|
derived_class = CType(object, className) Can cause InvalidCastException if base-class reference cast to derived class If (TypeOf className Is objectType) |
|
Exceptions |
|
try{ .. } catch (NumberFormatException nfe){ . } catch(DivideByZeroException dbz){ } finally{
} Methods passing exceptions up the call stack int myMethod( ) throws NumberFormatException, SQLException, ..{ . } Method throwing an exception public void myMethod() throws MyException{ . throw new MyException(..); } |